Method: Oj.load

Defined in:
ext/oj/oj.c

.load(json, options) { ... } ⇒ Object

Parses a JSON document String into a Object, Hash, Array, String, Fixnum, Float, true, false, or nil according to the default mode or the mode specified. Raises an exception if the JSON is malformed or the classes specified are not valid. If the string input is not a valid JSON document (an empty string is not a valid JSON document) an exception is raised.

When used with a document that has multiple JSON elements the block, if any, will be yielded to. If no block then the last element read will be returned.

This parser operates on string and will attempt to load files into memory if a file object is passed as the first argument. A stream input will be parsed using a stream parser but others use the slightly faster string parser.

A block can be provided with a single argument. That argument will be the parsed JSON document. This is useful when parsing a string that includes multiple JSON documents. The block can take up to 3 arguments, the parsed object, the position in the string or stream of the start of the JSON for that object, and the length of the JSON for that object plus trailing whitespace.

  • json [String|IO] JSON String or an Object that responds to read()

  • options [Hash] load options (same as default_options)

  • obj [Hash|Array|String|Fixnum|Float|Boolean|nil] parsed object.

  • start [_optional, Integer] start position of parsed JSON for obj.

  • len [_optional, Integer] length of parsed JSON for obj.

Returns [Hash|Array|String|Fixnum|Float|Boolean|nil]

Yields:



1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# File 'ext/oj/oj.c', line 1064

static VALUE load(int argc, VALUE *argv, VALUE self) {
    Mode mode = oj_default_options.mode;

    if (1 > argc) {
        rb_raise(rb_eArgError, "Wrong number of arguments to load().");
    }
    if (2 <= argc) {
        VALUE ropts = argv[1];
        VALUE v;

        if (Qnil != ropts || CompatMode != mode) {
            Check_Type(ropts, T_HASH);
            if (Qnil != (v = rb_hash_lookup(ropts, mode_sym))) {
                if (object_sym == v) {
                    mode = ObjectMode;
                } else if (strict_sym == v) {
                    mode = StrictMode;
                } else if (compat_sym == v || json_sym == v) {
                    mode = CompatMode;
                } else if (null_sym == v) {
                    mode = NullMode;
                } else if (custom_sym == v) {
                    mode = CustomMode;
                } else if (rails_sym == v) {
                    mode = RailsMode;
                } else if (wab_sym == v) {
                    mode = WabMode;
                } else {
                    rb_raise(rb_eArgError,
                             ":mode must be :object, :strict, :compat, :null, :custom, :rails, or "
                             ":wab.");
                }
            }
        }
    }
    switch (mode) {
    case StrictMode:
    case NullMode: return oj_strict_parse(argc, argv, self);
    case CompatMode:
    case RailsMode: return oj_compat_parse(argc, argv, self);
    case CustomMode: return oj_custom_parse(argc, argv, self);
    case WabMode: return oj_wab_parse(argc, argv, self);
    case ObjectMode:
    default: break;
    }
    return oj_object_parse(argc, argv, self);
}