Module: SimpleMapper::JsonFormat::ClassMethods
- Defined in:
- lib/simple_mapper/formats/json_format.rb
Instance Method Summary collapse
-
#from_json(json) ⇒ Object
This assumes a standard json format: ‘person’:{‘attribute’:”,‘another_att’:‘value’} And for a collection of objects: 1’},‘person’:{‘attribute’:”,‘another_att’:‘value’}]}.
Instance Method Details
#from_json(json) ⇒ Object
This assumes a standard json format:
{'person':{'attribute':'','another_att':'value'}}
And for a collection of objects:
{'people':[{'person':{'attribute':'','another_att':'value 1'}},{'person':{'attribute':'','another_att':'value'}}]}
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/simple_mapper/formats/json_format.rb', line 27 def from_json(json) doc = Serialize.hash_from_json(json) # doc could include a single 'model' element, or a 'models' wrapper around several. puts "Top-level JSON key(s): #{doc.keys.inspect}" if @debug if doc.is_a?(Hash) key = doc.keys.first if doc[key] && doc[key].keys.uniq == [key.singularize] && doc[key][key.singularize].is_a?(Array) puts "Several objects returned under key '#{key}'/'#{key.singularize}':" if @debug doc[key][key.singularize].collect do |e| puts "Obj: #{e.inspect}" if @debug Object.module_eval("::#{key.singularize.camelize}", __FILE__, __LINE__).load(e) end elsif doc[key] # top-level must be single object Object.module_eval("::#{key.singularize.camelize}", __FILE__, __LINE__).load(doc[self.name.underscore]) else nil end else # doc isn't a hash, probably nil doc end end |