Module: KnifeCloudformation::Utils::JSON

Included in:
Chef::Knife::CloudformationImport, KnifeCloudformation::Utils, StackExporter
Defined in:
lib/knife-cloudformation/utils/json.rb

Overview

JSON helper methods

Instance Method Summary collapse

Instance Method Details

#_format_json(thing) ⇒ String

Format object into pretty JSON

Parameters:

  • thing (Object)

Returns:

  • (String)


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

Parameters:

  • thing (String)

Returns:

  • (Object)


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

Parameters:

  • thing (Object)

Returns:

  • (String)


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_compatTrueClass, FalseClass

Attempt to load chef JSON compat helper

Returns:

  • (TrueClass, FalseClass)

    chef compat helper available



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