summaryrefslogtreecommitdiff
path: root/ext/json/ext/parser/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json/ext/parser/parser.c')
-rw-r--r--ext/json/ext/parser/parser.c182
1 files changed, 103 insertions, 79 deletions
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index 6928eda9e5..6851e06235 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -29,7 +29,7 @@ static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
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_chr, i_max_nesting, i_allow_nan, i_object_class, i_array_class;
#define MinusInfinity "-Infinity"
@@ -42,6 +42,8 @@ typedef struct JSON_ParserStruct {
int max_nesting;
int current_nesting;
int allow_nan;
+ VALUE object_class;
+ VALUE array_class;
} JSON_Parser;
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result);
@@ -56,11 +58,11 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
Data_Get_Struct(self, JSON_Parser, json);
-#line 82 "parser.rl"
+#line 84 "parser.rl"
-#line 64 "parser.c"
+#line 66 "parser.c"
static const int JSON_object_start = 1;
static const int JSON_object_first_final = 27;
static const int JSON_object_error = 0;
@@ -68,29 +70,30 @@ static const int JSON_object_error = 0;
static const int JSON_object_en_main = 1;
-#line 115 "parser.rl"
+#line 117 "parser.rl"
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
VALUE last_name = Qnil;
+ VALUE object_class = json->object_class;
if (json->max_nesting && json->current_nesting > json->max_nesting) {
- rb_raise(eNestingError, "nesting of %d is to deep", json->current_nesting);
+ rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
}
- *result = rb_hash_new();
+ *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
-#line 87 "parser.c"
+#line 90 "parser.c"
{
cs = JSON_object_start;
}
-#line 129 "parser.rl"
+#line 132 "parser.rl"
-#line 94 "parser.c"
+#line 97 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -118,7 +121,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 101 "parser.rl"
+#line 103 "parser.rl"
{
char *np = JSON_parse_string(json, p, pe, &last_name);
if (np == NULL) { p--; {p++; cs = 3; goto _out;} } else {p = (( np))-1;}
@@ -128,7 +131,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 132 "parser.c"
+#line 135 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -195,7 +198,7 @@ case 8:
goto st8;
goto st0;
tr11:
-#line 90 "parser.rl"
+#line 92 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
@@ -211,7 +214,7 @@ st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 215 "parser.c"
+#line 218 "parser.c"
switch( (*p) ) {
case 13: goto st9;
case 32: goto st9;
@@ -300,14 +303,14 @@ case 18:
goto st9;
goto st18;
tr4:
-#line 106 "parser.rl"
+#line 108 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} }
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
-#line 311 "parser.c"
+#line 314 "parser.c"
goto st0;
st19:
if ( ++p == pe )
@@ -405,7 +408,7 @@ case 26:
_out: {}
}
-#line 130 "parser.rl"
+#line 133 "parser.rl"
if (cs >= JSON_object_first_final) {
if (RTEST(json->create_id)) {
@@ -424,7 +427,7 @@ case 26:
}
-#line 428 "parser.c"
+#line 431 "parser.c"
static const int JSON_value_start = 1;
static const int JSON_value_first_final = 21;
static const int JSON_value_error = 0;
@@ -432,7 +435,7 @@ static const int JSON_value_error = 0;
static const int JSON_value_en_main = 1;
-#line 228 "parser.rl"
+#line 231 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -440,14 +443,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 444 "parser.c"
+#line 447 "parser.c"
{
cs = JSON_value_start;
}
-#line 235 "parser.rl"
+#line 238 "parser.rl"
-#line 451 "parser.c"
+#line 454 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -472,14 +475,14 @@ st0:
cs = 0;
goto _out;
tr0:
-#line 176 "parser.rl"
+#line 179 "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 181 "parser.rl"
+#line 184 "parser.rl"
{
char *np;
if(pe > p + 9 && !strncmp(MinusInfinity, p, 9)) {
@@ -499,7 +502,7 @@ tr2:
}
goto st21;
tr5:
-#line 199 "parser.rl"
+#line 202 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -509,7 +512,7 @@ tr5:
}
goto st21;
tr9:
-#line 207 "parser.rl"
+#line 210 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -519,7 +522,7 @@ tr9:
}
goto st21;
tr16:
-#line 169 "parser.rl"
+#line 172 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
@@ -529,7 +532,7 @@ tr16:
}
goto st21;
tr18:
-#line 162 "parser.rl"
+#line 165 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
@@ -539,19 +542,19 @@ tr18:
}
goto st21;
tr22:
-#line 156 "parser.rl"
+#line 159 "parser.rl"
{
*result = Qfalse;
}
goto st21;
tr25:
-#line 153 "parser.rl"
+#line 156 "parser.rl"
{
*result = Qnil;
}
goto st21;
tr28:
-#line 159 "parser.rl"
+#line 162 "parser.rl"
{
*result = Qtrue;
}
@@ -560,9 +563,9 @@ st21:
if ( ++p == pe )
goto _test_eof21;
case 21:
-#line 215 "parser.rl"
+#line 218 "parser.rl"
{ p--; {p++; cs = 21; goto _out;} }
-#line 566 "parser.c"
+#line 569 "parser.c"
goto st0;
st2:
if ( ++p == pe )
@@ -723,7 +726,7 @@ case 20:
_out: {}
}
-#line 236 "parser.rl"
+#line 239 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
@@ -733,7 +736,7 @@ case 20:
}
-#line 737 "parser.c"
+#line 740 "parser.c"
static const int JSON_integer_start = 1;
static const int JSON_integer_first_final = 5;
static const int JSON_integer_error = 0;
@@ -741,7 +744,7 @@ static const int JSON_integer_error = 0;
static const int JSON_integer_en_main = 1;
-#line 252 "parser.rl"
+#line 255 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -749,15 +752,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 753 "parser.c"
+#line 756 "parser.c"
{
cs = JSON_integer_start;
}
-#line 259 "parser.rl"
+#line 262 "parser.rl"
json->memo = p;
-#line 761 "parser.c"
+#line 764 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -791,14 +794,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 249 "parser.rl"
+#line 252 "parser.rl"
{ p--; {p++; cs = 5; goto _out;} }
goto st5;
st5:
if ( ++p == pe )
goto _test_eof5;
case 5:
-#line 802 "parser.c"
+#line 805 "parser.c"
goto st0;
st4:
if ( ++p == pe )
@@ -817,7 +820,7 @@ case 4:
_out: {}
}
-#line 261 "parser.rl"
+#line 264 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -829,7 +832,7 @@ case 4:
}
-#line 833 "parser.c"
+#line 836 "parser.c"
static const int JSON_float_start = 1;
static const int JSON_float_first_final = 10;
static const int JSON_float_error = 0;
@@ -837,7 +840,7 @@ static const int JSON_float_error = 0;
static const int JSON_float_en_main = 1;
-#line 283 "parser.rl"
+#line 286 "parser.rl"
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -845,15 +848,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 849 "parser.c"
+#line 852 "parser.c"
{
cs = JSON_float_start;
}
-#line 290 "parser.rl"
+#line 293 "parser.rl"
json->memo = p;
-#line 857 "parser.c"
+#line 860 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -911,14 +914,14 @@ case 5:
goto st0;
goto tr7;
tr7:
-#line 277 "parser.rl"
+#line 280 "parser.rl"
{ p--; {p++; cs = 10; goto _out;} }
goto st10;
st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 922 "parser.c"
+#line 925 "parser.c"
goto st0;
st6:
if ( ++p == pe )
@@ -979,7 +982,7 @@ case 9:
_out: {}
}
-#line 292 "parser.rl"
+#line 295 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
@@ -992,7 +995,7 @@ case 9:
-#line 996 "parser.c"
+#line 999 "parser.c"
static const int JSON_array_start = 1;
static const int JSON_array_first_final = 17;
static const int JSON_array_error = 0;
@@ -1000,27 +1003,28 @@ static const int JSON_array_error = 0;
static const int JSON_array_en_main = 1;
-#line 328 "parser.rl"
+#line 331 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
+ VALUE array_class = json->array_class;
if (json->max_nesting && json->current_nesting > json->max_nesting) {
- rb_raise(eNestingError, "nesting of %d is to deep", json->current_nesting);
+ rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
}
- *result = rb_ary_new();
+ *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
-#line 1017 "parser.c"
+#line 1021 "parser.c"
{
cs = JSON_array_start;
}
-#line 340 "parser.rl"
+#line 344 "parser.rl"
-#line 1024 "parser.c"
+#line 1028 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1059,7 +1063,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 309 "parser.rl"
+#line 312 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
@@ -1075,7 +1079,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1079 "parser.c"
+#line 1083 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1175,14 +1179,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 320 "parser.rl"
+#line 323 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1186 "parser.c"
+#line 1190 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1238,7 +1242,7 @@ case 16:
_out: {}
}
-#line 341 "parser.rl"
+#line 345 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@@ -1304,7 +1308,7 @@ static VALUE json_string_unescape(char *p, char *pe)
}
-#line 1308 "parser.c"
+#line 1312 "parser.c"
static const int JSON_string_start = 1;
static const int JSON_string_first_final = 8;
static const int JSON_string_error = 0;
@@ -1312,7 +1316,7 @@ static const int JSON_string_error = 0;
static const int JSON_string_en_main = 1;
-#line 425 "parser.rl"
+#line 429 "parser.rl"
static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -1321,15 +1325,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_new("", 0);
-#line 1325 "parser.c"
+#line 1329 "parser.c"
{
cs = JSON_string_start;
}
-#line 433 "parser.rl"
+#line 437 "parser.rl"
json->memo = p;
-#line 1333 "parser.c"
+#line 1337 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1354,7 +1358,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 411 "parser.rl"
+#line 415 "parser.rl"
{
*result = json_string_unescape(json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1365,14 +1369,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 422 "parser.rl"
+#line 426 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1376 "parser.c"
+#line 1380 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1448,7 +1452,7 @@ case 7:
_out: {}
}
-#line 435 "parser.rl"
+#line 439 "parser.rl"
if (cs >= JSON_string_first_final) {
return p + 1;
@@ -1459,7 +1463,7 @@ case 7:
-#line 1463 "parser.c"
+#line 1467 "parser.c"
static const int JSON_start = 1;
static const int JSON_first_final = 10;
static const int JSON_error = 0;
@@ -1467,7 +1471,7 @@ static const int JSON_error = 0;
static const int JSON_en_main = 1;
-#line 469 "parser.rl"
+#line 473 "parser.rl"
/*
@@ -1502,6 +1506,8 @@ static const int JSON_en_main = 1;
* * *create_additions*: If set to false, the Parser doesn't create
* additions even if a matchin class and create_id was found. This option
* defaults to true.
+ * * *object_class*: Defaults to Hash
+ * * *array_class*: Defaults to Array
*/
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
{
@@ -1551,11 +1557,25 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} else {
json->create_id = rb_funcall(mJSON, i_create_id, 0);
}
+ tmp = ID2SYM(i_object_class);
+ if (st_lookup(RHASH_TBL(opts), tmp, 0)) {
+ json->object_class = rb_hash_aref(opts, tmp);
+ } else {
+ json->object_class = Qnil;
+ }
+ tmp = ID2SYM(i_array_class);
+ if (st_lookup(RHASH_TBL(opts), tmp, 0)) {
+ json->array_class = rb_hash_aref(opts, tmp);
+ } else {
+ json->array_class = Qnil;
+ }
}
} else {
json->max_nesting = 19;
json->allow_nan = 0;
json->create_id = rb_funcall(mJSON, i_create_id, 0);
+ json->object_class = Qnil;
+ json->array_class = Qnil;
}
json->current_nesting = 0;
/*
@@ -1590,16 +1610,16 @@ static VALUE cParser_parse(VALUE self)
GET_STRUCT;
-#line 1594 "parser.c"
+#line 1614 "parser.c"
{
cs = JSON_start;
}
-#line 591 "parser.rl"
+#line 611 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1603 "parser.c"
+#line 1623 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1655,7 +1675,7 @@ case 5:
goto st1;
goto st5;
tr3:
-#line 458 "parser.rl"
+#line 462 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1664,7 +1684,7 @@ tr3:
}
goto st10;
tr4:
-#line 451 "parser.rl"
+#line 455 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1676,7 +1696,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1680 "parser.c"
+#line 1700 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1733,7 +1753,7 @@ case 9:
_out: {}
}
-#line 594 "parser.rl"
+#line 614 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -1753,6 +1773,8 @@ static void JSON_mark(JSON_Parser *json)
{
rb_gc_mark_maybe(json->Vsource);
rb_gc_mark_maybe(json->create_id);
+ rb_gc_mark_maybe(json->object_class);
+ rb_gc_mark_maybe(json->array_class);
}
static void JSON_free(JSON_Parser *json)
@@ -1802,4 +1824,6 @@ void Init_parser()
i_chr = rb_intern("chr");
i_max_nesting = rb_intern("max_nesting");
i_allow_nan = rb_intern("allow_nan");
+ i_object_class = rb_intern("object_class");
+ i_array_class = rb_intern("array_class");
}