Class: Chef::JSONCompat

Inherits:
Object show all
Defined in:
lib/chef/json_compat.rb

Constant Summary collapse

JSON_MAX_NESTING =
1000

Class Method Summary collapse

Class Method Details

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

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



40
41
42
# File 'lib/chef/json_compat.rb', line 40

def from_json(source, opts = {})
  ::JSON.parse(source, opts_add_max_nesting(opts))
end

.opts_add_max_nesting(opts) ⇒ Object

See CHEF-1292/PL-538. Increase the max nesting for JSON, which defaults to 19, and isn’t enough for some (for example, a Node within a Node) structures.



31
32
33
34
35
36
37
# File 'lib/chef/json_compat.rb', line 31

def opts_add_max_nesting(opts)
  if opts.nil? || !opts.has_key?(:max_nesting)
    opts = opts.nil? ? Hash.new : opts.clone
    opts[:max_nesting] = JSON_MAX_NESTING
  end
  opts
end

.to_json(obj, opts = nil) ⇒ Object



44
45
46
# File 'lib/chef/json_compat.rb', line 44

def to_json(obj, opts = nil)
  obj.to_json(opts_add_max_nesting(opts))
end

.to_json_pretty(obj, opts = nil) ⇒ Object



48
49
50
# File 'lib/chef/json_compat.rb', line 48

def to_json_pretty(obj, opts = nil)
  ::JSON.pretty_generate(obj, opts_add_max_nesting(opts))
end