Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MultiJson::Engines::OkJson.symbolize_keys supports Array

  • Loading branch information...
commit c885377d47a2aa39cb0d971fea78db2d2fa479a7 1 parent 644d1c5
@ishikawa authored
Showing with 29 additions and 6 deletions.
  1. +13 −5 lib/multi_json/engines/ok_json.rb
  2. +16 −1 spec/multi_json_spec.rb
View
18 lib/multi_json/engines/ok_json.rb
@@ -16,11 +16,19 @@ def self.encode(object) #:nodoc:
end
def self.symbolize_keys(object) #:nodoc:
- return object unless object.is_a?(Hash)
- object.inject({}) do |result, (key, value)|
- new_key = key.is_a?(String) ? key.to_sym : key
- new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
- result.merge! new_key => new_value
+ case object
+ when Array
+ object.map do |value|
+ symbolize_keys(value)
+ end
+ when Hash
+ object.inject({}) do |result, (key, value)|
+ new_key = key.is_a?(String) ? key.to_sym : key
+ new_value = symbolize_keys(value)
+ result.merge! new_key => new_value
+ end
+ else
+ object
end
end
View
17 spec/multi_json_spec.rb
@@ -111,7 +111,22 @@ def self.encode(string)
end
it 'allows for symbolization of keys' do
- MultiJson.decode('{"abc":{"def":"hgi"}}', :symbolize_keys => true).should == { :abc => { :def => 'hgi' } }
+ [
+ [
+ '{"abc":{"def":"hgi"}}',
+ { :abc => { :def => 'hgi' } }
+ ],
+ [
+ '[{"abc":{"def":"hgi"}}]',
+ [ { :abc => { :def => 'hgi' } } ]
+ ],
+ [
+ '{"abc":[{"def":"hgi"}]}',
+ { :abc => [ { :def => 'hgi' } ] }
+ ],
+ ].each do |example, expected|
+ MultiJson.decode(example, :symbolize_keys => true).should == expected
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.