Module: JSON

Defined in:
lib/puppet/functions/sensu_sorted_json.rb

Constant Summary collapse

@@loop =
0

Class Method Summary collapse

Class Method Details

.sorted_generate(obj) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/functions/sensu_sorted_json.rb', line 43

def sorted_generate(obj)
  case obj
    when Integer, Float, TrueClass, FalseClass, NilClass
      return obj.to_json
    when String
      # Convert quoted integers (string) to int
      return (obj.match(/\A[-]?[0-9]+\z/) ? obj.to_i : obj).to_json
    when Array
      arrayRet = []
      obj.each do |a|
        arrayRet.push(sorted_generate(a))
      end
      return "[" << arrayRet.join(',') << "]";
    when Hash
      ret = []
      validate_keys(obj)
      obj.keys.sort.each do |k|
        ret.push(k.to_s.to_json << ":" << sorted_generate(obj[k]))
      end
      return "{" << ret.join(",") << "}";
    else
      raise(Puppet::ParseError, "Unable to handle object of type <%s>" % obj.class.to_s)
  end
end

.sorted_pretty_generate(obj, indent_len = 4) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/puppet/functions/sensu_sorted_json.rb', line 68

def sorted_pretty_generate(obj, indent_len=4)
  # Indent length
  indent = " " * indent_len

  case obj

    when Integer, Float, TrueClass, FalseClass, NilClass
      return obj.to_json

    when String
      # Convert quoted integers (string) to int
      return (obj.match(/\A[-]?[0-9]+\z/) ? obj.to_i : obj).to_json

    when Array
      arrayRet = []

      # We need to increase the loop count before #each so the objects inside are indented twice.
      # When we come out of #each we decrease the loop count so the closing brace lines up properly.
      #
      # If you start with @@loop = 1, the count will be as follows
      #
      # "start_join": [     <-- @@loop == 1
      #   "192.168.50.20",  <-- @@loop == 2
      #   "192.168.50.21",  <-- @@loop == 2
      #   "192.168.50.22"   <-- @@loop == 2
      # ] <-- closing brace <-- @@loop == 1
      #
      @@loop += 1
      obj.each do |a|
        arrayRet.push(sorted_pretty_generate(a, indent_len))
      end
      @@loop -= 1

      return "[\n#{indent * (@@loop + 1)}" << arrayRet.join(",\n#{indent * (@@loop + 1)}") << "\n#{indent * @@loop}]";

    when Hash
      ret = []
      validate_keys(obj)

      # This loop works in a similar way to the above
      @@loop += 1
      obj.keys.sort.each do |k|
        ret.push("#{indent * @@loop}" << k.to_json << ": " << sorted_pretty_generate(obj[k], indent_len))
      end
      @@loop -= 1

      return "{\n" << ret.join(",\n") << "\n#{indent * @@loop}}";
    else
      raise(Puppet::ParseError, "Unable to handle object of type <%s>" % obj.class.to_s)
  end
end

.validate_keys(obj) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet/functions/sensu_sorted_json.rb', line 31

def validate_keys(obj)
  Puppet.debug("hello")
  obj.keys.each do |k|
    case k
      when String
        Puppet.debug("Found a valid key: " << k)
      else
        raise(Puppet::ParseError, "Unable to use key of type <%s>" % k.class.to_s)
    end
  end
end