From b304078ae6b3b669c49579117aba85d72901e972 Mon Sep 17 00:00:00 2001 From: hsbt Date: Fri, 16 Jun 2017 03:04:46 +0000 Subject: Merge json-2.1.0 from https://github.com/flori/json https://github.com/flori/json/blob/master/CHANGES.md#2017-04-18-210 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59099 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/json/json.gemspec | Bin 5474 -> 5474 bytes ext/json/lib/json/version.rb | 2 +- ext/json/parser/parser.c | 169 +++++++++++++++++++++++------------------- ext/json/parser/parser.h | 1 + ext/json/parser/parser.rl | 23 +++++- test/json/json_parser_test.rb | 5 ++ 6 files changed, 120 insertions(+), 80 deletions(-) diff --git a/ext/json/json.gemspec b/ext/json/json.gemspec index 54575411e9..b8f3009a9c 100644 Binary files a/ext/json/json.gemspec and b/ext/json/json.gemspec differ diff --git a/ext/json/lib/json/version.rb b/ext/json/lib/json/version.rb index b88dfb6a08..b65ed87f98 100644 --- a/ext/json/lib/json/version.rb +++ b/ext/json/lib/json/version.rb @@ -1,7 +1,7 @@ # frozen_string_literal: false module JSON # JSON version - VERSION = '2.0.4' + VERSION = '2.1.0' VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc: VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc: VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: diff --git a/ext/json/parser/parser.c b/ext/json/parser/parser.c index c0a240a732..d2e4eb6686 100644 --- a/ext/json/parser/parser.c +++ b/ext/json/parser/parser.c @@ -94,15 +94,16 @@ 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_object_class, i_array_class, i_key_p, i_deep_const_get, i_match, - i_match_string, i_aset, i_aref, i_leftshift; + i_object_class, i_array_class, i_decimal_class, i_key_p, + i_deep_const_get, i_match, i_match_string, i_aset, i_aref, + i_leftshift, i_new; -#line 124 "parser.rl" +#line 125 "parser.rl" -#line 106 "parser.c" +#line 107 "parser.c" enum {JSON_object_start = 1}; enum {JSON_object_first_final = 27}; enum {JSON_object_error = 0}; @@ -110,7 +111,7 @@ enum {JSON_object_error = 0}; enum {JSON_object_en_main = 1}; -#line 165 "parser.rl" +#line 166 "parser.rl" static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) @@ -126,14 +127,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 130 "parser.c" +#line 131 "parser.c" { cs = JSON_object_start; } -#line 180 "parser.rl" +#line 181 "parser.rl" -#line 137 "parser.c" +#line 138 "parser.c" { if ( p == pe ) goto _test_eof; @@ -161,7 +162,7 @@ case 2: goto st2; goto st0; tr2: -#line 147 "parser.rl" +#line 148 "parser.rl" { char *np; json->parsing_name = 1; @@ -174,7 +175,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 178 "parser.c" +#line 179 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -241,7 +242,7 @@ case 8: goto st8; goto st0; tr11: -#line 132 "parser.rl" +#line 133 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v, current_nesting); @@ -261,7 +262,7 @@ st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 265 "parser.c" +#line 266 "parser.c" switch( (*p) ) { case 13: goto st9; case 32: goto st9; @@ -350,14 +351,14 @@ case 18: goto st9; goto st18; tr4: -#line 155 "parser.rl" +#line 156 "parser.rl" { p--; {p++; cs = 27; goto _out;} } goto st27; st27: if ( ++p == pe ) goto _test_eof27; case 27: -#line 361 "parser.c" +#line 362 "parser.c" goto st0; st19: if ( ++p == pe ) @@ -455,7 +456,7 @@ case 26: _out: {} } -#line 181 "parser.rl" +#line 182 "parser.rl" if (cs >= JSON_object_first_final) { if (json->create_additions) { @@ -480,7 +481,7 @@ case 26: -#line 484 "parser.c" +#line 485 "parser.c" enum {JSON_value_start = 1}; enum {JSON_value_first_final = 29}; enum {JSON_value_error = 0}; @@ -488,7 +489,7 @@ enum {JSON_value_error = 0}; enum {JSON_value_en_main = 1}; -#line 281 "parser.rl" +#line 282 "parser.rl" static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) @@ -496,14 +497,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 500 "parser.c" +#line 501 "parser.c" { cs = JSON_value_start; } -#line 288 "parser.rl" +#line 289 "parser.rl" -#line 507 "parser.c" +#line 508 "parser.c" { if ( p == pe ) goto _test_eof; @@ -537,14 +538,14 @@ st0: cs = 0; goto _out; tr2: -#line 233 "parser.rl" +#line 234 "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 238 "parser.rl" +#line 239 "parser.rl" { char *np; if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) { @@ -564,7 +565,7 @@ tr3: } goto st29; tr7: -#line 256 "parser.rl" +#line 257 "parser.rl" { char *np; np = JSON_parse_array(json, p, pe, result, current_nesting + 1); @@ -572,7 +573,7 @@ tr7: } goto st29; tr11: -#line 262 "parser.rl" +#line 263 "parser.rl" { char *np; np = JSON_parse_object(json, p, pe, result, current_nesting + 1); @@ -580,7 +581,7 @@ tr11: } goto st29; tr25: -#line 226 "parser.rl" +#line 227 "parser.rl" { if (json->allow_nan) { *result = CInfinity; @@ -590,7 +591,7 @@ tr25: } goto st29; tr27: -#line 219 "parser.rl" +#line 220 "parser.rl" { if (json->allow_nan) { *result = CNaN; @@ -600,19 +601,19 @@ tr27: } goto st29; tr31: -#line 213 "parser.rl" +#line 214 "parser.rl" { *result = Qfalse; } goto st29; tr34: -#line 210 "parser.rl" +#line 211 "parser.rl" { *result = Qnil; } goto st29; tr37: -#line 216 "parser.rl" +#line 217 "parser.rl" { *result = Qtrue; } @@ -621,9 +622,9 @@ st29: if ( ++p == pe ) goto _test_eof29; case 29: -#line 268 "parser.rl" +#line 269 "parser.rl" { p--; {p++; cs = 29; goto _out;} } -#line 627 "parser.c" +#line 628 "parser.c" switch( (*p) ) { case 13: goto st29; case 32: goto st29; @@ -864,7 +865,7 @@ case 28: _out: {} } -#line 289 "parser.rl" +#line 290 "parser.rl" if (cs >= JSON_value_first_final) { return p; @@ -874,7 +875,7 @@ case 28: } -#line 878 "parser.c" +#line 879 "parser.c" enum {JSON_integer_start = 1}; enum {JSON_integer_first_final = 3}; enum {JSON_integer_error = 0}; @@ -882,7 +883,7 @@ enum {JSON_integer_error = 0}; enum {JSON_integer_en_main = 1}; -#line 305 "parser.rl" +#line 306 "parser.rl" static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -890,15 +891,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res int cs = EVIL; -#line 894 "parser.c" +#line 895 "parser.c" { cs = JSON_integer_start; } -#line 312 "parser.rl" +#line 313 "parser.rl" json->memo = p; -#line 902 "parser.c" +#line 903 "parser.c" { if ( p == pe ) goto _test_eof; @@ -932,14 +933,14 @@ case 3: goto st0; goto tr4; tr4: -#line 302 "parser.rl" +#line 303 "parser.rl" { p--; {p++; cs = 4; goto _out;} } goto st4; st4: if ( ++p == pe ) goto _test_eof4; case 4: -#line 943 "parser.c" +#line 944 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -958,7 +959,7 @@ case 5: _out: {} } -#line 314 "parser.rl" +#line 315 "parser.rl" if (cs >= JSON_integer_first_final) { long len = p - json->memo; @@ -973,7 +974,7 @@ case 5: } -#line 977 "parser.c" +#line 978 "parser.c" enum {JSON_float_start = 1}; enum {JSON_float_first_final = 8}; enum {JSON_float_error = 0}; @@ -981,7 +982,7 @@ enum {JSON_float_error = 0}; enum {JSON_float_en_main = 1}; -#line 339 "parser.rl" +#line 340 "parser.rl" static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -989,15 +990,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 993 "parser.c" +#line 994 "parser.c" { cs = JSON_float_start; } -#line 346 "parser.rl" +#line 347 "parser.rl" json->memo = p; -#line 1001 "parser.c" +#line 1002 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1055,14 +1056,14 @@ case 8: goto st0; goto tr9; tr9: -#line 333 "parser.rl" +#line 334 "parser.rl" { p--; {p++; cs = 9; goto _out;} } goto st9; st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 1066 "parser.c" +#line 1067 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -1123,14 +1124,20 @@ case 7: _out: {} } -#line 348 "parser.rl" +#line 349 "parser.rl" if (cs >= JSON_float_first_final) { long len = p - json->memo; fbuffer_clear(json->fbuffer); fbuffer_append(json->fbuffer, json->memo, len); fbuffer_append_char(json->fbuffer, '\0'); - *result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1)); + if (NIL_P(json->decimal_class)) { + *result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1)); + } else { + VALUE text; + text = rb_str_new2(FBUFFER_PTR(json->fbuffer)); + *result = rb_funcall(json->decimal_class, i_new, 1, text); + } return p + 1; } else { return NULL; @@ -1139,7 +1146,7 @@ case 7: -#line 1143 "parser.c" +#line 1150 "parser.c" enum {JSON_array_start = 1}; enum {JSON_array_first_final = 17}; enum {JSON_array_error = 0}; @@ -1147,7 +1154,7 @@ enum {JSON_array_error = 0}; enum {JSON_array_en_main = 1}; -#line 391 "parser.rl" +#line 398 "parser.rl" static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) @@ -1161,14 +1168,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 1165 "parser.c" +#line 1172 "parser.c" { cs = JSON_array_start; } -#line 404 "parser.rl" +#line 411 "parser.rl" -#line 1172 "parser.c" +#line 1179 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1207,7 +1214,7 @@ case 2: goto st2; goto st0; tr2: -#line 368 "parser.rl" +#line 375 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v, current_nesting); @@ -1227,7 +1234,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 1231 "parser.c" +#line 1238 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -1327,14 +1334,14 @@ case 12: goto st3; goto st12; tr4: -#line 383 "parser.rl" +#line 390 "parser.rl" { p--; {p++; cs = 17; goto _out;} } goto st17; st17: if ( ++p == pe ) goto _test_eof17; case 17: -#line 1338 "parser.c" +#line 1345 "parser.c" goto st0; st13: if ( ++p == pe ) @@ -1390,7 +1397,7 @@ case 16: _out: {} } -#line 405 "parser.rl" +#line 412 "parser.rl" if(cs >= JSON_array_first_final) { return p + 1; @@ -1479,7 +1486,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd) } -#line 1483 "parser.c" +#line 1490 "parser.c" enum {JSON_string_start = 1}; enum {JSON_string_first_final = 8}; enum {JSON_string_error = 0}; @@ -1487,7 +1494,7 @@ enum {JSON_string_error = 0}; enum {JSON_string_en_main = 1}; -#line 512 "parser.rl" +#line 519 "parser.rl" static int @@ -1509,15 +1516,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu *result = rb_str_buf_new(0); -#line 1513 "parser.c" +#line 1520 "parser.c" { cs = JSON_string_start; } -#line 533 "parser.rl" +#line 540 "parser.rl" json->memo = p; -#line 1521 "parser.c" +#line 1528 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1542,7 +1549,7 @@ case 2: goto st0; goto st2; tr2: -#line 498 "parser.rl" +#line 505 "parser.rl" { *result = json_string_unescape(*result, json->memo + 1, p); if (NIL_P(*result)) { @@ -1553,14 +1560,14 @@ tr2: {p = (( p + 1))-1;} } } -#line 509 "parser.rl" +#line 516 "parser.rl" { p--; {p++; cs = 8; goto _out;} } goto st8; st8: if ( ++p == pe ) goto _test_eof8; case 8: -#line 1564 "parser.c" +#line 1571 "parser.c" goto st0; st3: if ( ++p == pe ) @@ -1636,7 +1643,7 @@ case 7: _out: {} } -#line 535 "parser.rl" +#line 542 "parser.rl" if (json->create_additions && RTEST(match_string = json->match_string)) { VALUE klass; @@ -1789,6 +1796,12 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } else { json->array_class = Qnil; } + tmp = ID2SYM(i_decimal_class); + if (option_given_p(opts, tmp)) { + json->decimal_class = rb_hash_aref(opts, tmp); + } else { + json->decimal_class = Qnil; + } tmp = ID2SYM(i_match_string); if (option_given_p(opts, tmp)) { VALUE match_string = rb_hash_aref(opts, tmp); @@ -1806,6 +1819,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->create_id = rb_funcall(mJSON, i_create_id, 0); json->object_class = Qnil; json->array_class = Qnil; + json->decimal_class = Qnil; } source = convert_encoding(StringValue(source)); StringValue(source); @@ -1816,7 +1830,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } -#line 1820 "parser.c" +#line 1834 "parser.c" enum {JSON_start = 1}; enum {JSON_first_final = 10}; enum {JSON_error = 0}; @@ -1824,7 +1838,7 @@ enum {JSON_error = 0}; enum {JSON_en_main = 1}; -#line 728 "parser.rl" +#line 742 "parser.rl" /* @@ -1841,16 +1855,16 @@ static VALUE cParser_parse(VALUE self) GET_PARSER; -#line 1845 "parser.c" +#line 1859 "parser.c" { cs = JSON_start; } -#line 744 "parser.rl" +#line 758 "parser.rl" p = json->source; pe = p + json->len; -#line 1854 "parser.c" +#line 1868 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1884,7 +1898,7 @@ st0: cs = 0; goto _out; tr2: -#line 720 "parser.rl" +#line 734 "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;} @@ -1894,7 +1908,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1898 "parser.c" +#line 1912 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -1983,7 +1997,7 @@ case 9: _out: {} } -#line 747 "parser.rl" +#line 761 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2000,6 +2014,7 @@ static void JSON_mark(void *ptr) rb_gc_mark_maybe(json->create_id); rb_gc_mark_maybe(json->object_class); rb_gc_mark_maybe(json->array_class); + rb_gc_mark_maybe(json->decimal_class); rb_gc_mark_maybe(json->match_string); } @@ -2074,6 +2089,7 @@ void Init_parser(void) i_symbolize_names = rb_intern("symbolize_names"); i_object_class = rb_intern("object_class"); i_array_class = rb_intern("array_class"); + i_decimal_class = rb_intern("decimal_class"); i_match = rb_intern("match"); i_match_string = rb_intern("match_string"); i_key_p = rb_intern("key?"); @@ -2081,6 +2097,7 @@ void Init_parser(void) i_aset = rb_intern("[]="); i_aref = rb_intern("[]"); i_leftshift = rb_intern("<<"); + i_new = rb_intern("new"); } /* diff --git a/ext/json/parser/parser.h b/ext/json/parser/parser.h index 1d46831965..e6cf779024 100644 --- a/ext/json/parser/parser.h +++ b/ext/json/parser/parser.h @@ -39,6 +39,7 @@ typedef struct JSON_ParserStruct { int symbolize_names; VALUE object_class; VALUE array_class; + VALUE decimal_class; int create_additions; VALUE match_string; FBuffer *fbuffer; diff --git a/ext/json/parser/parser.rl b/ext/json/parser/parser.rl index 9e1341e90b..29900a4a4a 100644 --- a/ext/json/parser/parser.rl +++ b/ext/json/parser/parser.rl @@ -92,8 +92,9 @@ 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_object_class, i_array_class, i_key_p, i_deep_const_get, i_match, - i_match_string, i_aset, i_aref, i_leftshift; + i_object_class, i_array_class, i_decimal_class, i_key_p, + i_deep_const_get, i_match, i_match_string, i_aset, i_aref, + i_leftshift, i_new; %%{ machine JSON_common; @@ -351,7 +352,13 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul fbuffer_clear(json->fbuffer); fbuffer_append(json->fbuffer, json->memo, len); fbuffer_append_char(json->fbuffer, '\0'); - *result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1)); + if (NIL_P(json->decimal_class)) { + *result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1)); + } else { + VALUE text; + text = rb_str_new2(FBUFFER_PTR(json->fbuffer)); + *result = rb_funcall(json->decimal_class, i_new, 1, text); + } return p + 1; } else { return NULL; @@ -684,6 +691,12 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } else { json->array_class = Qnil; } + tmp = ID2SYM(i_decimal_class); + if (option_given_p(opts, tmp)) { + json->decimal_class = rb_hash_aref(opts, tmp); + } else { + json->decimal_class = Qnil; + } tmp = ID2SYM(i_match_string); if (option_given_p(opts, tmp)) { VALUE match_string = rb_hash_aref(opts, tmp); @@ -701,6 +714,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->create_id = rb_funcall(mJSON, i_create_id, 0); json->object_class = Qnil; json->array_class = Qnil; + json->decimal_class = Qnil; } source = convert_encoding(StringValue(source)); StringValue(source); @@ -760,6 +774,7 @@ static void JSON_mark(void *ptr) rb_gc_mark_maybe(json->create_id); rb_gc_mark_maybe(json->object_class); rb_gc_mark_maybe(json->array_class); + rb_gc_mark_maybe(json->decimal_class); rb_gc_mark_maybe(json->match_string); } @@ -834,6 +849,7 @@ void Init_parser(void) i_symbolize_names = rb_intern("symbolize_names"); i_object_class = rb_intern("object_class"); i_array_class = rb_intern("array_class"); + i_decimal_class = rb_intern("decimal_class"); i_match = rb_intern("match"); i_match_string = rb_intern("match_string"); i_key_p = rb_intern("key?"); @@ -841,6 +857,7 @@ void Init_parser(void) i_aset = rb_intern("[]="); i_aref = rb_intern("[]"); i_leftshift = rb_intern("<<"); + i_new = rb_intern("new"); } /* diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb index 40ad500cb6..f36e9c84e1 100644 --- a/test/json/json_parser_test.rb +++ b/test/json/json_parser_test.rb @@ -108,6 +108,11 @@ class JSONParserTest < Test::Unit::TestCase assert_equal -1.0/0, parse('-Infinity', :allow_nan => true) end + def test_parse_bigdecimals + assert_equal(BigDecimal, JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"].class) + assert_equal(BigDecimal.new("0.901234567890123456789E1"),JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"] ) + end + if Array.method_defined?(:permutation) def test_parse_more_complex_arrays a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] -- cgit v1.2.3