class Object

Public Class Methods

[](obj, opts={}) click to toggle source

If the obj argument is a String then it is assumed to be a JSON String and parsed otherwise the obj is encoded as a JSON String.

  • obj [String|Hash|Array] object to convert

  • opts [Hash] same options as either generate or parse

Returns [Object]

static VALUE
mimic_dump_load(int argc, VALUE *argv, VALUE self) {
    if (1 > argc) {
        rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
    } else if (T_STRING == rb_type(*argv)) {
        return mimic_load(argc, argv, self);
    } else {
        return mimic_dump(argc, argv, self);
    }
    return Qnil;
}

Public Instance Methods

to_json(*args) click to toggle source
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;
    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);
    }
    // To be strict the mimic_object_to_json_options should be used but people
    // seem to prefer the option of changing that.
    //oj_dump_obj_to_json(self, &mimic_object_to_json_options, &out);
    oj_dump_obj_to_json_using_params(self, &copts, &out, argc, argv);
    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;
}