Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support object class with duck type hash

  • Loading branch information...
commit eacb6564c05ff7428bcbb3c34f72655b1b5d6ca3 1 parent 3c7c292
@flori flori authored
View
6 ext/json/ext/generator/extconf.rb
@@ -5,9 +5,9 @@
end
if CONFIG['CC'] =~ /gcc/
$CFLAGS << ' -Wall'
- #unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
- # $CFLAGS << ' -O0 -ggdb'
- #end
+ unless $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
+ $CFLAGS << ' -O0 -ggdb'
+ end
end
create_makefile 'json/ext/generator'
View
6 ext/json/ext/parser/extconf.rb
@@ -5,9 +5,9 @@
end
if CONFIG['CC'] =~ /gcc/
$CFLAGS << ' -Wall'
- #unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
- # $CFLAGS << ' -O0 -ggdb'
- #end
+ if $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
+ $CFLAGS << ' -O0 -ggdb'
+ end
end
create_makefile 'json/ext/parser'
View
148 ext/json/ext/parser/parser.c
@@ -81,7 +81,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity;
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_quirks_mode,
i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match,
- i_match_string, i_aset, i_leftshift;
+ i_match_string, i_aset, i_aref, i_leftshift;
#line 110 "parser.rl"
@@ -445,7 +445,12 @@ case 26:
if (cs >= JSON_object_first_final) {
if (json->create_additions) {
- VALUE klassname = rb_hash_aref(*result, json->create_id);
+ VALUE klassname;
+ if (NIL_P(json->object_class)) {
+ klassname = rb_hash_aref(*result, json->create_id);
+ } else {
+ klassname = rb_funcall(*result, i_aref, 1, json->create_id);
+ }
if (!NIL_P(klassname)) {
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
@@ -461,7 +466,7 @@ case 26:
-#line 465 "parser.c"
+#line 470 "parser.c"
static const int JSON_value_start = 1;
static const int JSON_value_first_final = 21;
static const int JSON_value_error = 0;
@@ -469,7 +474,7 @@ static const int JSON_value_error = 0;
static const int JSON_value_en_main = 1;
-#line 266 "parser.rl"
+#line 271 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -477,14 +482,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 481 "parser.c"
+#line 486 "parser.c"
{
cs = JSON_value_start;
}
-#line 273 "parser.rl"
+#line 278 "parser.rl"
-#line 488 "parser.c"
+#line 493 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -509,14 +514,14 @@ case 1:
cs = 0;
goto _out;
tr0:
-#line 214 "parser.rl"
+#line 219 "parser.rl"
{
char *np = JSON_parse_string(json, p, pe, result);
if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
}
goto st21;
tr2:
-#line 219 "parser.rl"
+#line 224 "parser.rl"
{
char *np;
if(pe > p + 9 - json->quirks_mode && !strncmp(MinusInfinity, p, 9)) {
@@ -536,7 +541,7 @@ cs = 0;
}
goto st21;
tr5:
-#line 237 "parser.rl"
+#line 242 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -546,7 +551,7 @@ cs = 0;
}
goto st21;
tr9:
-#line 245 "parser.rl"
+#line 250 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -556,7 +561,7 @@ cs = 0;
}
goto st21;
tr16:
-#line 207 "parser.rl"
+#line 212 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
@@ -566,7 +571,7 @@ cs = 0;
}
goto st21;
tr18:
-#line 200 "parser.rl"
+#line 205 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
@@ -576,19 +581,19 @@ cs = 0;
}
goto st21;
tr22:
-#line 194 "parser.rl"
+#line 199 "parser.rl"
{
*result = Qfalse;
}
goto st21;
tr25:
-#line 191 "parser.rl"
+#line 196 "parser.rl"
{
*result = Qnil;
}
goto st21;
tr28:
-#line 197 "parser.rl"
+#line 202 "parser.rl"
{
*result = Qtrue;
}
@@ -597,9 +602,9 @@ cs = 0;
if ( ++p == pe )
goto _test_eof21;
case 21:
-#line 253 "parser.rl"
+#line 258 "parser.rl"
{ p--; {p++; cs = 21; goto _out;} }
-#line 603 "parser.c"
+#line 608 "parser.c"
goto st0;
st2:
if ( ++p == pe )
@@ -760,7 +765,7 @@ case 20:
_out: {}
}
-#line 274 "parser.rl"
+#line 279 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
@@ -770,7 +775,7 @@ case 20:
}
-#line 774 "parser.c"
+#line 779 "parser.c"
static const int JSON_integer_start = 1;
static const int JSON_integer_first_final = 3;
static const int JSON_integer_error = 0;
@@ -778,7 +783,7 @@ static const int JSON_integer_error = 0;
static const int JSON_integer_en_main = 1;
-#line 290 "parser.rl"
+#line 295 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -786,15 +791,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 790 "parser.c"
+#line 795 "parser.c"
{
cs = JSON_integer_start;
}
-#line 297 "parser.rl"
+#line 302 "parser.rl"
json->memo = p;
-#line 798 "parser.c"
+#line 803 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -828,14 +833,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 287 "parser.rl"
+#line 292 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} }
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
-#line 839 "parser.c"
+#line 844 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -854,7 +859,7 @@ case 5:
_out: {}
}
-#line 299 "parser.rl"
+#line 304 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -869,7 +874,7 @@ case 5:
}
-#line 873 "parser.c"
+#line 878 "parser.c"
static const int JSON_float_start = 1;
static const int JSON_float_first_final = 8;
static const int JSON_float_error = 0;
@@ -877,7 +882,7 @@ static const int JSON_float_error = 0;
static const int JSON_float_en_main = 1;
-#line 324 "parser.rl"
+#line 329 "parser.rl"
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -885,15 +890,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 889 "parser.c"
+#line 894 "parser.c"
{
cs = JSON_float_start;
}
-#line 331 "parser.rl"
+#line 336 "parser.rl"
json->memo = p;
-#line 897 "parser.c"
+#line 902 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -951,14 +956,14 @@ case 8:
goto st0;
goto tr9;
tr9:
-#line 318 "parser.rl"
+#line 323 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 962 "parser.c"
+#line 967 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -1019,7 +1024,7 @@ case 7:
_out: {}
}
-#line 333 "parser.rl"
+#line 338 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
@@ -1035,7 +1040,7 @@ case 7:
-#line 1039 "parser.c"
+#line 1044 "parser.c"
static const int JSON_array_start = 1;
static const int JSON_array_first_final = 17;
static const int JSON_array_error = 0;
@@ -1043,7 +1048,7 @@ static const int JSON_array_error = 0;
static const int JSON_array_en_main = 1;
-#line 376 "parser.rl"
+#line 381 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -1057,14 +1062,14 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul
*result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
-#line 1061 "parser.c"
+#line 1066 "parser.c"
{
cs = JSON_array_start;
}
-#line 389 "parser.rl"
+#line 394 "parser.rl"
-#line 1068 "parser.c"
+#line 1073 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1103,7 +1108,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 353 "parser.rl"
+#line 358 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
@@ -1123,7 +1128,7 @@ case 2:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1127 "parser.c"
+#line 1132 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1223,14 +1228,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 368 "parser.rl"
+#line 373 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1234 "parser.c"
+#line 1239 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1286,7 +1291,7 @@ case 16:
_out: {}
}
-#line 390 "parser.rl"
+#line 395 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@@ -1367,7 +1372,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1371 "parser.c"
+#line 1376 "parser.c"
static const int JSON_string_start = 1;
static const int JSON_string_first_final = 8;
static const int JSON_string_error = 0;
@@ -1375,7 +1380,7 @@ static const int JSON_string_error = 0;
static const int JSON_string_en_main = 1;
-#line 489 "parser.rl"
+#line 494 "parser.rl"
static int
@@ -1397,15 +1402,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_buf_new(0);
-#line 1401 "parser.c"
+#line 1406 "parser.c"
{
cs = JSON_string_start;
}
-#line 510 "parser.rl"
+#line 515 "parser.rl"
json->memo = p;
-#line 1409 "parser.c"
+#line 1414 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1430,7 +1435,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 475 "parser.rl"
+#line 480 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1441,14 +1446,14 @@ case 2:
{p = (( p + 1))-1;}
}
}
-#line 486 "parser.rl"
+#line 491 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1452 "parser.c"
+#line 1457 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1524,7 +1529,7 @@ case 7:
_out: {}
}
-#line 512 "parser.rl"
+#line 517 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1723,7 +1728,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1727 "parser.c"
+#line 1732 "parser.c"
static const int JSON_start = 1;
static const int JSON_first_final = 10;
static const int JSON_error = 0;
@@ -1731,7 +1736,7 @@ static const int JSON_error = 0;
static const int JSON_en_main = 1;
-#line 734 "parser.rl"
+#line 739 "parser.rl"
static VALUE cParser_parse_strict(VALUE self)
@@ -1742,16 +1747,16 @@ static VALUE cParser_parse_strict(VALUE self)
GET_PARSER;
-#line 1746 "parser.c"
+#line 1751 "parser.c"
{
cs = JSON_start;
}
-#line 744 "parser.rl"
+#line 749 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1755 "parser.c"
+#line 1760 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1807,7 +1812,7 @@ case 5:
goto st1;
goto st5;
tr3:
-#line 723 "parser.rl"
+#line 728 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1816,7 +1821,7 @@ case 5:
}
goto st10;
tr4:
-#line 716 "parser.rl"
+#line 721 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1828,7 +1833,7 @@ case 5:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1832 "parser.c"
+#line 1837 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1885,7 +1890,7 @@ case 9:
_out: {}
}
-#line 747 "parser.rl"
+#line 752 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -1897,7 +1902,7 @@ case 9:
-#line 1901 "parser.c"
+#line 1906 "parser.c"
static const int JSON_quirks_mode_start = 1;
static const int JSON_quirks_mode_first_final = 10;
static const int JSON_quirks_mode_error = 0;
@@ -1905,7 +1910,7 @@ static const int JSON_quirks_mode_error = 0;
static const int JSON_quirks_mode_en_main = 1;
-#line 772 "parser.rl"
+#line 777 "parser.rl"
static VALUE cParser_parse_quirks_mode(VALUE self)
@@ -1916,16 +1921,16 @@ static VALUE cParser_parse_quirks_mode(VALUE self)
GET_PARSER;
-#line 1920 "parser.c"
+#line 1925 "parser.c"
{
cs = JSON_quirks_mode_start;
}
-#line 782 "parser.rl"
+#line 787 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1929 "parser.c"
+#line 1934 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1959,7 +1964,7 @@ case 1:
cs = 0;
goto _out;
tr2:
-#line 764 "parser.rl"
+#line 769 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1969,7 +1974,7 @@ cs = 0;
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1973 "parser.c"
+#line 1978 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -2058,7 +2063,7 @@ case 9:
_out: {}
}
-#line 785 "parser.rl"
+#line 790 "parser.rl"
if (cs >= JSON_quirks_mode_first_final && p == pe) {
return result;
@@ -2173,6 +2178,7 @@ void Init_parser()
i_key_p = rb_intern("key?");
i_deep_const_get = rb_intern("deep_const_get");
i_aset = rb_intern("[]=");
+ i_aref = rb_intern("[]");
i_leftshift = rb_intern("<<");
#ifdef HAVE_RUBY_ENCODING_H
CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
View
10 ext/json/ext/parser/parser.rl
@@ -79,7 +79,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity;
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_quirks_mode,
i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match,
- i_match_string, i_aset, i_leftshift;
+ i_match_string, i_aset, i_aref, i_leftshift;
%%{
machine JSON_common;
@@ -167,7 +167,12 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
if (cs >= JSON_object_first_final) {
if (json->create_additions) {
- VALUE klassname = rb_hash_aref(*result, json->create_id);
+ VALUE klassname;
+ if (NIL_P(json->object_class)) {
+ klassname = rb_hash_aref(*result, json->create_id);
+ } else {
+ klassname = rb_funcall(*result, i_aref, 1, json->create_id);
+ }
if (!NIL_P(klassname)) {
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
@@ -896,6 +901,7 @@ void Init_parser()
i_key_p = rb_intern("key?");
i_deep_const_get = rb_intern("deep_const_get");
i_aset = rb_intern("[]=");
+ i_aref = rb_intern("[]");
i_leftshift = rb_intern("<<");
#ifdef HAVE_RUBY_ENCODING_H
CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
View
25 tests/test_json.rb
@@ -5,6 +5,7 @@
require File.join(File.dirname(__FILE__), 'setup_variant')
require 'stringio'
require 'tempfile'
+require 'ostruct'
unless Array.method_defined?(:permutation)
begin
@@ -256,13 +257,35 @@ def self.json_create(o)
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)
Please sign in to comment.
Something went wrong with that request. Please try again.