Skip to content

Commit

Permalink
added [] method to State objects
Browse files Browse the repository at this point in the history
  • Loading branch information
flori committed Mar 11, 2010
1 parent 98a6817 commit 3db5070
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.2
1.2.3
21 changes: 20 additions & 1 deletion ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {})
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 5 additions & 0 deletions lib/json/pure/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ def to_h
end
result
end

# Return the value returned by method +name+.
def [](name)
__send__ name
end
end

module GeneratorMethods
Expand Down
2 changes: 1 addition & 1 deletion lib/json/version.rb
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
6 changes: 4 additions & 2 deletions tests/test_json_generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ def test_states
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) }
Expand Down

0 comments on commit 3db5070

Please sign in to comment.