Puppet Function: sensu_sorted_json
- Defined in:
- lib/puppet/parser/functions/sensu_sorted_json.rb
- Function type:
- Ruby 3.x API
Overview
This function takes unsorted hash and outputs JSON object making sure the keys are sorted. Optionally you can pass a boolean as the second parameter, which controls if the output is pretty formatted.
Examples:
-------------------
-- UNSORTED HASH --
-------------------
unsorted_hash = {
'client_addr' => '127.0.0.1',
'bind_addr' => '192.168.34.56',
'start_join' => [
'192.168.34.60',
'192.168.34.61',
'192.168.34.62',
],
'ports' => {
'rpc' => 8567,
'https' => 8500,
'http' => -1,
},
}
-----------------
-- SORTED JSON --
-----------------
sorted_json(unsorted_hash)
{"bind_addr":"192.168.34.56","client_addr":"127.0.0.1",
"ports":{"http":-1,"https":8500,"rpc":8567},
"start_join":["192.168.34.60","192.168.34.61","192.168.34.62"]}
------------------------
-- PRETTY SORTED JSON --
------------------------
Params: data <hash>, pretty <true|false>.
sorted_json(unsorted_hash, true)
{
"bind_addr": "192.168.34.56",
"client_addr": "127.0.0.1",
"ports": {
"http": -1,
"https": 8500,
"rpc": 8567
},
"start_join": [
"192.168.34.60",
"192.168.34.61",
"192.168.34.62"
]
}
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/puppet/parser/functions/sensu_sorted_json.rb', line 123 newfunction(:sensu_sorted_json, :type => :rvalue, :doc => <<-EOS This function takes unsorted hash and outputs JSON object making sure the keys are sorted. Optionally you can pass a boolean as the second parameter, which controls if the output is pretty formatted. *Examples:* ------------------- -- UNSORTED HASH -- ------------------- unsorted_hash = { 'client_addr' => '127.0.0.1', 'bind_addr' => '192.168.34.56', 'start_join' => [ '192.168.34.60', '192.168.34.61', '192.168.34.62', ], 'ports' => { 'rpc' => 8567, 'https' => 8500, 'http' => -1, }, } ----------------- -- SORTED JSON -- ----------------- sorted_json(unsorted_hash) {"bind_addr":"192.168.34.56","client_addr":"127.0.0.1", "ports":{"http":-1,"https":8500,"rpc":8567}, "start_join":["192.168.34.60","192.168.34.61","192.168.34.62"]} ------------------------ -- PRETTY SORTED JSON -- ------------------------ Params: data <hash>, pretty <true|false>. sorted_json(unsorted_hash, true) { "bind_addr": "192.168.34.56", "client_addr": "127.0.0.1", "ports": { "http": -1, "https": 8500, "rpc": 8567 }, "start_join": [ "192.168.34.60", "192.168.34.61", "192.168.34.62" ] } EOS ) do |args| raise(Puppet::ParseError, "sensu_sorted_json(): Wrong number of arguments " + "given (#{args.size} for 1 or 2)") unless args.size.between?(1,2) unsorted_hash = args[0] || {} pretty = args[1] || false indent_len = 4 unsorted_hash.reject! {|key, value| value == :undef } if pretty return JSON.sorted_pretty_generate(unsorted_hash, indent_len) << "\n" else return JSON.sorted_generate(unsorted_hash) end end |