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.



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

def json_creatable=(value)
  @json_creatable = value
end

Class Method Details

.dump(obj, *args) ⇒ Object



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

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

.from_hash(object) ⇒ Object



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

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)


113
114
115
# File 'lib/oj/json.rb', line 113

def json_creatable?
  @json_creatable
end

.json_create(data) ⇒ Object



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

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

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



140
141
142
143
# File 'lib/oj/json.rb', line 140

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



157
158
159
# File 'lib/oj/json.rb', line 157

def [](name)
  __send__(name)
end

#[]=(name, value) ⇒ Object



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

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

#as_jsonObject



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

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

#to_hashObject



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

def to_hash
  table
end

#to_json(*a) ⇒ Object



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

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

#|(other) ⇒ Object



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

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