class Chef::JSONCompat

Constants

JSON_MAX_NESTING

Public Class Methods

from_json(source, opts = {}) click to toggle source

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

# File lib/chef/json_compat.rb, line 39
def from_json(source, opts = {})
  obj = parse(source, opts)

  # 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) || obj.kind_of?(Array)
    raise Chef::Exceptions::JSON::ParseError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})"
  end

  obj
end
parse(source, opts = {}) click to toggle source

API to use to avoid create_addtions

# File lib/chef/json_compat.rb, line 32
def parse(source, opts = {})
  FFI_Yajl::Parser.parse(source, opts)
rescue FFI_Yajl::ParseError => e
  raise Chef::Exceptions::JSON::ParseError, e.message
end
to_json(obj, opts = nil) click to toggle source
# File lib/chef/json_compat.rb, line 52
def to_json(obj, opts = nil)
  FFI_Yajl::Encoder.encode(obj, opts)
rescue FFI_Yajl::EncodeError => e
  raise Chef::Exceptions::JSON::EncodeError, e.message
end
to_json_pretty(obj, opts = nil) click to toggle source
# File lib/chef/json_compat.rb, line 58
def to_json_pretty(obj, opts = nil)
  opts ||= {}
  options_map = {}
  options_map[:pretty] = true
  options_map[:indent] = opts[:indent] if opts.has_key?(:indent)
  to_json(obj, options_map).chomp
end