File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments