Permalink
Browse files

When Array#as_json and Hash#as_json are called without options, they

should also call #as_json on the children without options (instead of
nil)
  • Loading branch information...
1 parent 34c08d2 commit 663c059d4a043e0baabb69e60436bb849eb620fb @chancancode chancancode committed Nov 22, 2013
@@ -164,7 +164,7 @@ def as_json(options = nil) #:nodoc:
class Array
def as_json(options = nil) #:nodoc:
- map { |v| v.as_json(options && options.dup) }
+ map { |v| options ? v.as_json(options.dup) : v.as_json }
end
def encode_json(encoder) #:nodoc:
@@ -187,7 +187,7 @@ def as_json(options = nil) #:nodoc:
self
end
- Hash[subset.map { |k, v| [k.to_s, v.as_json(options && options.dup)] }]
+ Hash[subset.map { |k, v| [k.to_s, options ? v.as_json(options.dup) : v.as_json] }]
end
def encode_json(encoder) #:nodoc:
@@ -32,6 +32,12 @@ def as_json(options={})
end
end
+ class OptionsTest
+ def as_json(options = :default)
+ options
+ end
+ end
+
class HashWithAsJson < Hash
attr_accessor :as_json_called
@@ -332,6 +338,16 @@ def test_to_json_should_not_keep_options_around
"other_hash" => {"foo"=>"other_foo","test"=>"other_test"}}, ActiveSupport::JSON.decode(hash.to_json))
end
+ def test_hash_as_json_without_options
+ json = { foo: OptionsTest.new }.as_json
+ assert_equal({"foo" => :default}, json)
+ end
+
+ def test_array_as_json_without_options
+ json = [ OptionsTest.new ].as_json
+ assert_equal([:default], json)
+ end
+
def test_struct_encoding
Struct.new('UserNameAndEmail', :name, :email)
Struct.new('UserNameAndDate', :name, :date)

0 comments on commit 663c059

Please sign in to comment.