Skip to content

Commit 8e1489d

Browse files
committed
Use Ruby's to_s for floats
This matches JSON's behaviour exactly. I'll look at ways to regain the speed later.
1 parent 0d2af51 commit 8e1489d

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

ext/rapidjson/encoder.hh

+6-13
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,12 @@ class RubyObjectEncoder {
122122
}
123123
}
124124
} else {
125-
// HACK: for standard notation we can use the RapidJSON
126-
// implementation, but for values we need scientific notation we
127-
// will fallback to Ruby's to_s so that we match JSON's output
128-
// exactly.
129-
//
130-
// Ideally we would just implement both
131-
if (f <= -1.0e15 || f >= 1.0e15 || (f >= -0.0001 && f <= 0.0001)) {
132-
VALUE str = rb_funcall(v, rb_intern("to_s"), 0);
133-
Check_Type(str, T_STRING);
134-
encode_raw_json_str(str);
135-
} else {
136-
writer.Double(f);
137-
}
125+
// TODO: We should avoid relying on to_s and do this conversion
126+
// ourselves. However it's difficult to get the exact same rounding
127+
// and truncation that Ruby uses.
128+
VALUE str = rb_funcall(v, rb_intern("to_s"), 0);
129+
Check_Type(str, T_STRING);
130+
encode_raw_json_str(str);
138131
}
139132
}
140133

0 commit comments

Comments
 (0)