Skip to content

Commit

Permalink
Merge pull request #156 from solarkennedy/AJ-json-convert-quoted-int
Browse files Browse the repository at this point in the history
convert quoted integers to int object
  • Loading branch information
solarkennedy committed Jul 17, 2015
2 parents 297bab6 + be8f59b commit f704d92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/puppet/parser/functions/consul_sorted_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

def sorted_json(obj)
case obj
when String, Fixnum, Float, TrueClass, FalseClass, NilClass
when Fixnum, 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|
Expand Down
7 changes: 7 additions & 0 deletions spec/functions/consul_sorted_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
describe 'consul_sorted_json' do
it { should run.with_params({'foo' => :undef}).and_return("{}") }
it { should run.with_params({'b' => 1, 'a' => 2, 'c' => 3}).and_return('{"a":2,"b":1,"c":3}')}
it { should run.with_params({
'w' => -1,
'x' => '8500',
'y' => '-8656',
'z' => 'foo bar 123 4 5 6',
}).and_return('{"w":-1,"x":8500,"y":-8656,"z":"foo bar 123 4 5 6"}')
}
end

0 comments on commit f704d92

Please sign in to comment.