Skip to content

Commit

Permalink
Fix for bigdecimal updates
Browse files Browse the repository at this point in the history
`BigDecimal.new` is no longer available from bigdecimal-1.4.0.
  • Loading branch information
mrkn authored and flori committed Feb 21, 2019
1 parent 409f8f6 commit ef2092f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
21 changes: 19 additions & 2 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)

static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
static VALUE CNaN, CInfinity, CMinusInfinity;
static VALUE cBigDecimal = Qundef;

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_decimal_class, i_key_p,
i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
i_leftshift, i_new;
i_leftshift, i_new, i_BigDecimal;


#line 125 "parser.rl"
Expand Down Expand Up @@ -985,6 +986,18 @@ enum {JSON_float_en_main = 1};
#line 340 "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;
}
}
return obj == cBigDecimal;
}

static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
Expand Down Expand Up @@ -1136,7 +1149,11 @@ case 7:
} else {
VALUE text;
text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
*result = rb_funcall(json->decimal_class, i_new, 1, text);
if (is_bigdecimal_class(json->decimal_class)) {
*result = rb_funcall(Qnil, i_BigDecimal, 1, text);
} else {
*result = rb_funcall(json->decimal_class, i_new, 1, text);
}
}
return p + 1;
} else {
Expand Down
21 changes: 19 additions & 2 deletions ext/json/ext/parser/parser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)

static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
static VALUE CNaN, CInfinity, CMinusInfinity;
static VALUE cBigDecimal = Qundef;

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_decimal_class, i_key_p,
i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
i_leftshift, i_new;
i_leftshift, i_new, i_BigDecimal;

%%{
machine JSON_common;
Expand Down Expand Up @@ -339,6 +340,18 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
) (^[0-9Ee.\-]? @exit );
}%%

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;
}

static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
Expand All @@ -357,7 +370,11 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
} else {
VALUE text;
text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
*result = rb_funcall(json->decimal_class, i_new, 1, text);
if (is_bigdecimal_class(json->decimal_class)) {
*result = rb_funcall(Qnil, i_BigDecimal, 1, text);
} else {
*result = rb_funcall(json->decimal_class, i_new, 1, text);
}
}
return p + 1;
} else {
Expand Down

0 comments on commit ef2092f

Please sign in to comment.