Method: Oj.load_file
- Defined in:
- ext/oj/oj.c
.load_file(path, 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.
If the input file is not a valid JSON document (an empty file is not a valid JSON document) an exception is raised.
This is a stream based parser which allows a large or huge file to be loaded without pulling the whole file into memory.
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.
-
path [String] to a file containing a JSON document
-
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 [Object|Hash|Array|String|Fixnum|Float|Boolean|nil]
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 |
# File 'ext/oj/oj.c', line 1143
static VALUE load_file(int argc, VALUE *argv, VALUE self) {
char *path;
int fd;
Mode mode = oj_default_options.mode;
struct _parseInfo pi;
if (1 > argc) {
rb_raise(rb_eArgError, "Wrong number of arguments to load().");
}
path = StringValuePtr(*argv);
parse_info_init(&pi);
pi.options = oj_default_options;
pi.handler = Qnil;
pi.err_class = Qnil;
pi.max_depth = 0;
if (2 <= argc) {
VALUE ropts = argv[1];
VALUE v;
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.");
}
}
}
#ifdef _WIN32
{
WCHAR *wide_path;
wide_path = rb_w32_mbstr_to_wstr(CP_UTF8, path, -1, NULL);
fd = rb_w32_wopen(wide_path, O_RDONLY);
OJ_FREE(wide_path);
}
#else
fd = open(path, O_RDONLY);
#endif
if (0 == fd) {
rb_raise(rb_eIOError, "%s", strerror(errno));
}
switch (mode) {
case StrictMode:
case NullMode: oj_set_strict_callbacks(&pi); return oj_pi_sparse(argc, argv, &pi, fd);
case CustomMode: oj_set_custom_callbacks(&pi); return oj_pi_sparse(argc, argv, &pi, fd);
case CompatMode:
case RailsMode: oj_set_compat_callbacks(&pi); return oj_pi_sparse(argc, argv, &pi, fd);
case WabMode: oj_set_wab_callbacks(&pi); return oj_pi_sparse(argc, argv, &pi, fd);
case ObjectMode:
default: break;
}
oj_set_object_callbacks(&pi);
return oj_pi_sparse(argc, argv, &pi, fd);
}
|