Module: SimpleMapper::JsonFormat::ClassMethods

Defined in:
lib/simple_mapper/formats/json_format.rb

Instance Method Summary collapse

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'}}]}


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/simple_mapper/formats/json_format.rb', line 31

def from_json(json)
  doc = Serialize.hash_from_json(json)
  # doc could include a single 'model' element, or a 'models' wrapper around several.
  if doc.is_a?(Hash)
    # In contrast to XML, JSON is not restricted to one top-level key. We will assume the objects are in a hash/array
    # referenced by either singular or plural of the klass.
# puts "Received #{doc.length} bytes of json. Top keys: #{doc.keys.join(', ')}. Looking for '#{self.entity_name.underscore}' or '#{self.entity_name.pluralize.underscore}'"
    meta = doc.dup
    key = if doc[self.entity_name.underscore]
      self.entity_name.underscore
    elsif doc[self.entity_name.pluralize.underscore]
      self.entity_name.pluralize.underscore
    end
    return nil if key.nil?
# puts "JSON has #{key}"
    meta.delete(key) # removing the data leaves us only the meta information
    meta.freeze
    ret = if doc[key].is_a?(Array)
      doc[key].collect do |e|
        obj = self.load(e)
        obj.instance_variable_set(:@meta, meta)
        obj
      end
    else
      obj = self.load(doc[key])
      obj.instance_variable_set(:@meta, meta)
      obj
    end
# puts "Collected: #{(ret.is_a?(Array) ? ret : [ret]).length} objects"; ret
  else # doc isn't a hash, probably nil
    doc
  end
end