diff options
author | Florian Frank <flori@ping.de> | 2010-08-05 03:45:04 +0200 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2010-08-05 09:46:43 +0200 |
commit | 6c5ec83475acf01df831858b20c9ccb512d5a8a5 (patch) | |
tree | 125656677fe5fe92647609ab9bac2457e7a78b90 /tests | |
parent | 646cc01a6677905fe0ef7fb35aff7bca4fd68123 (diff) | |
download | json-6c5ec83475acf01df831858b20c9ccb512d5a8a5.tar.gz |
Revert "use method dispatch for generation again"
This reverts commit 59eab2b7ad1b44dc424fb3fffa5520947f7360f3.
Conflicts:
ext/json/ext/generator/generator.c
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test_json.rb | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/tests/test_json.rb b/tests/test_json.rb index 19b742f..00e52f5 100755 --- a/tests/test_json.rb +++ b/tests/test_json.rb @@ -160,6 +160,20 @@ class TC_JSON < Test::Unit::TestCase class SubArray < Array; end + class SubArray2 < Array + def to_json(*a) + { + JSON.create_id => self.class.name, + 'ary' => to_a, + }.to_json(*a) + end + + def self.json_create(o) + o.delete JSON.create_id + o['ary'] + end + end + def test_parse_array_custom_class res = parse('[]', :array_class => SubArray) assert_equal([], res) @@ -174,6 +188,9 @@ class TC_JSON < Test::Unit::TestCase end class SubHash < Hash + end + + class SubHash2 < Hash def to_json(*a) { JSON.create_id => self.class.name, @@ -182,22 +199,38 @@ class TC_JSON < Test::Unit::TestCase def self.json_create(o) o.delete JSON.create_id - new.merge(o) + self[o] end end def test_parse_object_custom_class - res = parse('{}', :object_class => SubHash) + res = parse('{}', :object_class => SubHash2) assert_equal({}, res) - assert_equal(SubHash, res.class) + assert_equal(SubHash2, res.class) + end + + def test_generation_of_core_subclasses_with_new_to_json + obj = SubHash2["foo" => SubHash2["bar" => true]] + obj_json = JSON(obj) + obj_again = JSON(obj_json) + assert_kind_of SubHash2, obj_again + assert_kind_of SubHash2, obj_again['foo'] + assert obj_again['foo']['bar'] + assert_equal obj, obj_again + assert_equal ["foo"], JSON(JSON(SubArray2["foo"])) + end + + def test_generation_of_core_subclasses_with_default_to_json + assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"]) + assert_equal '["foo"]', JSON(SubArray["foo"]) end def test_generation_of_core_subclasses - obj = SubHash.new.merge( "foo" => SubHash.new.merge("bar" => true)) + obj = SubHash["foo" => SubHash["bar" => true]] obj_json = JSON(obj) obj_again = JSON(obj_json) - assert_kind_of SubHash, obj_again - assert_kind_of SubHash, obj_again['foo'] + assert_kind_of Hash, obj_again + assert_kind_of Hash, obj_again['foo'] assert obj_again['foo']['bar'] assert_equal obj, obj_again end |