Class: JSON::GenericObject

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/oj/json.rb

Overview

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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.json_creatable=(value) ⇒ Object (writeonly)

Sets the attribute json_creatable

Parameters:

  • value

    the value to set the attribute json_creatable to.



126
127
128
# File 'lib/oj/json.rb', line 126

def json_creatable=(value)
  @json_creatable = value
end

Class Method Details

.dump(obj, *args) ⇒ Object



154
155
156
# File 'lib/oj/json.rb', line 154

def dump(obj, *args)
  ::JSON.dump(obj, *args)
end

.from_hash(object) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/oj/json.rb', line 134

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

.json_creatable?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/oj/json.rb', line 122

def json_creatable?
  @json_creatable
end

.json_create(data) ⇒ Object



128
129
130
131
132
# File 'lib/oj/json.rb', line 128

def json_create(data)
  data = data.dup
  data.delete JSON.create_id
  self[data]
end

.load(source, proc = nil, opts = {}) ⇒ Object



149
150
151
152
# File 'lib/oj/json.rb', line 149

def load(source, proc = nil, opts = {})
  result = ::JSON.load(source, proc, opts.merge(:object_class => self))
  result.nil? ? new : result
end

Instance Method Details

#[](name) ⇒ Object



166
167
168
# File 'lib/oj/json.rb', line 166

def [](name)
  __send__(name)
end

#[]=(name, value) ⇒ Object



170
171
172
# File 'lib/oj/json.rb', line 170

def []=(name, value)
  __send__("#{name}=", value)
end

#as_jsonObject



178
179
180
# File 'lib/oj/json.rb', line 178

def as_json(*)
  { JSON.create_id => self.class.name }.merge to_hash
end

#to_hashObject



162
163
164
# File 'lib/oj/json.rb', line 162

def to_hash
  table
end

#to_json(*a) ⇒ Object



182
183
184
# File 'lib/oj/json.rb', line 182

def to_json(*a)
  as_json.to_json(*a)
end

#|(other) ⇒ Object



174
175
176
# File 'lib/oj/json.rb', line 174

def |(other)
  self.class[other.to_hash.merge(to_hash)]
end