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.


115
116
117
# File 'lib/oj/json.rb', line 115

def json_creatable=(value)
  @json_creatable = value
end

Class Method Details

.dump(obj, *args) ⇒ Object


143
144
145
# File 'lib/oj/json.rb', line 143

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

.from_hash(object) ⇒ Object


123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/oj/json.rb', line 123

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)

111
112
113
# File 'lib/oj/json.rb', line 111

def json_creatable?
  @json_creatable
end

.json_create(data) ⇒ Object


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

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

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


138
139
140
141
# File 'lib/oj/json.rb', line 138

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


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

def [](name)
  __send__(name)
end

#[]=(name, value) ⇒ Object


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

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

#as_jsonObject


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

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

#to_hashObject


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

def to_hash
  table
end

#to_json(*a) ⇒ Object


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

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

#|(other) ⇒ Object


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

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