ext/oj/mimic_json.c in oj-3.10.0 vs ext/oj/mimic_json.c in oj-3.10.1
- old
+ new
@@ -197,10 +197,11 @@
mimic_dump(int argc, VALUE *argv, VALUE self) {
char buf[4096];
struct _out out;
struct _options copts = oj_default_options;
VALUE rstr;
+ VALUE active_hack[1];
copts.str_rx.head = NULL;
copts.str_rx.tail = NULL;
out.buf = buf;
out.end = buf + sizeof(buf) - 10;
@@ -214,10 +215,11 @@
rb_raise(rb_eTypeError, "nil not allowed.");
}
*/
copts.dump_opts.max_depth = MAX_DEPTH; // when using dump there is no limit
out.omit_nil = copts.dump_opts.omit_nil;
+
if (2 <= argc) {
int limit;
// The json gem take a more liberal approach to optional
// arguments. Expected are (obj, anIO=nil, limit=nil) yet the io
@@ -228,10 +230,18 @@
}
if (3 <= argc && 0 <= (limit = mimic_limit_arg(argv[2]))) {
copts.dump_opts.max_depth = limit;
}
}
- oj_dump_obj_to_json(*argv, &copts, &out);
+ // ActiveSupport in active_support/core_ext/object/json.rb check the
+ // optional argument type to to_json and it the argument is a
+ // ::JSON::State it calls the JSON gem code otherwise it calls the active
+ // support encoder code. To make sure the desired branch is called a
+ // default ::JSON::State argument is passed in. Basically a hack to get
+ // around the active support hack so two wrongs make a right this time.
+ active_hack[0] = rb_funcall(state_class, oj_new_id, 0);
+ oj_dump_obj_to_json_using_params(*argv, &copts, &out, 1, active_hack);
+
if (0 == out.buf) {
rb_raise(rb_eNoMemError, "Not enough memory.");
}
rstr = rb_str_new2(out.buf);
rstr = oj_encode(rstr);