Skip to content

Commit 14e0300

Browse files
x4d3gkellogg
authored andcommitted
Support for symbols
1 parent dd8c50b commit 14e0300

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/json/canonicalization.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,21 @@ def to_json_c14n
6161
end
6262
end
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+
6472
class 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
8391
end
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+

spec/c14n_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@
99
end
1010
end
1111
end
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

0 commit comments

Comments
 (0)