Skip to content

Commit 01255f1

Browse files
committed
Fix for bigdecimal updates
`BigDecimal.new` is no longer available from bigdecimal-1.4.0.
1 parent bc96643 commit 01255f1

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

ext/json/ext/parser/parser.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
9191

9292
static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
9393
static VALUE CNaN, CInfinity, CMinusInfinity;
94+
static VALUE cBigDecimal = Qundef;
9495

9596
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
9697
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
9798
i_object_class, i_array_class, i_decimal_class, i_key_p,
9899
i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
99-
i_leftshift, i_new;
100+
i_leftshift, i_new, i_BigDecimal;
100101

101102

102103
#line 125 "parser.rl"
@@ -985,6 +986,18 @@ enum {JSON_float_en_main = 1};
985986
#line 340 "parser.rl"
986987

987988

989+
static int is_bigdecimal_class(VALUE obj)
990+
{
991+
if (cBigDecimal == Qundef) {
992+
if (rb_const_defined(rb_cObject, i_BigDecimal)) {
993+
cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal);
994+
} else {
995+
return 0;
996+
}
997+
}
998+
return obj == cBigDecimal;
999+
}
1000+
9881001
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
9891002
{
9901003
int cs = EVIL;
@@ -1135,9 +1148,13 @@ case 7:
11351148
*result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
11361149
}
11371150
else {
1138-
VALUE text;
1139-
text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
1140-
*result = rb_funcall(json->decimal_class, i_new, 1, text);
1151+
VALUE text;
1152+
text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
1153+
if (is_bigdecimal_class(json->decimal_class)) {
1154+
*result = rb_funcall(Qnil, i_BigDecimal, 1, text);
1155+
} else {
1156+
*result = rb_funcall(json->decimal_class, i_new, 1, text);
1157+
}
11411158
}
11421159
return p + 1;
11431160
} else {

ext/json/ext/parser/parser.rl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
8989

9090
static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
9191
static VALUE CNaN, CInfinity, CMinusInfinity;
92+
static VALUE cBigDecimal = Qundef;
9293

9394
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
9495
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
9596
i_object_class, i_array_class, i_decimal_class, i_key_p,
9697
i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
97-
i_leftshift, i_new;
98+
i_leftshift, i_new, i_BigDecimal;
9899

99100
%%{
100101
machine JSON_common;
@@ -339,6 +340,19 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
339340
) (^[0-9Ee.\-]? @exit );
340341
}%%
341342

343+
static int is_bigdecimal_class(VALUE obj)
344+
{
345+
if (cBigDecimal == Qundef) {
346+
if (rb_const_defined(rb_cObject, i_BigDecimal)) {
347+
cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal);
348+
}
349+
else {
350+
return 0;
351+
}
352+
}
353+
return obj == cBigDecimal;
354+
}
355+
342356
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
343357
{
344358
int cs = EVIL;
@@ -358,7 +372,11 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
358372
else {
359373
VALUE text;
360374
text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
361-
*result = rb_funcall(json->decimal_class, i_new, 1, text);
375+
if (is_bigdecimal_class(json->decimal_class)) {
376+
*result = rb_funcall(Qnil, i_BigDecimal, 1, text);
377+
} else {
378+
*result = rb_funcall(json->decimal_class, i_new, 1, text);
379+
}
362380
}
363381
return p + 1;
364382
} else {

0 commit comments

Comments
 (0)