Skip to content

Commit

Permalink
NaN and BigDecimal changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Oct 23, 2014
1 parent 4667cb7 commit 98a295e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Follow [@peterohler on Twitter](http://twitter.com/#!/peterohler) for announceme
### Current Release 2.10.5

- Restricted dump to not dump NaN nor Infinity but instead raise an exception.

- Changed compat mode so that the :bigdecimal_as_decimal option over-rides the
to_json method if the option is true. The default for mimic_JSON is to leave
the option off.

### Release 2.10.4

Expand Down
15 changes: 8 additions & 7 deletions ext/oj/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1178,13 +1178,20 @@ dump_data_null(VALUE obj, Out out) {

static void
dump_data_comp(VALUE obj, int depth, Out out) {
VALUE clas = rb_obj_class(obj);

if (rb_respond_to(obj, oj_to_hash_id)) {
volatile VALUE h = rb_funcall(obj, oj_to_hash_id, 0);

if (T_HASH != rb_type(h)) {
rb_raise(rb_eTypeError, "%s.to_hash() did not return a Hash.\n", rb_class2name(rb_obj_class(obj)));
}
dump_hash(h, Qundef, depth, out->opts->mode, out);

} else if (Yes == out->opts->bigdec_as_num && oj_bigdecimal_class == clas) {
volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);

dump_raw(rb_string_value_ptr((VALUE*)&rstr), RSTRING_LEN(rstr), out);
} else if (Yes == out->opts->to_json && rb_respond_to(obj, oj_as_json_id)) {
volatile VALUE aj = rb_funcall(obj, oj_as_json_id, 0);

Expand Down Expand Up @@ -1213,8 +1220,6 @@ dump_data_comp(VALUE obj, int depth, Out out) {
out->cur += len;
*out->cur = '\0';
} else {
VALUE clas = rb_obj_class(obj);

if (rb_cTime == clas) {
switch (out->opts->time_format) {
case RubyTime: dump_ruby_time(obj, out); break;
Expand All @@ -1225,11 +1230,7 @@ dump_data_comp(VALUE obj, int depth, Out out) {
} else if (oj_bigdecimal_class == clas) {
volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);

if (Yes == out->opts->bigdec_as_num) {
dump_raw(rb_string_value_ptr((VALUE*)&rstr), RSTRING_LEN(rstr), out);
} else {
dump_cstr(rb_string_value_ptr((VALUE*)&rstr), RSTRING_LEN(rstr), 0, 0, out);
}
dump_cstr(rb_string_value_ptr((VALUE*)&rstr), RSTRING_LEN(rstr), 0, 0, out);
} else {
volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);

Expand Down
2 changes: 1 addition & 1 deletion ext/oj/oj.c
Original file line number Diff line number Diff line change
Expand Up @@ -1717,7 +1717,7 @@ static struct _Options mimic_object_to_json_options = {
CompatMode, // mode
No, // class_cache
RubyTime, // time_format
Yes, // bigdec_as_num
No, // bigdec_as_num
AutoDec, // bigdec_load
No, // to_json
Yes, // nilnil
Expand Down
2 changes: 1 addition & 1 deletion test/test_various.rb
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ def test_bigdecimal_compat_as_json
BigDecimal.send(:define_method, :as_json) do
%{this is big}
end
json = Oj.dump(orig, :mode => :compat)
json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => false)
bg = Oj.load(json, :mode => :compat)
assert_equal("this is big", bg)
BigDecimal.send(:remove_method, :as_json) # cleanup
Expand Down

0 comments on commit 98a295e

Please sign in to comment.