1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
require 'ostruct'
require 'oj/state'
if defined?(JSON::PRETTY_STATE_PROTOTYPE)
# There are enough people that try to use both the json gen and oj in mimic
# mode so don't display the warning.
# warn "WARNING: oj/json is a compatability shim used by Oj. Requiring the file explicitly is not recommended."
end
unless defined?(JSON::PRETTY_STATE_PROTOTYPE)
module JSON
NaN = 0.0/0.0 unless defined?(::JSON::NaN)
Infinity = 1.0/0.0 unless defined?(::JSON::Infinity)
MinusInfinity = -1.0/0.0 unless defined?(::JSON::MinusInfinity)
# Taken from the unit test. Note that items like check_circular? are not
# present.
PRETTY_STATE_PROTOTYPE = Ext::Generator::State.from_state({
:allow_nan => false,
:array_nl => "\n",
:ascii_only => false,
:buffer_initial_length => 1024,
:depth => 0,
:indent => " ",
:max_nesting => 100,
:object_nl => "\n",
:space => " ",
:space_before => "",
}) unless defined?(::JSON::PRETTY_STATE_PROTOTYPE)
SAFE_STATE_PROTOTYPE = Ext::Generator::State.from_state({
:allow_nan => false,
:array_nl => "",
:ascii_only => false,
:buffer_initial_length => 1024,
:depth => 0,
:indent => "",
:max_nesting => 100,
:object_nl => "",
:space => "",
:space_before => "",
}) unless defined?(::JSON::SAFE_STATE_PROTOTYPE)
FAST_STATE_PROTOTYPE = Ext::Generator::State.from_state({
:allow_nan => false,
:array_nl => "",
:ascii_only => false,
:buffer_initial_length => 1024,
:depth => 0,
:indent => "",
:max_nesting => 0,
:object_nl => "",
:space => "",
:space_before => "",
}) unless defined?(::JSON::FAST_STATE_PROTOTYPE)
def self.dump_default_options
Oj::MimicDumpOption.new
end
def self.dump_default_options=(h)
m = Oj::MimicDumpOption.new
h.each do |k, v|
m[k] = v
end
end
def self.parser=(p)
@@parser = p
end
def self.parser()
@@parser
end
def self.generator=(g)
@@generator = g
end
def self.generator()
@@generator
end
module Ext
class Parser
def initialize(src)
raise TypeError.new("already initialized") unless @source.nil?
@source = src
end
def source()
raise TypeError.new("already initialized") if @source.nil?
@source
end
def parse()
raise TypeError.new("already initialized") if @source.nil?
JSON.parse(@source)
end
end # Parser
end # Ext
State = ::JSON::Ext::Generator::State unless defined?(::JSON::State)
begin
send(:remove_const, :Parser)
rescue
# ignore and move on
end
Parser = ::JSON::Ext::Parser unless defined?(::JSON::Parser)
self.parser = ::JSON::Ext::Parser
self.generator = ::JSON::Ext::Generator
# Taken directly from the json gem. Shamelessly copied. It is similar in
# some ways to the Oj::Bag class or the Oj::EasyHash class.
class GenericObject < OpenStruct
class << self
alias [] new
def json_creatable?
@json_creatable
end
attr_writer :json_creatable
def json_create(data)
data = data.dup
data.delete JSON.create_id
self[data]
end
def from_hash(object)
case
when object.respond_to?(:to_hash)
result = new
object.to_hash.each do |key, value|
result[key] = from_hash(value)
end
result
when object.respond_to?(:to_ary)
object.to_ary.map { |a| from_hash(a) }
else
object
end
end
def load(source, proc = nil, opts = {})
result = ::JSON.load(source, proc, opts.merge(:object_class => self))
result.nil? ? new : result
end
def dump(obj, *args)
::JSON.dump(obj, *args)
end
end # self
self.json_creatable = false
def to_hash
table
end
def [](name)
__send__(name)
end unless method_defined?(:[])
def []=(name, value)
__send__("#{name}=", value)
end unless method_defined?(:[]=)
def |(other)
self.class[other.to_hash.merge(to_hash)]
end
def as_json(*)
{ JSON.create_id => self.class.name }.merge to_hash
end
def to_json(*a)
as_json.to_json(*a)
end
end
end # JSON
end
|