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.



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

def json_creatable=(value)
  @json_creatable = value
end

Class Method Details

.dump(obj, *args) ⇒ Object



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

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

.from_hash(object) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/oj/json.rb', line 131

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)


119
120
121
# File 'lib/oj/json.rb', line 119

def json_creatable?
  @json_creatable
end

.json_create(data) ⇒ Object



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

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

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



146
147
148
149
# File 'lib/oj/json.rb', line 146

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



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

def [](name)
  __send__(name)
end

#[]=(name, value) ⇒ Object



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

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

#as_jsonObject



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

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

#to_hashObject



159
160
161
# File 'lib/oj/json.rb', line 159

def to_hash
  table
end

#to_json(*a) ⇒ Object



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

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

#|(other) ⇒ Object



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

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