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.
-
.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= sets the value of the cache_keys flag.
-
cache_keys returns the value of the cache_keys flag.
-
cache_strings= sets the value of the cache_strings to an positive integer less than 35. Strings shorter than
-
that length are cached.
- _cache_strings_ returns the value of the _cache_strings_ integer value.
- _handler=_ sets the SAJ handler
- _handler_ returns the SAJ handler
-
:usual
-
cache_keys= sets the value of the cache_keys flag.
-
cache_keys returns the value of the cache_keys flag.
-
cache_strings= sets the value of the cache_strings to a positive integer less than 35. Strings shorter than
-
that length are cached.
- _cache_strings_ returns the value of the _cache_strings_ integer value.
- _cache_expunge=_ sets the value of the _cache_expunge_ where 0 never expunges, 1 expunges slowly, 2 expunges
faster, and 3 or higher expunges agressively.
- _cache_expunge_ returns the value of the _cache_expunge_ integer value.
- _capacity=_ sets the capacity of the parser. The parser grows automatically but can be updated directly with this
call.
- _capacity_ returns the current capacity of the parser's internal stack.
- _create_id_ returns the value _create_id_ or _nil_ if there is no _create_id_.
- _create_id=_ sets the value _create_id_ or if _nil_ unsets it. 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=_ sets the approach to how decimals are parser. 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.
- _decimal_ returns the value of the decimal conversion option which can be :auto (default), :ruby, :float, or
:bigdecimal.
- _ignore_json_create_ returns the value of the _ignore_json_create_ flag.
- _ignore_json_create=_ sets the value of the _ignore_json_create_ flag. When set the class json_create method is
ignored on parsing in favor of creating an instance and populating directly.
- _missing_class_ return the value of the _missing_class_ indicator.
- _missing_class=_ sets the value of the _missing_class_ flag. 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=_ sets the _omit_null_ flag. If true then null values in a map or object are omitted from the
resulting Hash or Object.
- _omit_null_ returns the value of the _omit_null_ flag.
- _symbol_keys=_ sets the flag that indicates Hash keys should be parsed to Symbols versus Strings.
- _symbol_keys_ returns the value of the _symbol_keys_ flag.
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 |
# File 'ext/oj/parser.c', line 1029
static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
ojParser p = (ojParser)DATA_PTR(self);
const char *key = NULL;
volatile VALUE rkey = *argv;
volatile VALUE rv = Qnil;
#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 = rb_string_value_ptr(&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).
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 |
# File 'ext/oj/parser.c', line 924
static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
ojParser p = 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 Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
}
|
.saj ⇒ Object
Returns the default saj parser. Note the default SAJ parser can not be used concurrently in more than one thread.
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 |
# File 'ext/oj/parser.c', line 1214
static VALUE parser_saj(VALUE self) {
if (Qundef == saj_parser) {
ojParser p = 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 = Data_Wrap_Struct(parser_class, parser_mark, parser_free, 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.
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 |
# File 'ext/oj/parser.c', line 1191
static VALUE parser_usual(VALUE self) {
if (Qundef == usual_parser) {
ojParser p = 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 = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
rb_gc_register_address(&usual_parser);
}
return usual_parser;
}
|
.validate ⇒ Object
Returns the default validate parser.
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 |
# File 'ext/oj/parser.c', line 1236
static VALUE parser_validate(VALUE self) {
if (Qundef == validate_parser) {
ojParser p = 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 = Data_Wrap_Struct(parser_class, parser_mark, parser_free, 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.
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 |
# File 'ext/oj/parser.c', line 1113
static VALUE parser_file(VALUE self, VALUE filename) {
ojParser p = (ojParser)DATA_PTR(self);
const char *path;
int fd;
Check_Type(filename, T_STRING);
path = rb_string_value_ptr(&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);
}
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.
1161 1162 1163 1164 1165 |
# File 'ext/oj/parser.c', line 1161
static VALUE parser_just_one(VALUE self) {
ojParser p = (ojParser)DATA_PTR(self);
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.
1175 1176 1177 1178 1179 1180 1181 |
# File 'ext/oj/parser.c', line 1175
static VALUE parser_just_one_set(VALUE self, VALUE v) {
ojParser p = (ojParser)DATA_PTR(self);
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.
1096 1097 1098 1099 1100 1101 1102 1103 1104 |
# File 'ext/oj/parser.c', line 1096
static VALUE parser_load(VALUE self, VALUE reader) {
ojParser p = (ojParser)DATA_PTR(self);
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.
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
# File 'ext/oj/parser.c', line 1059
static VALUE parser_parse(VALUE self, VALUE json) {
ojParser p = (ojParser)DATA_PTR(self);
Check_Type(json, T_STRING);
parser_reset(p);
p->start(p);
parse(p, (const byte *)rb_string_value_ptr(&json));
return p->result(p);
}
|