module JSON
JSON is a JSON parser. This module when defined by the Oj module is a faster replacement for the original.
Constants
- ParserError
Public Class Methods
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; oj_default_options.create_id_len = 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)); oj_default_options.create_id_len = len - 1; } 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 buf[4096]; struct _Out out; struct _Options copts = oj_default_options; VALUE rstr; out.buf = buf; out.end = buf + sizeof(buf) - 10; out.allocated = 0; 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 (2 <= argc && Qnil != argv[1]) { VALUE io = argv[1]; VALUE args[1]; *args = rstr; rb_funcall2(io, oj_write_id, 1, args); rstr = io; } if (out.allocated) { xfree(out.buf); } 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) { struct _ParseInfo pi; VALUE obj; VALUE p = Qnil; pi.options = oj_default_options; oj_set_compat_callbacks(&pi); obj = oj_pi_parse(argc, argv, &pi, 0, 0, 0); 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 _ParseInfo pi; VALUE args[1]; if (argc < 1) { rb_raise(rb_eArgError, "Wrong number of arguments to parse."); } oj_set_compat_callbacks(&pi); pi.options = oj_default_options; pi.options.auto_define = No; pi.options.quirks_mode = No; if (2 <= argc) { 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))) { pi.options.sym_key = (Qtrue == v) ? Yes : No; } if (Qnil != (v = rb_hash_lookup(ropts, quirks_mode_sym))) { pi.options.quirks_mode = (Qtrue == v) ? Yes : No; } if (Qnil != (v = rb_hash_lookup(ropts, create_additions_sym))) { if (Qfalse == v) { oj_set_strict_callbacks(&pi); } } // :allow_nan is not supported as Oj always allows nan // :max_nesting is ignored as Oj has not nesting limit // :object_class is always Hash // :array_class is always Array } *args = *argv; return oj_pi_parse(1, args, &pi, 0, 0, 0); }
static VALUE mimic_parse(int argc, VALUE *argv, VALUE self) { struct _ParseInfo pi; VALUE args[1]; if (argc < 1) { rb_raise(rb_eArgError, "Wrong number of arguments to parse."); } oj_set_compat_callbacks(&pi); pi.options = oj_default_options; pi.options.auto_define = No; pi.options.quirks_mode = No; if (2 <= argc) { 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))) { pi.options.sym_key = (Qtrue == v) ? Yes : No; } if (Qnil != (v = rb_hash_lookup(ropts, quirks_mode_sym))) { pi.options.quirks_mode = (Qtrue == v) ? Yes : No; } if (Qnil != (v = rb_hash_lookup(ropts, create_additions_sym))) { if (Qfalse == v) { oj_set_strict_callbacks(&pi); } } // :allow_nan is not supported as Oj always allows nan // :max_nesting is ignored as Oj has not nesting limit // :object_class is always Hash // :array_class is always Array } *args = *argv; return oj_pi_parse(1, args, &pi, 0, 0, 0); }
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) { struct _ParseInfo pi; VALUE obj; VALUE p = Qnil; pi.options = oj_default_options; oj_set_compat_callbacks(&pi); obj = oj_pi_parse(argc, argv, &pi, 0, 0, 0); if (2 <= argc) { p = argv[1]; } mimic_walk(Qnil, obj, p); return obj; }
static VALUE mimic_generate(int argc, VALUE *argv, VALUE self) { struct _Options copts = oj_default_options; return mimic_generate_core(argc, argv, &copts); }