Method: Megam::JSONCompat.from_json

Defined in:
lib/megam/core/json_compat.rb

.from_json(source, opts = {}) ⇒ Object

Just call the JSON gem’s parse method with a modified :max_nesting field



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/megam/core/json_compat.rb', line 84

def from_json(source, opts = {})
  obj = ::Yajl::Parser.parse(source)

  # JSON gem requires top level object to be a Hash or Array (otherwise
  # you get the "must contain two octets" error). Yajl doesn't impose the
  # same limitation. For compatibility, we re-impose this condition.
  unless obj.kind_of?(Hash) or obj.kind_of?(Array)
    raise JSON::ParserError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})"
  end

  # The old default in the json gem (which we are mimicing because we
  # sadly rely on this misfeature) is to "create additions" i.e., convert
  # JSON objects into ruby objects. Explicit :create_additions => false
  # is required to turn it off.
  if opts[:create_additions].nil? || opts[:create_additions]
    map_to_rb_obj(obj)
  else
  obj
  end
end