diff options
author | Florian Frank <flori@ping.de> | 2010-07-24 17:23:05 +0200 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2010-08-03 04:51:33 +0200 |
commit | f0a14faf1d59a5ea86a79d3a5780e8433bfc9e16 (patch) | |
tree | a80939d27e13f30b95281c51c27c0750f1c5f13a | |
parent | 9f156be0fe40f57e4a5727904d5c2b761fa2cef2 (diff) | |
download | json-f0a14faf1d59a5ea86a79d3a5780e8433bfc9e16.tar.gz |
use method dispatch for generation again
-rw-r--r-- | ext/json/ext/generator/generator.c | 2 | ||||
-rwxr-xr-x | tests/test_json.rb | 36 |
2 files changed, 30 insertions, 8 deletions
diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 7658acd..3786b65 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -453,7 +453,7 @@ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self) rb_raise(eNestingError, "nesting of %ld is too deep", depth); } fbuffer_append_char(buffer, '{'); - keys = rb_funcall(self, rb_intern("keys"), 0); + keys = rb_funcall(self, i_keys, 0); for(i = 0; i < RARRAY_LEN(keys); i++) { if (i > 0) fbuffer_append(buffer, delim, delim_len); if (object_nl) { diff --git a/tests/test_json.rb b/tests/test_json.rb index 19b742f..a88049a 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) @@ -173,7 +187,9 @@ class TC_JSON < Test::Unit::TestCase assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } ')) end - class SubHash < Hash + class SubHash < Hash; end + + class SubHash2 < Hash def to_json(*a) { JSON.create_id => self.class.name, @@ -187,19 +203,25 @@ class TC_JSON < Test::Unit::TestCase 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 - obj = SubHash.new.merge( "foo" => SubHash.new.merge("bar" => true)) + def test_generation_of_core_subclasses_with_new_to_json + obj = SubHash2.new.merge( "foo" => SubHash2.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_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_parser_reset |