Module: KnifeCloudformation::Utils::JSON
- Defined in:
- lib/knife-cloudformation/utils/json.rb
Overview
JSON helper methods
Instance Method Summary collapse
-
#_format_json(thing) ⇒ String
Format object into pretty JSON.
-
#_from_json(thing) ⇒ Object
Load JSON data.
-
#_to_json(thing) ⇒ String
Convert to JSON.
-
#try_json_compat ⇒ TrueClass, FalseClass
Attempt to load chef JSON compat helper.
Instance Method Details
#_format_json(thing) ⇒ String
Format object into pretty JSON
52 53 54 55 56 57 58 59 |
# File 'lib/knife-cloudformation/utils/json.rb', line 52 def _format_json(thing) thing = _from_json(thing) if thing.is_a?(String) if(try_json_compat) Chef::JSONCompat.to_json_pretty(thing) else JSON.pretty_generate(thing) end end |
#_from_json(thing) ⇒ Object
Load JSON data
40 41 42 43 44 45 46 |
# File 'lib/knife-cloudformation/utils/json.rb', line 40 def _from_json(thing) if(try_json_compat) Chef::JSONCompat.from_json(thing) else JSON.read(thing) end end |
#_to_json(thing) ⇒ String
Convert to JSON
28 29 30 31 32 33 34 |
# File 'lib/knife-cloudformation/utils/json.rb', line 28 def _to_json(thing) if(try_json_compat) Chef::JSONCompat.to_json(thing) else JSON.dump(thing) end end |
#try_json_compat ⇒ TrueClass, FalseClass
Attempt to load chef JSON compat helper
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/knife-cloudformation/utils/json.rb', line 12 def try_json_compat unless(@_json_loaded) begin require 'chef/json_compat' rescue require "#{ENV['RUBY_JSON_LIB'] || 'json'}" end @_json_loaded = true end defined?(Chef::JSONCompat) end |