Skip to content

Commit

Permalink
Give up on compat mode and just use ruby string to_f
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Jul 26, 2020
1 parent c1d851b commit 3c1c896
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions ext/oj/mimic_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,8 @@ mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
pi.options.create_ok = No;
pi.options.allow_nan = (bang ? Yes : No);
pi.options.nilnil = No;
pi.options.bigdec_load = FloatDec;
//pi.options.bigdec_load = FloatDec;
pi.options.bigdec_load = RubyDec;
pi.options.mode = CompatMode;
pi.max_depth = 100;

Expand Down Expand Up @@ -686,7 +687,7 @@ static struct _options mimic_object_to_json_options = {
No, // class_cache
RubyTime, // time_format
No, // bigdec_as_num
FloatDec, // bigdec_load
RubyDec, // bigdec_load
No, // to_hash
No, // to_json
No, // as_json
Expand Down
3 changes: 2 additions & 1 deletion ext/oj/oj.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ typedef enum {
BigDec = 'b',
FloatDec = 'f',
AutoDec = 'a',
FastDec = 'F'
FastDec = 'F',
RubyDec = 'r',
} BigLoad;

typedef enum {
Expand Down
6 changes: 5 additions & 1 deletion ext/oj/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ read_num(ParseInfo pi) {
ni.nan = 0;
ni.neg = 0;
ni.has_exp = 0;
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load);
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load || RubyDec == pi->options.bigdec_load);
ni.bigdec_load = pi->options.bigdec_load;

if ('-' == *pi->cur) {
Expand Down Expand Up @@ -877,6 +877,10 @@ oj_num_as_value(NumInfo ni) {
ld = -ld;
}
rnum = rb_float_new((double)ld);
} else if (RubyDec == ni->bigdec_load) {
volatile VALUE sv = rb_str_new(ni->str, ni->len);

rnum = rb_funcall(sv, rb_intern("to_f"), 0);
} else {
char *end;
double d = strtod(ni->str, &end);
Expand Down
6 changes: 3 additions & 3 deletions ext/oj/sparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ read_num(ParseInfo pi) {
ni.nan = 0;
ni.neg = 0;
ni.has_exp = 0;
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load);
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load || RubyDec == pi->options.bigdec_load);
ni.bigdec_load = pi->options.bigdec_load;

c = reader_get(&pi->rd);
Expand Down Expand Up @@ -549,7 +549,7 @@ read_nan(ParseInfo pi) {
ni.infinity = 0;
ni.nan = 1;
ni.neg = 0;
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load);
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load || RubyDec == pi->options.bigdec_load);
ni.bigdec_load = pi->options.bigdec_load;

if ('a' != reader_get(&pi->rd) ||
Expand Down Expand Up @@ -747,7 +747,7 @@ oj_sparse2(ParseInfo pi) {
ni.infinity = 0;
ni.nan = 1;
ni.neg = 0;
ni.no_big = (FloatDec == pi->options.bigdec_load);
ni.no_big = (FloatDec == pi->options.bigdec_load || RubyDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load);
ni.bigdec_load = pi->options.bigdec_load;
add_num_value(pi, &ni);
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/test_custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_float_parse
def test_float_parse_fast
f = Oj.load("12.123456789012345678", mode: :custom, bigdecimal_load: :fast);
assert_equal(Float, f.class)
assert_equal(12.123456789012346, f)
assert_equal('12.12345678901235', "%0.14f" % [f]) # only care about 16 digits
end

def test_nan_dump
Expand Down

0 comments on commit 3c1c896

Please sign in to comment.