summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2010-03-11 10:08:05 +0100
committerFlorian Frank <flori@ping.de>2010-03-11 10:08:05 +0100
commit3db50701a4a71e49709c63483d2ba4b5a408373b (patch)
treedd91e8fc89adac5f40b4a83e4ac2f7161d7b3754
parent98a6817eec97852895208513311dc5d5af1c9c05 (diff)
downloadjson-1.2.3.tar.gz
added [] method to State objectsv1.2.3
-rw-r--r--CHANGES3
-rw-r--r--VERSION2
-rw-r--r--ext/json/ext/generator/generator.c21
-rw-r--r--lib/json/pure/generator.rb5
-rw-r--r--lib/json/version.rb2
-rwxr-xr-xtests/test_json_generate.rb6
6 files changed, 34 insertions, 5 deletions
diff --git a/CHANGES b/CHANGES
index 28715f7..1db68cc 100644
--- a/CHANGES
+++ b/CHANGES
@@ -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.
diff --git a/VERSION b/VERSION
index 23aa839..0495c4a 100644
--- a/VERSION
+++ b/VERSION
@@ -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) }