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



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

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.



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

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



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

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

.to_json_pretty(obj, opts = nil) ⇒ Object



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

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