diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/json/ext/generator/generator.c | 111 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.c | 169 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.rl | 24 |
3 files changed, 176 insertions, 128 deletions
diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 10669d7..cc8c3a7 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -237,6 +237,7 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string) int escape_len; unsigned char c; char buf[6] = { '\\', 'u' }; + int ascii_only = rb_enc_str_asciionly_p(string); for (start = 0, end = 0; end < len;) { p = ptr + end; @@ -281,14 +282,17 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string) break; default: { - unsigned short clen = trailingBytesForUTF8[c] + 1; - if (end + clen > len) { - rb_raise(rb_path2class("JSON::GeneratorError"), - "partial character in source, but hit end"); - } - if (!isLegalUTF8((UTF8 *) p, clen)) { - rb_raise(rb_path2class("JSON::GeneratorError"), - "source sequence is illegal/malformed utf-8"); + unsigned short clen = 1; + if (!ascii_only) { + clen += trailingBytesForUTF8[c]; + if (end + clen > len) { + rb_raise(rb_path2class("JSON::GeneratorError"), + "partial character in source, but hit end"); + } + if (!isLegalUTF8((UTF8 *) p, clen)) { + rb_raise(rb_path2class("JSON::GeneratorError"), + "source sequence is illegal/malformed utf-8"); + } } end += clen; } @@ -715,43 +719,83 @@ static VALUE cState_aset(VALUE self, VALUE name, VALUE value) return Qnil; } -static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj) +struct hash_foreach_arg { + FBuffer *buffer; + JSON_Generator_State *state; + VALUE Vstate; + int iter; +}; + +static int +json_object_i(VALUE key, VALUE val, VALUE _arg) { + struct hash_foreach_arg *arg = (struct hash_foreach_arg *)_arg; + FBuffer *buffer = arg->buffer; + JSON_Generator_State *state = arg->state; + VALUE Vstate = arg->Vstate; + char *object_nl = state->object_nl; long object_nl_len = state->object_nl_len; char *indent = state->indent; long indent_len = state->indent_len; - long max_nesting = state->max_nesting; char *delim = FBUFFER_PTR(state->object_delim); long delim_len = FBUFFER_LEN(state->object_delim); char *delim2 = FBUFFER_PTR(state->object_delim2); long delim2_len = FBUFFER_LEN(state->object_delim2); + long depth = state->depth; + int j; + VALUE klass, key_to_s; + + if (arg->iter > 0) fbuffer_append(buffer, delim, delim_len); + if (object_nl) { + fbuffer_append(buffer, object_nl, object_nl_len); + } + if (indent) { + for (j = 0; j < depth; j++) { + fbuffer_append(buffer, indent, indent_len); + } + } + + klass = CLASS_OF(key); + if (klass == rb_cString) { + key_to_s = key; + } else if (klass == rb_cSymbol) { + key_to_s = rb_id2str(SYM2ID(key)); + } else { + key_to_s = rb_funcall(key, i_to_s, 0); + } + Check_Type(key_to_s, T_STRING); + generate_json(buffer, Vstate, state, key_to_s); + fbuffer_append(buffer, delim2, delim2_len); + generate_json(buffer, Vstate, state, val); + + arg->iter++; + return ST_CONTINUE; +} + +static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj) +{ + char *object_nl = state->object_nl; + long object_nl_len = state->object_nl_len; + char *indent = state->indent; + long indent_len = state->indent_len; + long max_nesting = state->max_nesting; long depth = ++state->depth; - int i, j; - VALUE key, key_to_s, keys; + int j; + struct hash_foreach_arg arg; + if (max_nesting != 0 && depth > max_nesting) { fbuffer_free(buffer); rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth); } fbuffer_append_char(buffer, '{'); - keys = rb_funcall(obj, i_keys, 0); - for(i = 0; i < RARRAY_LEN(keys); i++) { - if (i > 0) fbuffer_append(buffer, delim, delim_len); - if (object_nl) { - fbuffer_append(buffer, object_nl, object_nl_len); - } - if (indent) { - for (j = 0; j < depth; j++) { - fbuffer_append(buffer, indent, indent_len); - } - } - key = rb_ary_entry(keys, i); - key_to_s = rb_funcall(key, i_to_s, 0); - Check_Type(key_to_s, T_STRING); - generate_json(buffer, Vstate, state, key_to_s); - fbuffer_append(buffer, delim2, delim2_len); - generate_json(buffer, Vstate, state, rb_hash_aref(obj, key)); - } + + arg.buffer = buffer; + arg.state = state; + arg.Vstate = Vstate; + arg.iter = 0; + rb_hash_foreach(obj, json_object_i, (VALUE)&arg); + depth = --state->depth; if (object_nl) { fbuffer_append(buffer, object_nl, object_nl_len); @@ -806,7 +850,9 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S { fbuffer_append_char(buffer, '"'); #ifdef HAVE_RUBY_ENCODING_H - obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8); + if (!rb_enc_str_asciicompat_p(obj)) { + obj = rb_str_encode(obj, CEncoding_UTF_8, 0, Qnil); + } #endif if (state->ascii_only) { convert_UTF8_to_JSON_ASCII(buffer, obj); @@ -1269,7 +1315,7 @@ static VALUE cState_allow_nan_p(VALUE self) /* * call-seq: ascii_only? * - * Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise + * Returns true, if only ASCII characters should be generated. Otherwise * returns false. */ static VALUE cState_ascii_only_p(VALUE self) @@ -1337,6 +1383,7 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l */ void Init_generator(void) { +#undef rb_intern rb_require("json/common"); mJSON = rb_define_module("JSON"); diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 595845b..63b895e 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -100,11 +100,11 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_leftshift, i_new, i_BigDecimal; -#line 125 "parser.rl" +#line 126 "parser.rl" -#line 107 "parser.c" +#line 108 "parser.c" enum {JSON_object_start = 1}; enum {JSON_object_first_final = 27}; enum {JSON_object_error = 0}; @@ -112,7 +112,7 @@ enum {JSON_object_error = 0}; enum {JSON_object_en_main = 1}; -#line 166 "parser.rl" +#line 167 "parser.rl" static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) @@ -128,14 +128,14 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class); -#line 131 "parser.c" +#line 132 "parser.c" { cs = JSON_object_start; } -#line 181 "parser.rl" +#line 182 "parser.rl" -#line 138 "parser.c" +#line 139 "parser.c" { if ( p == pe ) goto _test_eof; @@ -163,7 +163,7 @@ case 2: goto st2; goto st0; tr2: -#line 148 "parser.rl" +#line 149 "parser.rl" { char *np; json->parsing_name = 1; @@ -176,7 +176,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 179 "parser.c" +#line 180 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -243,7 +243,7 @@ case 8: goto st8; goto st0; tr11: -#line 133 "parser.rl" +#line 134 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v, current_nesting); @@ -263,7 +263,7 @@ st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 266 "parser.c" +#line 267 "parser.c" switch( (*p) ) { case 13: goto st9; case 32: goto st9; @@ -352,14 +352,14 @@ case 18: goto st9; goto st18; tr4: -#line 156 "parser.rl" +#line 157 "parser.rl" { p--; {p++; cs = 27; goto _out;} } goto st27; st27: if ( ++p == pe ) goto _test_eof27; case 27: -#line 362 "parser.c" +#line 363 "parser.c" goto st0; st19: if ( ++p == pe ) @@ -457,7 +457,7 @@ case 26: _out: {} } -#line 182 "parser.rl" +#line 183 "parser.rl" if (cs >= JSON_object_first_final) { if (json->create_additions) { @@ -482,7 +482,7 @@ case 26: -#line 485 "parser.c" +#line 486 "parser.c" enum {JSON_value_start = 1}; enum {JSON_value_first_final = 29}; enum {JSON_value_error = 0}; @@ -490,7 +490,7 @@ enum {JSON_value_error = 0}; enum {JSON_value_en_main = 1}; -#line 282 "parser.rl" +#line 283 "parser.rl" static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) @@ -498,14 +498,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 501 "parser.c" +#line 502 "parser.c" { cs = JSON_value_start; } -#line 289 "parser.rl" +#line 290 "parser.rl" -#line 508 "parser.c" +#line 509 "parser.c" { if ( p == pe ) goto _test_eof; @@ -539,14 +539,14 @@ st0: cs = 0; goto _out; tr2: -#line 234 "parser.rl" +#line 235 "parser.rl" { char *np = JSON_parse_string(json, p, pe, result); if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;} } goto st29; tr3: -#line 239 "parser.rl" +#line 240 "parser.rl" { char *np; if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) { @@ -566,7 +566,7 @@ tr3: } goto st29; tr7: -#line 257 "parser.rl" +#line 258 "parser.rl" { char *np; np = JSON_parse_array(json, p, pe, result, current_nesting + 1); @@ -574,7 +574,7 @@ tr7: } goto st29; tr11: -#line 263 "parser.rl" +#line 264 "parser.rl" { char *np; np = JSON_parse_object(json, p, pe, result, current_nesting + 1); @@ -582,7 +582,7 @@ tr11: } goto st29; tr25: -#line 227 "parser.rl" +#line 228 "parser.rl" { if (json->allow_nan) { *result = CInfinity; @@ -592,7 +592,7 @@ tr25: } goto st29; tr27: -#line 220 "parser.rl" +#line 221 "parser.rl" { if (json->allow_nan) { *result = CNaN; @@ -602,19 +602,19 @@ tr27: } goto st29; tr31: -#line 214 "parser.rl" +#line 215 "parser.rl" { *result = Qfalse; } goto st29; tr34: -#line 211 "parser.rl" +#line 212 "parser.rl" { *result = Qnil; } goto st29; tr37: -#line 217 "parser.rl" +#line 218 "parser.rl" { *result = Qtrue; } @@ -623,9 +623,9 @@ st29: if ( ++p == pe ) goto _test_eof29; case 29: -#line 269 "parser.rl" +#line 270 "parser.rl" { p--; {p++; cs = 29; goto _out;} } -#line 628 "parser.c" +#line 629 "parser.c" switch( (*p) ) { case 13: goto st29; case 32: goto st29; @@ -866,7 +866,7 @@ case 28: _out: {} } -#line 290 "parser.rl" +#line 291 "parser.rl" if (cs >= JSON_value_first_final) { return p; @@ -876,7 +876,7 @@ case 28: } -#line 879 "parser.c" +#line 880 "parser.c" enum {JSON_integer_start = 1}; enum {JSON_integer_first_final = 3}; enum {JSON_integer_error = 0}; @@ -884,7 +884,7 @@ enum {JSON_integer_error = 0}; enum {JSON_integer_en_main = 1}; -#line 306 "parser.rl" +#line 307 "parser.rl" static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -892,15 +892,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res int cs = EVIL; -#line 895 "parser.c" +#line 896 "parser.c" { cs = JSON_integer_start; } -#line 313 "parser.rl" +#line 314 "parser.rl" json->memo = p; -#line 903 "parser.c" +#line 904 "parser.c" { if ( p == pe ) goto _test_eof; @@ -934,14 +934,14 @@ case 3: goto st0; goto tr4; tr4: -#line 303 "parser.rl" +#line 304 "parser.rl" { p--; {p++; cs = 4; goto _out;} } goto st4; st4: if ( ++p == pe ) goto _test_eof4; case 4: -#line 944 "parser.c" +#line 945 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -960,7 +960,7 @@ case 5: _out: {} } -#line 315 "parser.rl" +#line 316 "parser.rl" if (cs >= JSON_integer_first_final) { long len = p - json->memo; @@ -975,7 +975,7 @@ case 5: } -#line 978 "parser.c" +#line 979 "parser.c" enum {JSON_float_start = 1}; enum {JSON_float_first_final = 8}; enum {JSON_float_error = 0}; @@ -983,19 +983,20 @@ enum {JSON_float_error = 0}; enum {JSON_float_en_main = 1}; -#line 340 "parser.rl" +#line 341 "parser.rl" static int is_bigdecimal_class(VALUE obj) { - if (cBigDecimal == Qundef) { - if (rb_const_defined(rb_cObject, i_BigDecimal)) { - cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal); - } else { - return 0; - } + if (cBigDecimal == Qundef) { + if (rb_const_defined(rb_cObject, i_BigDecimal)) { + cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal); } - return obj == cBigDecimal; + else { + return 0; + } + } + return obj == cBigDecimal; } static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -1003,15 +1004,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 994 "parser.c" +#line 1008 "parser.c" { cs = JSON_float_start; } -#line 347 "parser.rl" +#line 361 "parser.rl" json->memo = p; -#line 1002 "parser.c" +#line 1016 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1069,14 +1070,14 @@ case 8: goto st0; goto tr9; tr9: -#line 334 "parser.rl" +#line 335 "parser.rl" { p--; {p++; cs = 9; goto _out;} } goto st9; st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 1067 "parser.c" +#line 1081 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -1137,7 +1138,7 @@ case 7: _out: {} } -#line 349 "parser.rl" +#line 363 "parser.rl" if (cs >= JSON_float_first_final) { long len = p - json->memo; @@ -1163,7 +1164,7 @@ case 7: -#line 1150 "parser.c" +#line 1168 "parser.c" enum {JSON_array_start = 1}; enum {JSON_array_first_final = 17}; enum {JSON_array_error = 0}; @@ -1171,7 +1172,7 @@ enum {JSON_array_error = 0}; enum {JSON_array_en_main = 1}; -#line 398 "parser.rl" +#line 416 "parser.rl" static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) @@ -1185,14 +1186,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 1172 "parser.c" +#line 1190 "parser.c" { cs = JSON_array_start; } -#line 411 "parser.rl" +#line 429 "parser.rl" -#line 1179 "parser.c" +#line 1197 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1231,7 +1232,7 @@ case 2: goto st2; goto st0; tr2: -#line 375 "parser.rl" +#line 393 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v, current_nesting); @@ -1251,7 +1252,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 1238 "parser.c" +#line 1256 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -1351,14 +1352,14 @@ case 12: goto st3; goto st12; tr4: -#line 390 "parser.rl" +#line 408 "parser.rl" { p--; {p++; cs = 17; goto _out;} } goto st17; st17: if ( ++p == pe ) goto _test_eof17; case 17: -#line 1345 "parser.c" +#line 1363 "parser.c" goto st0; st13: if ( ++p == pe ) @@ -1414,7 +1415,7 @@ case 16: _out: {} } -#line 412 "parser.rl" +#line 430 "parser.rl" if(cs >= JSON_array_first_final) { return p + 1; @@ -1503,7 +1504,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd) } -#line 1490 "parser.c" +#line 1508 "parser.c" enum {JSON_string_start = 1}; enum {JSON_string_first_final = 8}; enum {JSON_string_error = 0}; @@ -1511,7 +1512,7 @@ enum {JSON_string_error = 0}; enum {JSON_string_en_main = 1}; -#line 519 "parser.rl" +#line 537 "parser.rl" static int @@ -1533,15 +1534,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu *result = rb_str_buf_new(0); -#line 1520 "parser.c" +#line 1538 "parser.c" { cs = JSON_string_start; } -#line 540 "parser.rl" +#line 558 "parser.rl" json->memo = p; -#line 1528 "parser.c" +#line 1546 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1566,7 +1567,7 @@ case 2: goto st0; goto st2; tr2: -#line 505 "parser.rl" +#line 523 "parser.rl" { *result = json_string_unescape(*result, json->memo + 1, p); if (NIL_P(*result)) { @@ -1577,14 +1578,14 @@ tr2: {p = (( p + 1))-1;} } } -#line 516 "parser.rl" +#line 534 "parser.rl" { p--; {p++; cs = 8; goto _out;} } goto st8; st8: if ( ++p == pe ) goto _test_eof8; case 8: -#line 1571 "parser.c" +#line 1589 "parser.c" goto st0; st3: if ( ++p == pe ) @@ -1660,7 +1661,7 @@ case 7: _out: {} } -#line 542 "parser.rl" +#line 560 "parser.rl" if (json->create_additions && RTEST(match_string = json->match_string)) { VALUE klass; @@ -1675,10 +1676,8 @@ case 7: if (json->symbolize_names && json->parsing_name) { *result = rb_str_intern(*result); - } else { - if (RB_TYPE_P(*result, T_STRING)) { - rb_str_resize(*result, RSTRING_LEN(*result)); - } + } else if (RB_TYPE_P(*result, T_STRING)) { + rb_str_resize(*result, RSTRING_LEN(*result)); } if (cs >= JSON_string_first_final) { return p + 1; @@ -1849,7 +1848,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } -#line 1836 "parser.c" +#line 1852 "parser.c" enum {JSON_start = 1}; enum {JSON_first_final = 10}; enum {JSON_error = 0}; @@ -1857,7 +1856,7 @@ enum {JSON_error = 0}; enum {JSON_en_main = 1}; -#line 744 "parser.rl" +#line 760 "parser.rl" /* @@ -1874,16 +1873,16 @@ static VALUE cParser_parse(VALUE self) GET_PARSER; -#line 1861 "parser.c" +#line 1877 "parser.c" { cs = JSON_start; } -#line 760 "parser.rl" +#line 776 "parser.rl" p = json->source; pe = p + json->len; -#line 1870 "parser.c" +#line 1886 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1917,7 +1916,7 @@ st0: cs = 0; goto _out; tr2: -#line 736 "parser.rl" +#line 752 "parser.rl" { char *np = JSON_parse_value(json, p, pe, &result, 0); if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} @@ -1927,7 +1926,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1914 "parser.c" +#line 1930 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -2016,7 +2015,7 @@ case 9: _out: {} } -#line 763 "parser.rl" +#line 779 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2083,6 +2082,7 @@ static VALUE cParser_source(VALUE self) void Init_parser(void) { +#undef rb_intern rb_require("json/common"); mJSON = rb_define_module("JSON"); mExt = rb_define_module_under(mJSON, "Ext"); @@ -2117,6 +2117,7 @@ void Init_parser(void) i_aref = rb_intern("[]"); i_leftshift = rb_intern("<<"); i_new = rb_intern("new"); + i_BigDecimal = rb_intern("BigDecimal"); } /* diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index c888d9c..e7738d6 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -342,14 +342,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res static int is_bigdecimal_class(VALUE obj) { - if (cBigDecimal == Qundef) { - if (rb_const_defined(rb_cObject, i_BigDecimal)) { - cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal); - } else { - return 0; - } - } - return obj == cBigDecimal; + if (cBigDecimal == Qundef) { + if (rb_const_defined(rb_cObject, i_BigDecimal)) { + cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal); + } + else { + return 0; + } + } + return obj == cBigDecimal; } static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -570,10 +571,8 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu if (json->symbolize_names && json->parsing_name) { *result = rb_str_intern(*result); - } else { - if (RB_TYPE_P(*result, T_STRING)) { - rb_str_resize(*result, RSTRING_LEN(*result)); - } + } else if (RB_TYPE_P(*result, T_STRING)) { + rb_str_resize(*result, RSTRING_LEN(*result)); } if (cs >= JSON_string_first_final) { return p + 1; @@ -878,6 +877,7 @@ void Init_parser(void) i_aref = rb_intern("[]"); i_leftshift = rb_intern("<<"); i_new = rb_intern("new"); + i_BigDecimal = rb_intern("BigDecimal"); } /* |