class Chef::JSONCompat
Public Class Methods
from_json(source, opts = {})
click to toggle source
# File lib/chef/json_compat.rb, line 36 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.is_a?(Hash) || obj.is_a?(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
# File lib/chef/json_compat.rb, line 30 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 49 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 55 def to_json_pretty(obj, opts = nil) options_map = { pretty: true } options_map[:indent] = opts[:indent] if opts.respond_to?(:key?) && opts.key?(:indent) to_json(obj, options_map).chomp end