summaryrefslogtreecommitdiff
path: root/tests/test_json.rb
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2010-05-05 23:21:37 +0200
committerFlorian Frank <flori@ping.de>2010-05-05 23:21:37 +0200
commit82ed5b724fb36dcba884cdb2251d953580c9de56 (patch)
treebe514006bff82b336da6d4df3152170ef0420006 /tests/test_json.rb
parentd496f792bf98dc49512d8f04f5867d26f6b8aed3 (diff)
downloadjson-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-xtests/test_json.rb23
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)