Skip to content

Commit ba5e3f6

Browse files
committed
Nicely ordered
1 parent c3a5ba0 commit ba5e3f6

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

json2yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ require "bundler/setup"
2626
require 'json'
2727
require 'yaml'
2828

29+
class Hash
30+
# Replacing the to_yaml function so it'll serialize hashes sorted (by their keys)
31+
#
32+
# Original function is in /usr/lib/ruby/1.8/yaml/rubytypes.rb
33+
#
34+
# Credit goes to Dov Murik http://snippets.dzone.com/user/dubek
35+
#
36+
def to_yaml( opts = {} )
37+
YAML::quick_emit( object_id, opts ) do |out|
38+
out.map( taguri, to_yaml_style ) do |map|
39+
sort.each do |k, v| # <-- here's my addition (the 'sort')
40+
map.add( k, v )
41+
end
42+
end
43+
end
44+
end
45+
end
46+
2947
raise Exception, 'you must provide a json files' unless ARGV[0]
3048

3149
my_hash = JSON.parse(File.open(ARGV[0]).read)

0 commit comments

Comments
 (0)