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.
-
.load_file(filename, options = {}) ⇒ Object
Load the content from a file as JSON.
-
.load_file_if_valid(filename, options = {}) ⇒ Object
Load the content from a file as JSON if contents are in valid format.
Class Method Details
.dump(object, options = {}) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/puppet/util/json.rb', line 50 def self.dump(object, = {}) # Options is a state when we're being called recursively if !.is_a?(JSON::State) && .delete(:pretty) .merge!(::JSON::PRETTY_STATE_PROTOTYPE.to_h) end object.to_json() 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.
41 42 43 44 45 46 47 48 |
# File 'lib/puppet/util/json.rb', line 41 def self.load(string, = {}) 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 |
.load_file(filename, options = {}) ⇒ Object
Load the content from a file as JSON.
33 34 35 36 |
# File 'lib/puppet/util/json.rb', line 33 def self.load_file(filename, = {}) json = Puppet::FileSystem.read(filename, :encoding => 'utf-8') load(json, ) end |
.load_file_if_valid(filename, options = {}) ⇒ Object
Load the content from a file as JSON if contents are in valid format. This method does not raise error but returns ‘nil` when invalid file is given.
25 26 27 28 29 30 |
# File 'lib/puppet/util/json.rb', line 25 def self.load_file_if_valid(filename, = {}) load_file(filename, ) rescue Puppet::Util::Json::ParseError, ArgumentError, Errno::ENOENT => detail Puppet.debug("Could not retrieve JSON content from '#{filename}': #{detail.}") nil end |