diff options
author | Jakub Suder <jakub.suder@gmail.com> | 2010-08-29 16:10:31 +0200 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-09-07 11:33:10 -0700 |
commit | 2524cf404ce943eca8a5f2d173188fd0cf2ac8b9 (patch) | |
tree | 0d589e9e06623d0bda440b78ccd93a426c2e4807 /activesupport/test/json | |
parent | 2e8a3d0f43d7b394873ce21396ae49feacb99a74 (diff) | |
download | rails-2524cf404ce943eca8a5f2d173188fd0cf2ac8b9.tar.gz rails-2524cf404ce943eca8a5f2d173188fd0cf2ac8b9.tar.bz2 rails-2524cf404ce943eca8a5f2d173188fd0cf2ac8b9.zip |
fixed some issues with JSON encoding
- as_json in ActiveModel should return a hash
and handle :only/:except/:methods options
- Array and Hash should call as_json on their elements
- json methods should not modify options argument
[#5374 state:committed]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activesupport/test/json')
-rw-r--r-- | activesupport/test/json/encoding_test.rb | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 1527d02d16..e0494de6e4 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -108,12 +108,24 @@ class TestJSONEncoding < Test::Unit::TestCase end end - def test_exception_raised_when_encoding_circular_reference + def test_exception_raised_when_encoding_circular_reference_in_array a = [1] a << a assert_raise(ActiveSupport::JSON::Encoding::CircularReferenceError) { ActiveSupport::JSON.encode(a) } end + def test_exception_raised_when_encoding_circular_reference_in_hash + a = { :name => 'foo' } + a[:next] = a + assert_raise(ActiveSupport::JSON::Encoding::CircularReferenceError) { ActiveSupport::JSON.encode(a) } + end + + def test_exception_raised_when_encoding_circular_reference_in_hash_inside_array + a = { :name => 'foo', :sub => [] } + a[:sub] << a + assert_raise(ActiveSupport::JSON::Encoding::CircularReferenceError) { ActiveSupport::JSON.encode(a) } + end + def test_hash_key_identifiers_are_always_quoted values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"} assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values)) @@ -152,6 +164,57 @@ class TestJSONEncoding < Test::Unit::TestCase end end + def test_hash_should_pass_encoding_options_to_children_in_as_json + person = { + :name => 'John', + :address => { + :city => 'London', + :country => 'UK' + } + } + json = person.as_json :only => [:address, :city] + + assert_equal({ 'address' => { 'city' => 'London' }}, json) + end + + def test_hash_should_pass_encoding_options_to_children_in_to_json + person = { + :name => 'John', + :address => { + :city => 'London', + :country => 'UK' + } + } + json = person.to_json :only => [:address, :city] + + assert_equal(%({"address":{"city":"London"}}), json) + end + + def test_array_should_pass_encoding_options_to_children_in_as_json + people = [ + { :name => 'John', :address => { :city => 'London', :country => 'UK' }}, + { :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }} + ] + json = people.as_json :only => [:address, :city] + expected = [ + { 'address' => { 'city' => 'London' }}, + { 'address' => { 'city' => 'Paris' }} + ] + + assert_equal(expected, json) + end + + def test_array_should_pass_encoding_options_to_children_in_to_json + people = [ + { :name => 'John', :address => { :city => 'London', :country => 'UK' }}, + { :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }} + ] + json = people.to_json :only => [:address, :city] + + assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json) + end + + protected def object_keys(json_object) |