diff options
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | VERSION | 2 | ||||
-rw-r--r-- | ext/json/ext/generator/generator.c | 21 | ||||
-rw-r--r-- | lib/json/pure/generator.rb | 5 | ||||
-rw-r--r-- | lib/json/version.rb | 2 | ||||
-rwxr-xr-x | tests/test_json_generate.rb | 6 |
6 files changed, 34 insertions, 5 deletions
@@ -1,3 +1,6 @@ +2010-03-11 (1.2.3) + * Added a State#[] method which returns an attribute's value in order to + increase duck type compatibility to Hash. 2010-02-27 (1.2.2) * Made some changes to make the building of the parser/generator compatible to Rubinius. @@ -1 +1 @@ -1.2.2 +1.2.3 diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 40daf09..c907c4b 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -41,7 +41,8 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject, static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before, i_object_nl, i_array_nl, i_check_circular, i_max_nesting, - i_allow_nan, i_pack, i_unpack, i_create_id, i_extend, i_key_p; + i_allow_nan, i_pack, i_unpack, i_create_id, i_extend, i_key_p, + i_aref, i_send, i_respond_to_p; typedef struct JSON_Generator_StateStruct { VALUE indent; @@ -584,6 +585,20 @@ static VALUE cState_to_h(VALUE self) return result; } +/* + * call-seq: [](name) + * + * Return the value returned by method +name+. + */ +static VALUE cState_aref(VALUE self, VALUE name) +{ + GET_STATE(self); + if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) { + return rb_funcall(self, i_send, 1, name); + } else { + return Qnil; + } +} /* * call-seq: new(opts = {}) @@ -884,6 +899,7 @@ void Init_generator() rb_define_method(cState, "forget", cState_forget, 1); rb_define_method(cState, "configure", cState_configure, 1); rb_define_method(cState, "to_h", cState_to_h, 0); + rb_define_method(cState, "[]", cState_aref, 1); mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods"); mObject = rb_define_module_under(mGeneratorMethods, "Object"); @@ -926,6 +942,9 @@ void Init_generator() i_create_id = rb_intern("create_id"); i_extend = rb_intern("extend"); i_key_p = rb_intern("key?"); + i_aref = rb_intern("[]"); + i_send = rb_intern("__send__"); + i_respond_to_p = rb_intern("respond_to?"); #ifdef HAVE_RUBY_ENCODING_H mEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); i_encoding = rb_intern("encoding"); diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb index 57ef483..be73cb1 100644 --- a/lib/json/pure/generator.rb +++ b/lib/json/pure/generator.rb @@ -217,6 +217,11 @@ module JSON end result end + + # Return the value returned by method +name+. + def [](name) + __send__ name + end end module GeneratorMethods diff --git a/lib/json/version.rb b/lib/json/version.rb index 6d449a6..571612e 100644 --- a/lib/json/version.rb +++ b/lib/json/version.rb @@ -1,6 +1,6 @@ module JSON # JSON version - VERSION = '1.2.2' + VERSION = '1.2.3' VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc: VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc: VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb index e725e6f..e204bf2 100755 --- a/tests/test_json_generate.rb +++ b/tests/test_json_generate.rb @@ -88,13 +88,15 @@ EOT json = generate({1=>2}, nil) assert_equal('{"1":2}', json) s = JSON.state.new(:check_circular => true) - #assert s.check_circular + assert s.check_circular? + assert s[:check_circular?] h = { 1=>2 } h[3] = h assert_raises(JSON::CircularDatastructure) { generate(h) } assert_raises(JSON::CircularDatastructure) { generate(h, s) } s = JSON.state.new(:check_circular => true) - #assert s.check_circular + assert s.check_circular? + assert s[:check_circular?] a = [ 1, 2 ] a << a assert_raises(JSON::CircularDatastructure) { generate(a, s) } |