module Puppet::Util::Json

Public Class Methods

dump(object, options = {}) click to toggle source
   # File lib/puppet/util/json.rb
60 def self.dump(object, options = {})
61   if defined? MultiJson
62     MultiJson.dump(object, options)
63   else
64     options.merge!(::JSON::PRETTY_STATE_PROTOTYPE.to_h) if options.delete(:pretty)
65     object.to_json(options)
66   end
67 end
load(string, options = {}) click to toggle source

These methods do similar processing to the fallback implemented by MultiJson when using the built-in JSON backend, to ensure consistent behavior whether or not MultiJson can be loaded.

   # File lib/puppet/util/json.rb
32 def self.load(string, options = {})
33   if defined? MultiJson
34     begin
35       # This ensures that JrJackson will parse very large or very small
36       # numbers as floats rather than BigDecimals, which are serialized as
37       # strings by the built-in JSON gem and therefore can cause schema errors,
38       # for example, when we are rendering reports to JSON using `to_pson` in
39       # PuppetDB.
40       if MultiJson.adapter.name == "MultiJson::Adapters::JrJackson"
41         options[:use_bigdecimal] = false
42       end
43 
44       MultiJson.load(string, options)
45     rescue MultiJson::ParseError => e
46       raise Puppet::Util::Json::ParseError.build(e, string)
47     end
48   else
49     begin
50       string = string.read if string.respond_to?(:read)
51 
52       options[:symbolize_names] = true if options.delete(:symbolize_keys)
53       ::JSON.parse(string, options)
54     rescue JSON::ParserError => e
55       raise Puppet::Util::Json::ParseError.build(e, string)
56     end
57   end
58 end