ext/json/ext/parser/parser.rl in json_pure-2.1.0 vs ext/json/ext/parser/parser.rl in json_pure-2.2.0
- old
+ new
@@ -87,16 +87,17 @@
return len;
}
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;
cr = '\n';
@@ -337,10 +338,23 @@
(('0' | [1-9][0-9]*) '.' [0-9]+ ([Ee] [+\-]?[0-9]+)?)
| (('0' | [1-9][0-9]*) ([Ee] [+\-]?[0-9]+))
) (^[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;
%% write init;
@@ -355,11 +369,15 @@
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);
+ 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 {
return NULL;
}
@@ -552,11 +570,13 @@
}
if (json->symbolize_names && json->parsing_name) {
*result = rb_str_intern(*result);
} else {
- rb_str_resize(*result, RSTRING_LEN(*result));
+ if (RB_TYPE_P(*result, T_STRING)) {
+ rb_str_resize(*result, RSTRING_LEN(*result));
+ }
}
if (cs >= JSON_string_first_final) {
return p + 1;
} else {
return NULL;
@@ -822,10 +842,11 @@
return rb_str_dup(json->Vsource);
}
void Init_parser(void)
{
+#undef rb_intern
rb_require("json/common");
mJSON = rb_define_module("JSON");
mExt = rb_define_module_under(mJSON, "Ext");
cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
eParserError = rb_path2class("JSON::ParserError");
@@ -856,9 +877,10 @@
i_deep_const_get = rb_intern("deep_const_get");
i_aset = rb_intern("[]=");
i_aref = rb_intern("[]");
i_leftshift = rb_intern("<<");
i_new = rb_intern("new");
+ i_BigDecimal = rb_intern("BigDecimal");
}
/*
* Local variables:
* mode: c