Method: Oj.dump

Defined in:
ext/oj/oj.c

.dump(obj, options) ⇒ Object

Dumps an Object (obj) to a string.

  • obj [Object] Object to serialize as an JSON document String

  • options [Hash] same as default_options



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'ext/oj/oj.c', line 942

static VALUE
dump(int argc, VALUE *argv, VALUE self) {
    char    buf[4096];
    struct _Out   out;
    struct _Options copts = oj_default_options;
    VALUE   rstr;

    if (1 > argc) {
  rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
    }
    if (2 == argc) {
  oj_parse_options(argv[1], &copts);
    }
    if (CompatMode == copts.mode) {
  copts.dump_opts.nan_dump = true;
    }
    out.buf = buf;
    out.end = buf + sizeof(buf) - 10;
    out.allocated = 0;
    out.omit_nil = copts.dump_opts.omit_nil;
    out.caller = CALLER_DUMP;
    oj_dump_obj_to_json(*argv, &copts, &out);
    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);
    }
    return rstr;
}