diff options
author | Florian Frank <flori@ping.de> | 2011-12-20 20:14:10 +0100 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2011-12-20 20:45:15 +0100 |
commit | eacb6564c05ff7428bcbb3c34f72655b1b5d6ca3 (patch) | |
tree | 3edffa71fa198ddafa8c91771131ca537fae0ed3 /tests | |
parent | 3c7c29215d18d25fff3d3af2d599f90894c52c72 (diff) | |
download | json-eacb6564c05ff7428bcbb3c34f72655b1b5d6ca3.tar.gz |
Support object class with duck type hash
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test_json.rb | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/tests/test_json.rb b/tests/test_json.rb index c308933..fe21475 100755 --- a/tests/test_json.rb +++ b/tests/test_json.rb @@ -5,6 +5,7 @@ require 'test/unit' require File.join(File.dirname(__FILE__), 'setup_variant') require 'stringio' require 'tempfile' +require 'ostruct' unless Array.method_defined?(:permutation) begin @@ -256,13 +257,35 @@ class TC_JSON < Test::Unit::TestCase end end - def test_parse_object_custom_class + class SubOpenStruct < OpenStruct + def [](k) + __send__(k) + end + + def []=(k, v) + @item_set = true + __send__("#{k}=", v) + end + + def item_set? + @item_set + end + end + + def test_parse_object_custom_hash_derived_class res = parse('{"foo":"bar"}', :object_class => SubHash) assert_equal({"foo" => "bar"}, res) assert_equal(SubHash, res.class) assert res.item_set? end + def test_parse_object_custom_non_hash_derived_class + res = parse('{"foo":"bar"}', :object_class => SubOpenStruct) + assert_equal "bar", res.foo + assert_equal(SubOpenStruct, res.class) + assert res.item_set? + end + def test_generation_of_core_subclasses_with_new_to_json obj = SubHash2["foo" => SubHash2["bar" => true]] obj_json = JSON(obj) |