@@ -89,13 +89,12 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
8989
9090static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
9191static VALUE CNaN, CInfinity, CMinusInfinity;
92- static VALUE cBigDecimal = Qundef;
9392
9493static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
9594 i_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
9695 i_object_class, i_array_class, i_decimal_class, i_key_p,
9796 i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
98- i_leftshift, i_new, i_BigDecimal , i_freeze, i_uminus;
97+ i_leftshift, i_new, i_try_convert , i_freeze, i_uminus;
9998
10099%%{
101100 machine JSON_common;
@@ -345,19 +344,6 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
345344 ) (^[0-9Ee.\-]? @exit );
346345}%%
347346
348- static int is_bigdecimal_class(VALUE obj)
349- {
350- if (cBigDecimal == Qundef) {
351- if (rb_const_defined(rb_cObject, i_BigDecimal)) {
352- cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal);
353- }
354- else {
355- return 0;
356- }
357- }
358- return obj == cBigDecimal;
359- }
360-
361347static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
362348{
363349 int cs = EVIL;
@@ -367,21 +353,46 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
367353 %% write exec;
368354
369355 if (cs >= JSON_float_first_final) {
356+ VALUE mod = Qnil;
357+ ID method_id = 0;
358+ if (rb_respond_to(json->decimal_class, i_try_convert)) {
359+ mod = json->decimal_class;
360+ method_id = i_try_convert;
361+ } else if (rb_respond_to(json->decimal_class, i_new)) {
362+ mod = json->decimal_class;
363+ method_id = i_new;
364+ } else if (RB_TYPE_P(json->decimal_class, T_CLASS)) {
365+ VALUE name = rb_class_name(json->decimal_class);
366+ const char *name_cstr = RSTRING_PTR(name);
367+ const char *last_colon = strrchr(name_cstr, ':');
368+ if (last_colon) {
369+ const char *mod_path_end = last_colon - 1;
370+ VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
371+ mod = rb_path_to_class(mod_path);
372+
373+ const char *method_name_beg = last_colon + 1;
374+ long before_len = method_name_beg - name_cstr;
375+ long len = RSTRING_LEN(name) - before_len;
376+ VALUE method_name = rb_str_substr(name, before_len, len);
377+ method_id = SYM2ID(rb_str_intern(method_name));
378+ } else {
379+ mod = rb_mKernel;
380+ method_id = SYM2ID(rb_str_intern(name));
381+ }
382+ }
383+
370384 long len = p - json->memo;
371385 fbuffer_clear(json->fbuffer);
372386 fbuffer_append(json->fbuffer, json->memo, len);
373387 fbuffer_append_char(json->fbuffer, '\0');
374- if (NIL_P(json->decimal_class)) {
375- *result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
388+
389+ if (method_id) {
390+ VALUE text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
391+ *result = rb_funcallv(mod, method_id, 1, &text);
376392 } else {
377- VALUE text;
378- text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
379- if (is_bigdecimal_class(json->decimal_class)) {
380- *result = rb_funcall(Qnil, i_BigDecimal, 1, text);
381- } else {
382- *result = rb_funcall(json->decimal_class, i_new, 1, text);
383- }
393+ *result = DBL2NUM(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
384394 }
395+
385396 return p + 1;
386397 } else {
387398 return NULL;
@@ -868,6 +879,10 @@ static VALUE cParser_source(VALUE self)
868879
869880void Init_parser(void)
870881{
882+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
883+ rb_ext_ractor_safe(true);
884+ #endif
885+
871886#undef rb_intern
872887 rb_require("json/common");
873888 mJSON = rb_define_module("JSON");
@@ -910,7 +925,7 @@ void Init_parser(void)
910925 i_aref = rb_intern("[]");
911926 i_leftshift = rb_intern("<<");
912927 i_new = rb_intern("new");
913- i_BigDecimal = rb_intern("BigDecimal ");
928+ i_try_convert = rb_intern("try_convert ");
914929 i_freeze = rb_intern("freeze");
915930 i_uminus = rb_intern("-@");
916931}
0 commit comments