Class: JSON::GenericObject
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- JSON::GenericObject
- 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
-
.json_creatable ⇒ Object
writeonly
Sets the attribute json_creatable.
Class Method Summary collapse
- .dump(obj, *args) ⇒ Object
- .from_hash(object) ⇒ Object
- .json_creatable? ⇒ Boolean
- .json_create(data) ⇒ Object
- .load(source, proc = nil, opts = {}) ⇒ Object
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #as_json ⇒ Object
- #to_hash ⇒ Object
- #to_json(*a) ⇒ Object
- #|(other) ⇒ Object
Class Attribute Details
.json_creatable=(value) ⇒ Object (writeonly)
Sets the attribute json_creatable
111 112 113 |
# File 'lib/oj/json.rb', line 111 def json_creatable=(value) @json_creatable = value end |
Class Method Details
.dump(obj, *args) ⇒ Object
139 140 141 |
# File 'lib/oj/json.rb', line 139 def dump(obj, *args) ::JSON.dump(obj, *args) end |
.from_hash(object) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/oj/json.rb', line 119 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
107 108 109 |
# File 'lib/oj/json.rb', line 107 def json_creatable? @json_creatable end |
.json_create(data) ⇒ Object
113 114 115 116 117 |
# File 'lib/oj/json.rb', line 113 def json_create(data) data = data.dup data.delete JSON.create_id self[data] end |
.load(source, proc = nil, opts = {}) ⇒ Object
134 135 136 137 |
# File 'lib/oj/json.rb', line 134 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
151 152 153 |
# File 'lib/oj/json.rb', line 151 def [](name) __send__(name) end |
#[]=(name, value) ⇒ Object
155 156 157 |
# File 'lib/oj/json.rb', line 155 def []=(name, value) __send__("#{name}=", value) end |
#as_json ⇒ Object
163 164 165 |
# File 'lib/oj/json.rb', line 163 def as_json(*) { JSON.create_id => self.class.name }.merge to_hash end |
#to_hash ⇒ Object
147 148 149 |
# File 'lib/oj/json.rb', line 147 def to_hash table end |
#to_json(*a) ⇒ Object
167 168 169 |
# File 'lib/oj/json.rb', line 167 def to_json(*a) as_json.to_json(*a) end |
#|(other) ⇒ Object
159 160 161 |
# File 'lib/oj/json.rb', line 159 def |(other) self.class[other.to_hash.merge(to_hash)] end |