Module: Puppet::Util::Json
- Defined in:
- lib/puppet/util/json.rb
Defined Under Namespace
Classes: ParseError
Class Method Summary collapse
- .dump(object, options = {}) ⇒ Object
-
.load(string, options = {}) ⇒ Object
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.
Class Method Details
.dump(object, options = {}) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/puppet/util/json.rb', line 60 def self.dump(object, = {}) if defined? MultiJson MultiJson.dump(object, ) else .merge!(::JSON::PRETTY_STATE_PROTOTYPE.to_h) if .delete(:pretty) object.to_json() end end |
.load(string, options = {}) ⇒ Object
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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/puppet/util/json.rb', line 32 def self.load(string, = {}) if defined? MultiJson begin # This ensures that JrJackson will parse very large or very small # numbers as floats rather than BigDecimals, which are serialized as # strings by the built-in JSON gem and therefore can cause schema errors, # for example, when we are rendering reports to JSON using `to_pson` in # PuppetDB. if MultiJson.adapter.name == "MultiJson::Adapters::JrJackson" [:use_bigdecimal] = false end MultiJson.load(string, ) rescue MultiJson::ParseError => e raise Puppet::Util::Json::ParseError.build(e, string) end else begin string = string.read if string.respond_to?(:read) [:symbolize_names] = true if .delete(:symbolize_keys) ::JSON.parse(string, ) rescue JSON::ParserError => e raise Puppet::Util::Json::ParseError.build(e, string) end end end |