Class: Oj::Parser
- Inherits:
-
Object
- Object
- Oj::Parser
- Defined in:
- ext/oj/parser.c,
ext/oj/parser.c
Overview
A reusable parser that makes use of named delegates to determine the handling of parsed data. Delegates are available for validation, a callback parser (SAJ), and a usual delegate that builds Ruby objects as parsing proceeds.
This parser is considerably faster than the older Oj.parse call and isolates options to just the parser so that other parts of the code are not forced to use the same options.
Class Method Summary collapse
-
.new(mode = nil) ⇒ Object
Creates a new Parser with the specified mode.
- .safe(*args) ⇒ Object
-
.saj ⇒ Object
Returns the default SAJ parser.
-
.usual ⇒ Object
Returns the default usual parser.
-
.validate ⇒ Object
Returns the default validate parser.
Instance Method Summary collapse
-
#file(filename) ⇒ Object
Parse a JSON file.
-
#just_one ⇒ Object
Returns the current state of the just_one [Boolean] option.
-
#just_one=(value) ⇒ Object
Sets the just_one option which limits the parsing of a string or or stream to a single JSON element.
-
#load(reader) ⇒ Object
Parse a JSON stream.
-
#method_missing(value) ⇒ Object
Methods not handled by the parser are passed to the delegate.
-
#parse(json) ⇒ Object
Parse a JSON string.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(value) ⇒ Object
Methods not handled by the parser are passed to the delegate. The methods supported by delegate are:
-
:validate
- no options
-
:saj
- cache_keys is a flag indicating hash keys should be cached.
- cache_strings is a positive integer less than 35. Strings shorter than that length are cached.
- handler is the SAJ handler
-
:usual
- cache_keys is a flag indicating hash keys should be cached.
- cache_strings is a positive integer less than 35. Strings shorter than that length are cached.
- cache_expunge dictates when the cache will be expunged where 0 never expunges, 1 expunges slowly, 2 expunges faster, and 3 or higher expunges agressively.
- capacity is the capacity of the parser's internal stack. The parser grows automatically but can be updated directly with this call.
- create_id if non-nil is the key that is used to specify the type of object to create when parsing. Parsed JSON objects that include the specified element use the element value as the name of the class to create an object from instead of a Hash.
- decimal is the approach to how decimals are parsed. If :auto then the decimals with significant digits are 16 or less are Floats and long ones are BigDecimal. :ruby uses a call to Ruby to convert a string to a Float. :float always generates a Float. :bigdecimal always results in a BigDecimal.
- ignore_json_create is a flag that when set the class json_create method is ignored on parsing in favor of creating an instance and populating directly.
- missing_class is an indicator that determines how unknown class names are handled. Valid values are :auto which creates any missing classes on parse, :ignore which ignores and continues as a Hash (default), and :raise which raises an exception if the class is not found.
- omit_null is a flag that if true then null values in a map or object are omitted from the resulting Hash or Object.
- symbol_keys is a flag that indicates Hash keys should be parsed to Symbols versus Strings.
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 |
# File 'ext/oj/parser.c', line 1153
static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
ojParser p;
const char *key = NULL;
volatile VALUE rkey = *argv;
volatile VALUE rv = Qnil;
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
#if HAVE_RB_EXT_RACTOR_SAFE
// This doesn't seem to do anything.
rb_ext_ractor_safe(true);
#endif
switch (rb_type(rkey)) {
case RUBY_T_SYMBOL:
rkey = rb_sym2str(rkey);
// fall through
case RUBY_T_STRING: key = StringValuePtr(rkey); break;
default: rb_raise(rb_eArgError, "option method must be a symbol or string");
}
if (1 < argc) {
rv = argv[1];
}
return p->option(p, key, rv);
}
|
Class Method Details
.new(mode = nil) ⇒ Object
Creates a new Parser with the specified mode. If no mode is provided validation is assumed. Optional arguments can be provided that match the mode. For example with the :usual mode the call might look like Oj::Parser.new(:usual, cache_keys: true).
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 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 |
# File 'ext/oj/parser.c', line 1036
static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
ojParser p = OJ_R_ALLOC(struct _ojParser);
#if HAVE_RB_EXT_RACTOR_SAFE
// This doesn't seem to do anything.
rb_ext_ractor_safe(true);
#endif
memset(p, 0, sizeof(struct _ojParser));
buf_init(&p->key);
buf_init(&p->buf);
p->map = value_map;
if (argc < 1) {
oj_set_parser_validator(p);
} else {
VALUE mode = argv[0];
if (Qnil == mode) {
oj_set_parser_validator(p);
} else {
const char *ms = NULL;
switch (rb_type(mode)) {
case RUBY_T_SYMBOL:
mode = rb_sym2str(mode);
// fall through
case RUBY_T_STRING: ms = RSTRING_PTR(mode); break;
default: rb_raise(rb_eArgError, "mode must be :validate, :usual, :saj, or :object");
}
if (0 == strcmp("usual", ms) || 0 == strcmp("standard", ms) || 0 == strcmp("strict", ms) ||
0 == strcmp("compat", ms)) {
oj_set_parser_usual(p);
} else if (0 == strcmp("object", ms)) {
// TBD
} else if (0 == strcmp("saj", ms)) {
oj_set_parser_saj(p);
} else if (0 == strcmp("validate", ms)) {
oj_set_parser_validator(p);
} else if (0 == strcmp("debug", ms)) {
oj_set_parser_debug(p);
} else {
rb_raise(rb_eArgError, "mode must be :validate, :usual, :saj, or :object");
}
}
if (1 < argc) {
VALUE ropts = argv[1];
Check_Type(ropts, T_HASH);
rb_hash_foreach(ropts, opt_cb, (VALUE)p);
}
}
return TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
}
|
.safe(*args) ⇒ Object
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 |
# File 'ext/oj/parser.c', line 1429
static VALUE parser_safe(int argc, VALUE *argv, VALUE self) {
VALUE options;
if (1 == argc) {
options = argv[0];
Check_Type(options, T_HASH);
} else {
options = rb_hash_new();
}
ojParser p = OJ_R_ALLOC(struct _ojParser);
memset(p, 0, sizeof(struct _ojParser));
buf_init(&p->key);
buf_init(&p->buf);
p->map = value_map;
oj_set_parser_safe(p, options);
return TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
}
|
.saj ⇒ Object
Returns the default SAJ parser. Note the default SAJ parser can not be used concurrently in more than one thread.
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 |
# File 'ext/oj/parser.c', line 1392
static VALUE parser_saj(VALUE self) {
if (Qundef == saj_parser) {
ojParser p = OJ_R_ALLOC(struct _ojParser);
memset(p, 0, sizeof(struct _ojParser));
buf_init(&p->key);
buf_init(&p->buf);
p->map = value_map;
oj_set_parser_saj(p);
saj_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
rb_gc_register_address(&saj_parser);
}
return saj_parser;
}
|
.usual ⇒ Object
Returns the default usual parser. Note the default usual parser can not be used concurrently in more than one thread.
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 |
# File 'ext/oj/parser.c', line 1369
static VALUE parser_usual(VALUE self) {
if (Qundef == usual_parser) {
ojParser p = OJ_R_ALLOC(struct _ojParser);
memset(p, 0, sizeof(struct _ojParser));
buf_init(&p->key);
buf_init(&p->buf);
p->map = value_map;
oj_set_parser_usual(p);
usual_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
rb_gc_register_address(&usual_parser);
}
return usual_parser;
}
|
.validate ⇒ Object
Returns the default validate parser.
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 |
# File 'ext/oj/parser.c', line 1414
static VALUE parser_validate(VALUE self) {
if (Qundef == validate_parser) {
ojParser p = OJ_R_ALLOC(struct _ojParser);
memset(p, 0, sizeof(struct _ojParser));
buf_init(&p->key);
buf_init(&p->buf);
p->map = value_map;
oj_set_parser_validator(p);
validate_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
rb_gc_register_address(&validate_parser);
}
return validate_parser;
}
|
Instance Method Details
#file(filename) ⇒ Object
Parse a JSON file.
Returns the result according to the delegate of the parser.
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 |
# File 'ext/oj/parser.c', line 1286
static VALUE parser_file(VALUE self, VALUE filename) {
ojParser p;
const char *path;
int fd;
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
path = StringValuePtr(filename);
parser_reset(p);
p->start(p);
if (0 > (fd = open(path, O_RDONLY))) {
rb_raise(rb_eIOError, "error opening %s", path);
}
#if USE_THREAD_LIMIT
struct stat info;
// st_size will be 0 if not a file
if (0 == fstat(fd, &info) && USE_THREAD_LIMIT < info.st_size) {
// Use threaded version.
// TBD only if has pthreads
// TBD parse_large(p, fd);
return p->result(p);
}
#endif
byte buf[16385];
size_t size = sizeof(buf) - 1;
size_t rsize;
while (true) {
if (0 < (rsize = read(fd, buf, size))) {
buf[rsize] = '\0';
parse(p, buf, rsize, true);
}
if (rsize <= 0) {
if (0 != rsize) {
rb_raise(rb_eIOError, "error reading from %s", path);
}
break;
}
}
return p->result(p);
}
|
#just_one ⇒ Object
Returns the current state of the just_one [Boolean] option.
1335 1336 1337 1338 1339 1340 1341 |
# File 'ext/oj/parser.c', line 1335
static VALUE parser_just_one(VALUE self) {
ojParser p;
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
return p->just_one ? Qtrue : Qfalse;
}
|
#just_one=(value) ⇒ Object
Sets the just_one option which limits the parsing of a string or or stream to a single JSON element.
Returns the current state of the just_one [Boolean] option.
1351 1352 1353 1354 1355 1356 1357 1358 1359 |
# File 'ext/oj/parser.c', line 1351
static VALUE parser_just_one_set(VALUE self, VALUE v) {
ojParser p;
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
p->just_one = (Qtrue == v);
return p->just_one ? Qtrue : Qfalse;
}
|
#load(reader) ⇒ Object
Parse a JSON stream.
Returns the result according to the delegate of the parser.
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 |
# File 'ext/oj/parser.c', line 1267
static VALUE parser_load(VALUE self, VALUE reader) {
ojParser p;
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
parser_reset(p);
p->reader = reader;
rb_rescue2(load, self, load_rescue, Qnil, rb_eEOFError, 0);
return p->result(p);
}
|
#parse(json) ⇒ Object
Parse a JSON string.
Returns the result according to the delegate of the parser.
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 |
# File 'ext/oj/parser.c', line 1213
static VALUE parser_parse(VALUE self, VALUE json) {
ojParser p;
int frozen = OBJ_FROZEN(json);
const byte *ptr;
if (!frozen) {
rb_str_freeze(json);
}
ptr = (const byte *)StringValuePtr(json);
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
parser_reset(p);
p->start(p);
parse(p, ptr, (size_t)RSTRING_LEN(json), false);
validate_document_end(p);
return p->result(p);
}
|