ext/json/ext/parser/parser.rl in json_pure-1.1.1 vs ext/json/ext/parser/parser.rl in json_pure-1.1.2

- old
+ new

@@ -8,12 +8,12 @@ #define EVIL 0x666 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_chr, i_max_nesting, - i_allow_nan; +static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, + i_chr, i_max_nesting, i_allow_nan; #define MinusInfinity "-Infinity" typedef struct JSON_ParserStruct { VALUE Vsource; @@ -111,15 +111,17 @@ %% write init; %% write exec; if (cs >= JSON_object_first_final) { - VALUE klassname = rb_hash_aref(*result, json->create_id); - if (!NIL_P(klassname)) { - VALUE klass = rb_path2class(StringValueCStr(klassname)); - if RTEST(rb_funcall(klass, i_json_creatable_p, 0)) { - *result = rb_funcall(klass, i_json_create, 1, *result); + if (RTEST(json->create_id)) { + VALUE klassname = rb_hash_aref(*result, json->create_id); + if (!NIL_P(klassname)) { + VALUE klass = rb_path2class(StringValueCStr(klassname)); + if RTEST(rb_funcall(klass, i_json_creatable_p, 0)) { + *result = rb_funcall(klass, i_json_create, 1, *result); + } } } return p + 1; } else { return NULL; @@ -471,10 +473,13 @@ * structures. Disable depth checking with :max_nesting => false|nil|0, it * defaults to 19. * * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in * defiance of RFC 4627 to be parsed by the Parser. This option defaults to * false. + * * *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. */ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) { char *ptr; long len; @@ -485,12 +490,10 @@ ptr = RSTRING_PTR(source); len = RSTRING_LEN(source); if (len < 2) { rb_raise(eParserError, "A JSON text must at least contain two octets!"); } - json->max_nesting = 19; - json->allow_nan = 0; if (!NIL_P(opts)) { opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash"); if (NIL_P(opts)) { rb_raise(rb_eArgError, "opts needs to be like a hash"); } else { @@ -501,17 +504,36 @@ Check_Type(max_nesting, T_FIXNUM); json->max_nesting = FIX2INT(max_nesting); } else { json->max_nesting = 0; } + } else { + json->max_nesting = 19; } tmp = ID2SYM(i_allow_nan); if (st_lookup(RHASH(opts)->tbl, tmp, 0)) { VALUE allow_nan = rb_hash_aref(opts, tmp); - if (RTEST(allow_nan)) json->allow_nan = 1; + json->allow_nan = RTEST(allow_nan) ? 1 : 0; + } else { + json->allow_nan = 0; } + tmp = ID2SYM(i_create_additions); + if (st_lookup(RHASH(opts)->tbl, tmp, 0)) { + VALUE create_additions = rb_hash_aref(opts, tmp); + if (RTEST(create_additions)) { + json->create_id = rb_funcall(mJSON, i_create_id, 0); + } else { + json->create_id = Qnil; + } + } else { + json->create_id = rb_funcall(mJSON, i_create_id, 0); + } } + } else { + json->max_nesting = 19; + json->allow_nan = 0; + json->create_id = rb_funcall(mJSON, i_create_id, 0); } json->current_nesting = 0; /* Convert these? if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) { @@ -525,11 +547,10 @@ } */ json->len = len; json->source = ptr; json->Vsource = source; - json->create_id = rb_funcall(mJSON, i_create_id, 0); return self; } /* * call-seq: parse() @@ -554,11 +575,11 @@ } else { rb_raise(eParserError, "unexpected token at '%s'", p); } } -static JSON_Parser *JSON_allocate() +inline static JSON_Parser *JSON_allocate() { JSON_Parser *json = ALLOC(JSON_Parser); MEMZERO(json, JSON_Parser, 1); return json; } @@ -609,9 +630,10 @@ CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity")); i_json_creatable_p = rb_intern("json_creatable?"); i_json_create = rb_intern("json_create"); i_create_id = rb_intern("create_id"); + i_create_additions = rb_intern("create_additions"); i_chr = rb_intern("chr"); i_max_nesting = rb_intern("max_nesting"); i_allow_nan = rb_intern("allow_nan"); }