JSON is a JSON parser. This module when defined by the Oj module is a faster replacement for the original.
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.
@param [String|Hash|Array] obj object to convert @param [Hash] opts same options as either generate or parse
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;
}
Sets the create_id tag to look for in JSON document. That key triggers the creation of a class with the same name.
@param [nil|String] id new create_id @return the id
static VALUE
mimic_create_id(VALUE self, VALUE id) {
Check_Type(id, T_STRING);
if (0 != oj_default_options.create_id) {
if (json_class != oj_default_options.create_id) {
xfree((char*)oj_default_options.create_id);
}
oj_default_options.create_id = 0;
}
if (Qnil != id) {
size_t len = RSTRING_LEN(id) + 1;
oj_default_options.create_id = ALLOC_N(char, len);
strcpy((char*)oj_default_options.create_id, StringValuePtr(id));
}
return id;
}
Encodes an object as a JSON String.
@param [Object] obj object to convert to encode as JSON @param [IO] anIO an IO that allows writing @param [Fixnum] limit ignored
static VALUE
mimic_dump(int argc, VALUE *argv, VALUE self) {
char *json;
struct _Options copts = oj_default_options;
VALUE rstr;
if (0 == (json = oj_write_obj_to_str(*argv, &copts))) {
rb_raise(rb_eNoMemError, "Not enough memory.");
}
rstr = rb_str_new2(json);
#if HAS_ENCODING_SUPPORT
rb_enc_associate(rstr, oj_utf8_encoding);
#endif
if (2 <= argc && Qnil != argv[1]) {
VALUE io = argv[1];
VALUE args[1];
*args = rstr;
rb_funcall2(io, oj_write_id, 1, args);
rstr = io;
}
xfree(json);
return rstr;
}
static VALUE
mimic_generate(int argc, VALUE *argv, VALUE self) {
struct _Options copts = oj_default_options;
return mimic_generate_core(argc, argv, &copts);
}
static VALUE
mimic_generate(int argc, VALUE *argv, VALUE self) {
struct _Options copts = oj_default_options;
return mimic_generate_core(argc, argv, &copts);
}
Encode obj as a JSON String. The obj argument must be a Hash, Array, or respond to to_h or to_json. Options other than those listed such as :allow_nan or :max_nesting are ignored.
@param [Object|Hash|Array] obj object to convert to a JSON String @param [Hash] opts options @param [String] :indent String to use for indentation @param [String] :space String placed after a , or : delimiter @param [String] :space_before String placed before a : delimiter @param [String] :object_nl String placed after a JSON object @param [String] :array_nl String placed after a JSON array
static VALUE
mimic_generate(int argc, VALUE *argv, VALUE self) {
struct _Options copts = oj_default_options;
return mimic_generate_core(argc, argv, &copts);
}
Does nothing other than provide compatibiltiy. @param [Object] generator ignored
static VALUE
no_op1(VALUE self, VALUE obj) {
return Qnil;
}
Loads a Ruby Object from a JSON source that can be either a String or an IO. If Proc is given or a block is providedit is called with each nested element of the loaded Object.
@param [String|IO] source JSON source @param [Proc] proc to yield to on each element or nil
static VALUE
mimic_load(int argc, VALUE *argv, VALUE self) {
VALUE obj = load(1, argv, self);
VALUE p = Qnil;
if (2 <= argc) {
p = argv[1];
}
mimic_walk(Qnil, obj, p);
return obj;
}
Parses a JSON String or IO into a Ruby Object. Options other than those listed such as :allow_nan or :max_nesting are ignored. :object_class and :array_object are not supported.
@param [String|IO] source source to parse @param [Hash] opts options @param [true|false] :symbolize_names flag indicating JSON object keys should be Symbols instead of Strings @param [true|false] :create_additions flag indicating a key matching create_id in a JSON object should trigger the creation of Ruby Object @see create_id=
static VALUE
mimic_parse(int argc, VALUE *argv, VALUE self) {
struct _Options options = oj_default_options;
if (1 > argc) {
rb_raise(rb_eArgError, "Wrong number of arguments to parse().");
}
if (2 <= argc && Qnil != argv[1]) {
VALUE ropts = argv[1];
VALUE v;
if (T_HASH != rb_type(ropts)) {
rb_raise(rb_eArgError, "options must be a hash.");
}
if (Qnil != (v = rb_hash_lookup(ropts, symbolize_names_sym))) {
options.sym_key = (Qtrue == v) ? Yes : No;
}
if (Qnil != (v = rb_hash_lookup(ropts, create_additions_sym))) {
options.mode = (Qtrue == v) ? CompatMode : StrictMode;
}
// :allow_nan is not supported as Oj always allows nan
// :max_nesting is always set to 100
// :object_class is always Hash
// :array_class is always Array
}
return load_with_opts(*argv, &options);
}
static VALUE
mimic_parse(int argc, VALUE *argv, VALUE self) {
struct _Options options = oj_default_options;
if (1 > argc) {
rb_raise(rb_eArgError, "Wrong number of arguments to parse().");
}
if (2 <= argc && Qnil != argv[1]) {
VALUE ropts = argv[1];
VALUE v;
if (T_HASH != rb_type(ropts)) {
rb_raise(rb_eArgError, "options must be a hash.");
}
if (Qnil != (v = rb_hash_lookup(ropts, symbolize_names_sym))) {
options.sym_key = (Qtrue == v) ? Yes : No;
}
if (Qnil != (v = rb_hash_lookup(ropts, create_additions_sym))) {
options.mode = (Qtrue == v) ? CompatMode : StrictMode;
}
// :allow_nan is not supported as Oj always allows nan
// :max_nesting is always set to 100
// :object_class is always Hash
// :array_class is always Array
}
return load_with_opts(*argv, &options);
}
Does nothing other than provide compatibiltiy. @param [Object] parser ignored
static VALUE
no_op1(VALUE self, VALUE obj) {
return Qnil;
}
static VALUE
mimic_pretty_generate(int argc, VALUE *argv, VALUE self) {
struct _Options copts = oj_default_options;
struct _DumpOpts dump_opts;
dump_opts.indent = " ";
dump_opts.indent_size = (uint8_t)strlen(dump_opts.indent);
dump_opts.before_sep = " ";
dump_opts.before_size = (uint8_t)strlen(dump_opts.before_sep);
dump_opts.after_sep = " ";
dump_opts.after_size = (uint8_t)strlen(dump_opts.after_sep);
dump_opts.hash_nl = "\n";
dump_opts.hash_size = (uint8_t)strlen(dump_opts.hash_nl);
dump_opts.array_nl = "\n";
dump_opts.array_size = (uint8_t)strlen(dump_opts.array_nl);
copts.dump_opts = &dump_opts;
return mimic_generate_core(argc, argv, &copts);
}
static VALUE
mimic_pretty_generate(int argc, VALUE *argv, VALUE self) {
struct _Options copts = oj_default_options;
struct _DumpOpts dump_opts;
dump_opts.indent = " ";
dump_opts.indent_size = (uint8_t)strlen(dump_opts.indent);
dump_opts.before_sep = " ";
dump_opts.before_size = (uint8_t)strlen(dump_opts.before_sep);
dump_opts.after_sep = " ";
dump_opts.after_size = (uint8_t)strlen(dump_opts.after_sep);
dump_opts.hash_nl = "\n";
dump_opts.hash_size = (uint8_t)strlen(dump_opts.hash_nl);
dump_opts.array_nl = "\n";
dump_opts.array_size = (uint8_t)strlen(dump_opts.array_nl);
copts.dump_opts = &dump_opts;
return mimic_generate_core(argc, argv, &copts);
}
Yields to the proc for every element in the obj recursivly.
@param [Hash|Array] obj object to walk @param [Proc] proc to yield to on each element
static VALUE
mimic_recurse_proc(VALUE self, VALUE obj) {
rb_need_block();
mimic_walk(Qnil, obj, Qnil);
return Qnil;
}
Loads a Ruby Object from a JSON source that can be either a String or an IO. If Proc is given or a block is providedit is called with each nested element of the loaded Object.
@param [String|IO] source JSON source @param [Proc] proc to yield to on each element or nil
static VALUE
mimic_load(int argc, VALUE *argv, VALUE self) {
VALUE obj = load(1, argv, self);
VALUE p = Qnil;
if (2 <= argc) {
p = argv[1];
}
mimic_walk(Qnil, obj, p);
return obj;
}
Generated with the Darkfish Rdoc Generator 2.