diff options
author | Florian Frank <flori@ping.de> | 2010-05-05 23:21:37 +0200 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2010-05-05 23:21:37 +0200 |
commit | 82ed5b724fb36dcba884cdb2251d953580c9de56 (patch) | |
tree | be514006bff82b336da6d4df3152170ef0420006 /tests/test_json.rb | |
parent | d496f792bf98dc49512d8f04f5867d26f6b8aed3 (diff) | |
download | json-82ed5b724fb36dcba884cdb2251d953580c9de56.tar.gz |
Fix for subclassed core classes, github issue 20v1.4.3
Fixed a problem in the 1.4.x versions, that caused subclasses of core
classes to miss calling their to_json methods.
Diffstat (limited to 'tests/test_json.rb')
-rwxr-xr-x | tests/test_json.rb | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/tests/test_json.rb b/tests/test_json.rb index f5a432f..19b742f 100755 --- a/tests/test_json.rb +++ b/tests/test_json.rb @@ -173,7 +173,18 @@ class TC_JSON < Test::Unit::TestCase assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } ')) end - class SubHash < Hash; end + class SubHash < Hash + def to_json(*a) + { + JSON.create_id => self.class.name, + }.merge(self).to_json(*a) + end + + def self.json_create(o) + o.delete JSON.create_id + new.merge(o) + end + end def test_parse_object_custom_class res = parse('{}', :object_class => SubHash) @@ -181,6 +192,16 @@ class TC_JSON < Test::Unit::TestCase assert_equal(SubHash, res.class) end + def test_generation_of_core_subclasses + obj = SubHash.new.merge( "foo" => SubHash.new.merge("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 obj_again['foo']['bar'] + assert_equal obj, obj_again + end + def test_parser_reset parser = Parser.new(@json) assert_equal(@hash, parser.parse) |