Skip to content

Commit fd6f86f

Browse files
authored
Merge pull request #362 from mrkn/update_for_bigdecimal
Fix for bigdecimal updates
2 parents 409f8f6 + 7469100 commit fd6f86f

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

ext/json/ext/parser/parser.c

Lines changed: 19 additions & 2 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;
@@ -1136,7 +1149,11 @@ case 7:
11361149
} else {
11371150
VALUE text;
11381151
text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
1139-
*result = rb_funcall(json->decimal_class, i_new, 1, text);
1152+
if (is_bigdecimal_class(json->decimal_class)) {
1153+
*result = rb_funcall(Qnil, i_BigDecimal, 1, text);
1154+
} else {
1155+
*result = rb_funcall(json->decimal_class, i_new, 1, text);
1156+
}
11401157
}
11411158
return p + 1;
11421159
} else {

ext/json/ext/parser/parser.rl

Lines changed: 19 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,18 @@ 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+
} else {
349+
return 0;
350+
}
351+
}
352+
return obj == cBigDecimal;
353+
}
354+
342355
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
343356
{
344357
int cs = EVIL;
@@ -357,7 +370,11 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
357370
} else {
358371
VALUE text;
359372
text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
360-
*result = rb_funcall(json->decimal_class, i_new, 1, text);
373+
if (is_bigdecimal_class(json->decimal_class)) {
374+
*result = rb_funcall(Qnil, i_BigDecimal, 1, text);
375+
} else {
376+
*result = rb_funcall(json->decimal_class, i_new, 1, text);
377+
}
361378
}
362379
return p + 1;
363380
} else {

0 commit comments

Comments
 (0)