ext/oj/mimic_json.c in oj-3.13.11 vs ext/oj/mimic_json.c in oj-3.13.12
- old
+ new
@@ -196,21 +196,20 @@
* - *limit* [_Fixnum_] ignored
*
* Returns [_String_] a JSON string.
*/
static VALUE 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;
- out.allocated = false;
+
+ oj_out_init(&out);
+
out.caller = CALLER_DUMP;
copts.escape_mode = JXEsc;
copts.mode = CompatMode;
/* seems like this is not correct
@@ -255,13 +254,13 @@
*args = rstr;
rb_funcall2(io, oj_write_id, 1, args);
rstr = io;
}
- if (out.allocated) {
- xfree(out.buf);
- }
+
+ oj_out_free(&out);
+
return rstr;
}
// This is the signature for the hash_foreach callback also.
static int mimic_walk(VALUE key, VALUE obj, VALUE proc) {
@@ -356,19 +355,20 @@
}
return Qnil;
}
static VALUE mimic_generate_core(int argc, VALUE *argv, Options copts) {
- char buf[4096];
struct _out out;
VALUE rstr;
- memset(buf, 0, sizeof(buf));
+ if (0 == argc) {
+ rb_raise(rb_eArgError, "wrong number of arguments (0))");
+ }
+ memset(out.stack_buffer, 0, sizeof(out.stack_buffer));
- out.buf = buf;
- out.end = buf + sizeof(buf) - 10;
- out.allocated = false;
+ oj_out_init(&out);
+
out.omit_nil = copts->dump_opts.omit_nil;
out.caller = CALLER_GENERATE;
// For obj.to_json or generate nan is not allowed but if called from dump
// it is.
copts->dump_opts.nan_dump = RaiseNan;
@@ -396,13 +396,13 @@
if (0 == out.buf) {
rb_raise(rb_eNoMemError, "Not enough memory.");
}
rstr = rb_str_new2(out.buf);
rstr = oj_encode(rstr);
- if (out.allocated) {
- xfree(out.buf);
- }
+
+ oj_out_free(&out);
+
return rstr;
}
/* Document-method: fast_generate
* call-seq: fast_generate(obj, opts=nil)
@@ -455,13 +455,16 @@
// Some (all?) json gem to_json methods need a State instance and not just
// a Hash. I haven't dug deep enough to find out why but using a State
// instance and not a Hash gives the desired behavior.
*rargs = *argv;
+ if (0 == argc) {
+ rb_raise(rb_eArgError, "wrong number of arguments (0))");
+ }
if (1 == argc) {
h = rb_hash_new();
- } else {
+ } else {
h = argv[1];
}
if (!oj_hash_has_key(h, oj_indent_sym)) {
rb_hash_aset(h, oj_indent_sym, rb_str_new2(" "));
}
@@ -738,20 +741,19 @@
NULL, // tail
{'\0'}, // err
}};
static VALUE mimic_object_to_json(int argc, VALUE *argv, VALUE self) {
- char buf[4096];
struct _out out;
VALUE rstr;
struct _options copts = oj_default_options;
copts.str_rx.head = NULL;
copts.str_rx.tail = NULL;
- out.buf = buf;
- out.end = buf + sizeof(buf) - 10;
- out.allocated = false;
+
+ oj_out_init(&out);
+
out.omit_nil = copts.dump_opts.omit_nil;
copts.mode = CompatMode;
copts.to_json = No;
if (1 <= argc && Qnil != argv[0]) {
oj_parse_mimic_dump_options(argv[0], &copts);
@@ -763,12 +765,12 @@
if (NULL == out.buf) {
rb_raise(rb_eNoMemError, "Not enough memory.");
}
rstr = rb_str_new2(out.buf);
rstr = oj_encode(rstr);
- if (out.allocated) {
- xfree(out.buf);
- }
+
+ oj_out_free(&out);
+
return rstr;
}
/* Document-method: state
* call-seq: state()