Skip to content

Commit 644d1c5

Browse files
committed
MultiJson::Engines::OkJson.stringify_keys supports Array
1 parent e797309 commit 644d1c5

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

lib/multi_json/engines/ok_json.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ def self.symbolize_keys(object) #:nodoc:
2525
end
2626

2727
def self.stringify_keys(object) #:nodoc:
28-
return object unless object.is_a?(Hash)
29-
object.inject({}) do |result, (key, value)|
30-
new_key = key.is_a?(Symbol) ? key.to_s : key
31-
new_value = value.is_a?(Hash) ? stringify_keys(value) : value
32-
result.merge! new_key => new_value
28+
case object
29+
when Array
30+
object.map do |value|
31+
stringify_keys(value)
32+
end
33+
when Hash
34+
object.inject({}) do |result, (key, value)|
35+
new_key = key.is_a?(Symbol) ? key.to_s : key
36+
new_value = stringify_keys(value)
37+
result.merge! new_key => new_value
38+
end
39+
else
40+
object
3341
end
3442
end
3543
end

spec/multi_json_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,23 @@ def self.encode(string)
6464
end
6565

6666
it 'encodes symbol keys as strings' do
67-
encoded_json = MultiJson.encode({ :foo => { :bar => 'baz' } })
68-
MultiJson.decode(encoded_json).should == { 'foo' => { 'bar' => 'baz' } }
67+
[
68+
[
69+
{ :foo => { :bar => 'baz' } },
70+
{ 'foo' => { 'bar' => 'baz' } }
71+
],
72+
[
73+
[ { :foo => { :bar => 'baz' } } ],
74+
[ { 'foo' => { 'bar' => 'baz' } } ],
75+
],
76+
[
77+
{ :foo => [ { :bar => 'baz' } ] },
78+
{ 'foo' => [ { 'bar' => 'baz' } ] },
79+
]
80+
].each do |example, expected|
81+
encoded_json = MultiJson.encode(example)
82+
MultiJson.decode(encoded_json).should == expected
83+
end
6984
end
7085

7186
it 'encodes rootless JSON' do

0 commit comments

Comments
 (0)