File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -61,13 +61,21 @@ def to_json_c14n
6161 end
6262end
6363
64+ def encode_key ( k )
65+ case k
66+ when String
67+ return k . encode ( Encoding ::UTF_16 )
68+ end
69+ k
70+ end
71+
6472class Hash
6573 # Output JSON with keys sorted lexicographically
6674 # @return [String]
6775 def to_json_c14n
6876 "{" + self .
6977 keys .
70- sort_by { |k | k . encode ( Encoding :: UTF_16 ) } .
78+ sort_by { |k | encode_key ( k ) } .
7179 map { |k | k . to_json_c14n + ':' + self [ k ] . to_json_c14n }
7280 . join ( ',' ) +
7381 '}'
@@ -81,3 +89,12 @@ def to_json_c14n
8189 self . to_json
8290 end
8391end
92+
93+ class Symbol
94+ # Output JSON with control characters escaped
95+ # @return [String]
96+ def to_json_c14n
97+ self . to_json
98+ end
99+ end
100+
Original file line number Diff line number Diff line change 99 end
1010 end
1111end
12+
13+ describe "special cases for hash keys" do
14+ it "handles hash defined with symbols" do
15+ data = { a : [ { b :"b" } ] }
16+ expect ( data . to_json_c14n ) . to eq "{\" a\" :[{\" b\" :\" b\" }]}"
17+ end
18+ end
You can’t perform that action at this time.
0 commit comments