oj 3.13.17 → 3.16.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +77 -0
  3. data/README.md +4 -2
  4. data/ext/oj/buf.h +7 -6
  5. data/ext/oj/cache.c +29 -26
  6. data/ext/oj/cache.h +3 -2
  7. data/ext/oj/cache8.c +10 -9
  8. data/ext/oj/circarray.c +7 -5
  9. data/ext/oj/circarray.h +2 -2
  10. data/ext/oj/code.c +5 -12
  11. data/ext/oj/code.h +2 -2
  12. data/ext/oj/compat.c +20 -60
  13. data/ext/oj/custom.c +44 -96
  14. data/ext/oj/debug.c +3 -9
  15. data/ext/oj/dump.c +69 -39
  16. data/ext/oj/dump.h +1 -4
  17. data/ext/oj/dump_compat.c +557 -592
  18. data/ext/oj/dump_leaf.c +3 -5
  19. data/ext/oj/dump_object.c +42 -48
  20. data/ext/oj/dump_strict.c +10 -22
  21. data/ext/oj/encoder.c +1 -1
  22. data/ext/oj/err.c +2 -13
  23. data/ext/oj/err.h +9 -12
  24. data/ext/oj/extconf.rb +16 -6
  25. data/ext/oj/fast.c +76 -106
  26. data/ext/oj/intern.c +63 -51
  27. data/ext/oj/intern.h +3 -7
  28. data/ext/oj/mem.c +318 -0
  29. data/ext/oj/mem.h +53 -0
  30. data/ext/oj/mimic_json.c +43 -30
  31. data/ext/oj/object.c +61 -70
  32. data/ext/oj/odd.c +8 -6
  33. data/ext/oj/odd.h +4 -4
  34. data/ext/oj/oj.c +243 -205
  35. data/ext/oj/oj.h +82 -78
  36. data/ext/oj/parse.c +123 -188
  37. data/ext/oj/parse.h +23 -24
  38. data/ext/oj/parser.c +103 -63
  39. data/ext/oj/parser.h +19 -9
  40. data/ext/oj/rails.c +68 -92
  41. data/ext/oj/reader.c +10 -15
  42. data/ext/oj/reader.h +4 -2
  43. data/ext/oj/resolve.c +3 -4
  44. data/ext/oj/rxclass.c +6 -5
  45. data/ext/oj/rxclass.h +1 -1
  46. data/ext/oj/saj.c +10 -9
  47. data/ext/oj/saj2.c +74 -92
  48. data/ext/oj/saj2.h +23 -0
  49. data/ext/oj/scp.c +3 -14
  50. data/ext/oj/sparse.c +22 -70
  51. data/ext/oj/stream_writer.c +43 -35
  52. data/ext/oj/strict.c +20 -52
  53. data/ext/oj/string_writer.c +60 -34
  54. data/ext/oj/trace.h +31 -4
  55. data/ext/oj/usual.c +125 -150
  56. data/ext/oj/usual.h +69 -0
  57. data/ext/oj/util.h +1 -1
  58. data/ext/oj/val_stack.c +14 -3
  59. data/ext/oj/val_stack.h +8 -7
  60. data/ext/oj/wab.c +25 -57
  61. data/lib/oj/active_support_helper.rb +1 -3
  62. data/lib/oj/bag.rb +7 -1
  63. data/lib/oj/easy_hash.rb +4 -5
  64. data/lib/oj/error.rb +0 -1
  65. data/lib/oj/json.rb +162 -150
  66. data/lib/oj/mimic.rb +6 -2
  67. data/lib/oj/state.rb +9 -6
  68. data/lib/oj/version.rb +1 -2
  69. data/lib/oj.rb +2 -0
  70. data/pages/Compatibility.md +1 -1
  71. data/pages/InstallOptions.md +20 -0
  72. data/pages/Options.md +10 -0
  73. data/test/_test_active.rb +8 -9
  74. data/test/_test_active_mimic.rb +7 -8
  75. data/test/_test_mimic_rails.rb +17 -20
  76. data/test/activerecord/result_test.rb +5 -6
  77. data/test/files.rb +15 -15
  78. data/test/foo.rb +9 -72
  79. data/test/helper.rb +11 -8
  80. data/test/isolated/shared.rb +3 -2
  81. data/test/json_gem/json_addition_test.rb +2 -2
  82. data/test/json_gem/json_common_interface_test.rb +8 -6
  83. data/test/json_gem/json_encoding_test.rb +0 -0
  84. data/test/json_gem/json_ext_parser_test.rb +1 -0
  85. data/test/json_gem/json_fixtures_test.rb +3 -2
  86. data/test/json_gem/json_generator_test.rb +53 -37
  87. data/test/json_gem/json_generic_object_test.rb +11 -11
  88. data/test/json_gem/json_parser_test.rb +47 -47
  89. data/test/json_gem/json_string_matching_test.rb +9 -9
  90. data/test/json_gem/test_helper.rb +7 -3
  91. data/test/mem.rb +13 -12
  92. data/test/perf.rb +21 -26
  93. data/test/perf_compat.rb +31 -33
  94. data/test/perf_dump.rb +28 -28
  95. data/test/perf_fast.rb +80 -82
  96. data/test/perf_file.rb +27 -29
  97. data/test/perf_object.rb +65 -69
  98. data/test/perf_once.rb +12 -11
  99. data/test/perf_parser.rb +42 -48
  100. data/test/perf_saj.rb +46 -54
  101. data/test/perf_scp.rb +57 -69
  102. data/test/perf_simple.rb +41 -39
  103. data/test/perf_strict.rb +68 -70
  104. data/test/perf_wab.rb +67 -69
  105. data/test/prec.rb +5 -5
  106. data/test/sample/change.rb +0 -1
  107. data/test/sample/dir.rb +0 -1
  108. data/test/sample/doc.rb +0 -1
  109. data/test/sample/file.rb +0 -1
  110. data/test/sample/group.rb +0 -1
  111. data/test/sample/hasprops.rb +0 -1
  112. data/test/sample/layer.rb +0 -1
  113. data/test/sample/rect.rb +0 -1
  114. data/test/sample/shape.rb +0 -1
  115. data/test/sample/text.rb +0 -1
  116. data/test/sample.rb +16 -16
  117. data/test/sample_json.rb +8 -8
  118. data/test/test_compat.rb +80 -53
  119. data/test/test_custom.rb +73 -51
  120. data/test/test_debian.rb +7 -10
  121. data/test/test_fast.rb +86 -90
  122. data/test/test_file.rb +28 -35
  123. data/test/test_gc.rb +16 -5
  124. data/test/test_generate.rb +5 -5
  125. data/test/test_hash.rb +4 -4
  126. data/test/test_integer_range.rb +9 -9
  127. data/test/test_null.rb +20 -20
  128. data/test/test_object.rb +94 -96
  129. data/test/test_parser.rb +6 -22
  130. data/test/test_parser_debug.rb +27 -0
  131. data/test/test_parser_saj.rb +61 -22
  132. data/test/test_parser_usual.rb +16 -6
  133. data/test/test_rails.rb +2 -2
  134. data/test/test_saj.rb +10 -8
  135. data/test/test_scp.rb +37 -39
  136. data/test/test_strict.rb +40 -32
  137. data/test/test_various.rb +148 -100
  138. data/test/test_wab.rb +48 -44
  139. data/test/test_writer.rb +47 -47
  140. data/test/tests.rb +13 -4
  141. data/test/tests_mimic.rb +12 -3
  142. data/test/tests_mimic_addition.rb +12 -3
  143. metadata +36 -27
  144. data/test/activesupport4/decoding_test.rb +0 -108
  145. data/test/activesupport4/encoding_test.rb +0 -531
  146. data/test/activesupport4/test_helper.rb +0 -41
  147. data/test/activesupport5/abstract_unit.rb +0 -45
  148. data/test/activesupport5/decoding_test.rb +0 -133
  149. data/test/activesupport5/encoding_test.rb +0 -500
  150. data/test/activesupport5/encoding_test_cases.rb +0 -98
  151. data/test/activesupport5/test_helper.rb +0 -72
  152. data/test/activesupport5/time_zone_test_helpers.rb +0 -39
  153. data/test/bar.rb +0 -11
  154. data/test/baz.rb +0 -16
  155. data/test/bug.rb +0 -16
  156. data/test/zoo.rb +0 -13
data/ext/oj/parse.h CHANGED
@@ -16,23 +16,25 @@
16
16
  #include "val_stack.h"
17
17
 
18
18
  struct _rxClass;
19
+ struct _parseInfo;
19
20
 
20
21
  typedef struct _numInfo {
21
- int64_t i;
22
- int64_t num;
23
- int64_t div;
24
- int64_t di;
25
- const char *str;
26
- size_t len;
27
- long exp;
28
- int big;
29
- int infinity;
30
- int nan;
31
- int neg;
32
- int has_exp;
33
- int no_big;
34
- int bigdec_load;
35
- } * NumInfo;
22
+ int64_t i;
23
+ int64_t num;
24
+ int64_t div;
25
+ int64_t di;
26
+ const char *str;
27
+ size_t len;
28
+ long exp;
29
+ struct _parseInfo *pi;
30
+ int big;
31
+ int infinity;
32
+ int nan;
33
+ int neg;
34
+ int has_exp;
35
+ int no_big;
36
+ int bigdec_load;
37
+ } *NumInfo;
36
38
 
37
39
  typedef struct _parseInfo {
38
40
  // used for the string parser
@@ -54,11 +56,7 @@ typedef struct _parseInfo {
54
56
  VALUE (*start_hash)(struct _parseInfo *pi);
55
57
  void (*end_hash)(struct _parseInfo *pi);
56
58
  VALUE (*hash_key)(struct _parseInfo *pi, const char *key, size_t klen);
57
- void (*hash_set_cstr)(struct _parseInfo *pi,
58
- Val kval,
59
- const char * str,
60
- size_t len,
61
- const char * orig);
59
+ void (*hash_set_cstr)(struct _parseInfo *pi, Val kval, const char *str, size_t len, const char *orig);
62
60
  void (*hash_set_num)(struct _parseInfo *pi, Val kval, NumInfo ni);
63
61
  void (*hash_set_value)(struct _parseInfo *pi, Val kval, VALUE value);
64
62
 
@@ -73,11 +71,10 @@ typedef struct _parseInfo {
73
71
  void (*add_value)(struct _parseInfo *pi, VALUE val);
74
72
  VALUE err_class;
75
73
  bool has_callbacks;
76
- } * ParseInfo;
74
+ } *ParseInfo;
77
75
 
78
- extern void oj_parse2(ParseInfo pi);
79
- extern void
80
- oj_set_error_at(ParseInfo pi, VALUE err_clas, const char *file, int line, const char *format, ...);
76
+ extern void oj_parse2(ParseInfo pi);
77
+ extern void oj_set_error_at(ParseInfo pi, VALUE err_clas, const char *file, int line, const char *format, ...);
81
78
  extern VALUE oj_pi_parse(int argc, VALUE *argv, ParseInfo pi, char *json, size_t len, int yieldOk);
82
79
  extern VALUE oj_num_as_value(NumInfo ni);
83
80
 
@@ -97,6 +94,8 @@ static inline void parse_info_init(ParseInfo pi) {
97
94
  memset(pi, 0, sizeof(struct _parseInfo));
98
95
  }
99
96
 
97
+ extern void oj_scanner_init(void);
98
+
100
99
  static inline bool empty_ok(Options options) {
101
100
  switch (options->mode) {
102
101
  case ObjectMode:
data/ext/oj/parser.c CHANGED
@@ -20,7 +20,7 @@
20
20
  #define FRAC_LIMIT 10000000000000000ULL
21
21
 
22
22
  // Give better performance with indented JSON but worse with unindented.
23
- //#define SPACE_JUMP
23
+ // #define SPACE_JUMP
24
24
 
25
25
  enum {
26
26
  SKIP_CHAR = 'a',
@@ -1145,8 +1145,10 @@ static void parser_free(void *ptr) {
1145
1145
  p = (ojParser)ptr;
1146
1146
  buf_cleanup(&p->key);
1147
1147
  buf_cleanup(&p->buf);
1148
- p->free(p);
1149
- xfree(ptr);
1148
+ if (NULL != p->free) {
1149
+ p->free(p);
1150
+ }
1151
+ OJ_R_FREE(ptr);
1150
1152
  }
1151
1153
 
1152
1154
  static void parser_mark(void *ptr) {
@@ -1156,10 +1158,23 @@ static void parser_mark(void *ptr) {
1156
1158
  if (0 != p->reader) {
1157
1159
  rb_gc_mark(p->reader);
1158
1160
  }
1159
- p->mark(p);
1161
+ if (NULL != p->mark) {
1162
+ p->mark(p);
1163
+ }
1160
1164
  }
1161
1165
  }
1162
1166
 
1167
+ static const rb_data_type_t oj_parser_type = {
1168
+ "Oj/parser",
1169
+ {
1170
+ parser_mark,
1171
+ parser_free,
1172
+ NULL,
1173
+ },
1174
+ 0,
1175
+ 0,
1176
+ };
1177
+
1163
1178
  extern void oj_set_parser_validator(ojParser p);
1164
1179
  extern void oj_set_parser_saj(ojParser p);
1165
1180
  extern void oj_set_parser_usual(ojParser p);
@@ -1176,7 +1191,7 @@ static int opt_cb(VALUE rkey, VALUE value, VALUE ptr) {
1176
1191
  rkey = rb_sym2str(rkey);
1177
1192
  // fall through
1178
1193
  case RUBY_T_STRING:
1179
- key = rb_string_value_ptr(&rkey);
1194
+ key = StringValuePtr(rkey);
1180
1195
  klen = RSTRING_LEN(rkey);
1181
1196
  break;
1182
1197
  default: rb_raise(rb_eArgError, "option keys must be a symbol or string");
@@ -1201,7 +1216,7 @@ static int opt_cb(VALUE rkey, VALUE value, VALUE ptr) {
1201
1216
  * Oj::Parser.new(:usual, cache_keys: true).
1202
1217
  */
1203
1218
  static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
1204
- ojParser p = ALLOC(struct _ojParser);
1219
+ ojParser p = OJ_R_ALLOC(struct _ojParser);
1205
1220
 
1206
1221
  #if HAVE_RB_EXT_RACTOR_SAFE
1207
1222
  // This doesn't seem to do anything.
@@ -1251,7 +1266,33 @@ static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
1251
1266
  rb_hash_foreach(ropts, opt_cb, (VALUE)p);
1252
1267
  }
1253
1268
  }
1254
- return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1269
+ return TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1270
+ }
1271
+
1272
+ // Create a new parser without setting the delegate. The parser is
1273
+ // wrapped. The parser is (ojParser)DATA_PTR(value) where value is the return
1274
+ // from this function. A delegate must be added before the parser can be
1275
+ // used. Optionally oj_parser_set_options can be called if the options are not
1276
+ // set directly.
1277
+ VALUE oj_parser_new(void) {
1278
+ ojParser p = OJ_R_ALLOC(struct _ojParser);
1279
+
1280
+ #if HAVE_RB_EXT_RACTOR_SAFE
1281
+ // This doesn't seem to do anything.
1282
+ rb_ext_ractor_safe(true);
1283
+ #endif
1284
+ memset(p, 0, sizeof(struct _ojParser));
1285
+ buf_init(&p->key);
1286
+ buf_init(&p->buf);
1287
+ p->map = value_map;
1288
+
1289
+ return TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1290
+ }
1291
+
1292
+ // Set set the options from a hash (ropts).
1293
+ void oj_parser_set_option(ojParser p, VALUE ropts) {
1294
+ Check_Type(ropts, T_HASH);
1295
+ rb_hash_foreach(ropts, opt_cb, (VALUE)p);
1255
1296
  }
1256
1297
 
1257
1298
  /* Document-method: method_missing(value)
@@ -1264,53 +1305,41 @@ static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
1264
1305
  * - no options
1265
1306
  *
1266
1307
  * - *:saj*
1267
- * - _cache_keys=_ sets the value of the _cache_keys_ flag.
1268
- * - _cache_keys_ returns the value of the _cache_keys_ flag.
1269
- * - _cache_strings=_ sets the value of the _cache_strings_ to an positive integer less than 35. Strings shorter than
1270
- * that length are cached.
1271
- * - _cache_strings_ returns the value of the _cache_strings_ integer value.
1272
- * - _handler=_ sets the SAJ handler
1273
- * - _handler_ returns the SAJ handler
1308
+ * - _cache_keys_ is a flag indicating hash keys should be cached.
1309
+ * - _cache_strings_ is a positive integer less than 35. Strings shorter than that length are cached.
1310
+ * - _handler_ is the SAJ handler
1274
1311
  *
1275
1312
  * - *:usual*
1276
- * - _cache_keys=_ sets the value of the _cache_keys_ flag.
1277
- * - _cache_keys_ returns the value of the _cache_keys_ flag.
1278
- * - _cache_strings=_ sets the value of the _cache_strings_ to a positive integer less than 35. Strings shorter than
1279
- * that length are cached.
1280
- * - _cache_strings_ returns the value of the _cache_strings_ integer value.
1281
- * - _cache_expunge=_ sets the value of the _cache_expunge_ where 0 never expunges, 1 expunges slowly, 2 expunges
1282
- * faster, and 3 or higher expunges agressively.
1283
- * - _cache_expunge_ returns the value of the _cache_expunge_ integer value.
1284
- * - _capacity=_ sets the capacity of the parser. The parser grows automatically but can be updated directly with this
1285
- * call.
1286
- * - _capacity_ returns the current capacity of the parser's internal stack.
1287
- * - _create_id_ returns the value _create_id_ or _nil_ if there is no _create_id_.
1288
- * - _create_id=_ sets the value _create_id_ or if _nil_ unsets it. Parsed JSON objects that include the specified
1289
- * element use the element value as the name of the class to create an object from instead of a Hash.
1290
- * - _decimal=_ sets the approach to how decimals are parser. If _:auto_ then the decimals with significant digits are
1291
- * 16 or less are Floats and long ones are BigDecimal. _:ruby_ uses a call to Ruby to convert a string to a Float.
1292
- * _:float_ always generates a Float. _:bigdecimal_ always results in a BigDecimal.
1293
- * - _decimal_ returns the value of the decimal conversion option which can be :auto (default), :ruby, :float, or
1294
- * :bigdecimal.
1295
- * - _ignore_json_create_ returns the value of the _ignore_json_create_ flag.
1296
- * - _ignore_json_create=_ sets the value of the _ignore_json_create_ flag. When set the class json_create method is
1297
- * ignored on parsing in favor of creating an instance and populating directly.
1298
- * - _missing_class_ return the value of the _missing_class_ indicator.
1299
- * - _missing_class=_ sets the value of the _missing_class_ flag. Valid values are _:auto_ which creates any missing
1300
- * classes on parse, :ignore which ignores and continues as a Hash (default), and :raise which raises an exception if
1301
- * the class is not found.
1302
- * - _omit_null=_ sets the _omit_null_ flag. If true then null values in a map or object are omitted from the
1303
- * resulting Hash or Object.
1304
- * - _omit_null_ returns the value of the _omit_null_ flag.
1305
- * - _symbol_keys=_ sets the flag that indicates Hash keys should be parsed to Symbols versus Strings.
1306
- * - _symbol_keys_ returns the value of the _symbol_keys_ flag.
1313
+ * - _cache_keys_ is a flag indicating hash keys should be cached.
1314
+ * - _cache_strings_ is a positive integer less than 35. Strings shorter than that length are cached.
1315
+ * - _cache_expunge_ dictates when the cache will be expunged where 0 never expunges,
1316
+ * 1 expunges slowly, 2 expunges faster, and 3 or higher expunges agressively.
1317
+ * - _capacity_ is the capacity of the parser's internal stack. The parser grows automatically
1318
+ * but can be updated directly with this call.
1319
+ * - _create_id_ if non-nil is the key that is used to specify the type of object to create
1320
+ * when parsing. Parsed JSON objects that include the specified element use the element
1321
+ * value as the name of the class to create an object from instead of a Hash.
1322
+ * - _decimal_ is the approach to how decimals are parsed. If _:auto_ then
1323
+ * the decimals with significant digits are 16 or less are Floats and long
1324
+ * ones are BigDecimal. _:ruby_ uses a call to Ruby to convert a string to a Float.
1325
+ * _:float_ always generates a Float. _:bigdecimal_ always results in a BigDecimal.
1326
+ * - _ignore_json_create_ is a flag that when set the class json_create method is
1327
+ * ignored on parsing in favor of creating an instance and populating directly.
1328
+ * - _missing_class_ is an indicator that determines how unknown class names are handled.
1329
+ * Valid values are _:auto_ which creates any missing classes on parse, :ignore which ignores
1330
+ * and continues as a Hash (default), and :raise which raises an exception if the class is not found.
1331
+ * - _omit_null_ is a flag that if true then null values in a map or object are omitted
1332
+ * from the resulting Hash or Object.
1333
+ * - _symbol_keys_ is a flag that indicates Hash keys should be parsed to Symbols versus Strings.
1307
1334
  */
1308
1335
  static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
1309
- ojParser p = (ojParser)DATA_PTR(self);
1336
+ ojParser p;
1310
1337
  const char *key = NULL;
1311
1338
  volatile VALUE rkey = *argv;
1312
1339
  volatile VALUE rv = Qnil;
1313
1340
 
1341
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1342
+
1314
1343
  #if HAVE_RB_EXT_RACTOR_SAFE
1315
1344
  // This doesn't seem to do anything.
1316
1345
  rb_ext_ractor_safe(true);
@@ -1319,7 +1348,7 @@ static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
1319
1348
  case RUBY_T_SYMBOL:
1320
1349
  rkey = rb_sym2str(rkey);
1321
1350
  // fall through
1322
- case RUBY_T_STRING: key = rb_string_value_ptr(&rkey); break;
1351
+ case RUBY_T_STRING: key = StringValuePtr(rkey); break;
1323
1352
  default: rb_raise(rb_eArgError, "option method must be a symbol or string");
1324
1353
  }
1325
1354
  if (1 < argc) {
@@ -1336,12 +1365,14 @@ static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
1336
1365
  * Returns the result according to the delegate of the parser.
1337
1366
  */
1338
1367
  static VALUE parser_parse(VALUE self, VALUE json) {
1339
- ojParser p = (ojParser)DATA_PTR(self);
1368
+ ojParser p;
1369
+ const byte *ptr = (const byte *)StringValuePtr(json);
1370
+
1371
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1340
1372
 
1341
- Check_Type(json, T_STRING);
1342
1373
  parser_reset(p);
1343
1374
  p->start(p);
1344
- parse(p, (const byte *)rb_string_value_ptr(&json));
1375
+ parse(p, ptr);
1345
1376
 
1346
1377
  return p->result(p);
1347
1378
  }
@@ -1352,9 +1383,11 @@ static VALUE load_rescue(VALUE self, VALUE x) {
1352
1383
  }
1353
1384
 
1354
1385
  static VALUE load(VALUE self) {
1355
- ojParser p = (ojParser)DATA_PTR(self);
1386
+ ojParser p;
1356
1387
  volatile VALUE rbuf = rb_str_new2("");
1357
1388
 
1389
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1390
+
1358
1391
  p->start(p);
1359
1392
  while (true) {
1360
1393
  rb_funcall(p->reader, oj_readpartial_id, 2, INT2NUM(16385), rbuf);
@@ -1373,7 +1406,9 @@ static VALUE load(VALUE self) {
1373
1406
  * Returns the result according to the delegate of the parser.
1374
1407
  */
1375
1408
  static VALUE parser_load(VALUE self, VALUE reader) {
1376
- ojParser p = (ojParser)DATA_PTR(self);
1409
+ ojParser p;
1410
+
1411
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1377
1412
 
1378
1413
  parser_reset(p);
1379
1414
  p->reader = reader;
@@ -1390,12 +1425,13 @@ static VALUE parser_load(VALUE self, VALUE reader) {
1390
1425
  * Returns the result according to the delegate of the parser.
1391
1426
  */
1392
1427
  static VALUE parser_file(VALUE self, VALUE filename) {
1393
- ojParser p = (ojParser)DATA_PTR(self);
1428
+ ojParser p;
1394
1429
  const char *path;
1395
1430
  int fd;
1396
1431
 
1397
- Check_Type(filename, T_STRING);
1398
- path = rb_string_value_ptr(&filename);
1432
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1433
+
1434
+ path = StringValuePtr(filename);
1399
1435
 
1400
1436
  parser_reset(p);
1401
1437
  p->start(p);
@@ -1438,7 +1474,9 @@ static VALUE parser_file(VALUE self, VALUE filename) {
1438
1474
  * Returns the current state of the just_one [_Boolean_] option.
1439
1475
  */
1440
1476
  static VALUE parser_just_one(VALUE self) {
1441
- ojParser p = (ojParser)DATA_PTR(self);
1477
+ ojParser p;
1478
+
1479
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1442
1480
 
1443
1481
  return p->just_one ? Qtrue : Qfalse;
1444
1482
  }
@@ -1452,7 +1490,9 @@ static VALUE parser_just_one(VALUE self) {
1452
1490
  * Returns the current state of the just_one [_Boolean_] option.
1453
1491
  */
1454
1492
  static VALUE parser_just_one_set(VALUE self, VALUE v) {
1455
- ojParser p = (ojParser)DATA_PTR(self);
1493
+ ojParser p;
1494
+
1495
+ TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1456
1496
 
1457
1497
  p->just_one = (Qtrue == v);
1458
1498
 
@@ -1469,14 +1509,14 @@ static VALUE usual_parser = Qundef;
1469
1509
  */
1470
1510
  static VALUE parser_usual(VALUE self) {
1471
1511
  if (Qundef == usual_parser) {
1472
- ojParser p = ALLOC(struct _ojParser);
1512
+ ojParser p = OJ_R_ALLOC(struct _ojParser);
1473
1513
 
1474
1514
  memset(p, 0, sizeof(struct _ojParser));
1475
1515
  buf_init(&p->key);
1476
1516
  buf_init(&p->buf);
1477
1517
  p->map = value_map;
1478
1518
  oj_set_parser_usual(p);
1479
- usual_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1519
+ usual_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1480
1520
  rb_gc_register_address(&usual_parser);
1481
1521
  }
1482
1522
  return usual_parser;
@@ -1492,14 +1532,14 @@ static VALUE saj_parser = Qundef;
1492
1532
  */
1493
1533
  static VALUE parser_saj(VALUE self) {
1494
1534
  if (Qundef == saj_parser) {
1495
- ojParser p = ALLOC(struct _ojParser);
1535
+ ojParser p = OJ_R_ALLOC(struct _ojParser);
1496
1536
 
1497
1537
  memset(p, 0, sizeof(struct _ojParser));
1498
1538
  buf_init(&p->key);
1499
1539
  buf_init(&p->buf);
1500
1540
  p->map = value_map;
1501
1541
  oj_set_parser_saj(p);
1502
- saj_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1542
+ saj_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1503
1543
  rb_gc_register_address(&saj_parser);
1504
1544
  }
1505
1545
  return saj_parser;
@@ -1514,14 +1554,14 @@ static VALUE validate_parser = Qundef;
1514
1554
  */
1515
1555
  static VALUE parser_validate(VALUE self) {
1516
1556
  if (Qundef == validate_parser) {
1517
- ojParser p = ALLOC(struct _ojParser);
1557
+ ojParser p = OJ_R_ALLOC(struct _ojParser);
1518
1558
 
1519
1559
  memset(p, 0, sizeof(struct _ojParser));
1520
1560
  buf_init(&p->key);
1521
1561
  buf_init(&p->buf);
1522
1562
  p->map = value_map;
1523
1563
  oj_set_parser_validator(p);
1524
- validate_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1564
+ validate_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
1525
1565
  rb_gc_register_address(&validate_parser);
1526
1566
  }
1527
1567
  return validate_parser;
data/ext/oj/parser.h CHANGED
@@ -4,8 +4,8 @@
4
4
  #ifndef OJ_PARSER_H
5
5
  #define OJ_PARSER_H
6
6
 
7
- #include <stdbool.h>
8
7
  #include <ruby.h>
8
+ #include <stdbool.h>
9
9
 
10
10
  #include "buf.h"
11
11
 
@@ -32,13 +32,13 @@ typedef struct _num {
32
32
  long double dub;
33
33
  int64_t fixnum; // holds all digits
34
34
  uint32_t len;
35
- int16_t div; // 10^div
35
+ int16_t div; // 10^div
36
36
  int16_t exp;
37
- uint8_t shift; // shift of fixnum to get decimal
37
+ uint8_t shift; // shift of fixnum to get decimal
38
38
  bool neg;
39
39
  bool exp_neg;
40
40
  // for numbers as strings, reuse buf
41
- } * Num;
41
+ } *Num;
42
42
 
43
43
  struct _ojParser;
44
44
 
@@ -54,11 +54,11 @@ typedef struct _funcs {
54
54
  void (*close_array)(struct _ojParser *p);
55
55
  void (*open_object)(struct _ojParser *p);
56
56
  void (*close_object)(struct _ojParser *p);
57
- } * Funcs;
57
+ } *Funcs;
58
58
 
59
59
  typedef struct _ojParser {
60
- const char * map;
61
- const char * next_map;
60
+ const char *map;
61
+ const char *next_map;
62
62
  int depth;
63
63
  unsigned char stack[1024];
64
64
 
@@ -67,7 +67,7 @@ typedef struct _ojParser {
67
67
  struct _buf key;
68
68
  struct _buf buf;
69
69
 
70
- struct _funcs funcs[3]; // indexed by XXX_FUN defines
70
+ struct _funcs funcs[3]; // indexed by XXX_FUN defines
71
71
 
72
72
  void (*start)(struct _ojParser *p);
73
73
  VALUE (*option)(struct _ojParser *p, const char *key, VALUE value);
@@ -86,6 +86,16 @@ typedef struct _ojParser {
86
86
  uint32_t ucode;
87
87
  ojType type; // valType
88
88
  bool just_one;
89
- } * ojParser;
89
+ } *ojParser;
90
+
91
+ // Create a new parser without setting the delegate. The parser is
92
+ // wrapped. The parser is (ojParser)DATA_PTR(value) where value is the return
93
+ // from this function. A delegate must be added before the parser can be
94
+ // used. Optionally oj_parser_set_options can be called if the options are not
95
+ // set directly.
96
+ extern VALUE oj_parser_new();
97
+
98
+ // Set set the options from a hash (ropts).
99
+ extern void oj_parser_set_option(ojParser p, VALUE ropts);
90
100
 
91
101
  #endif /* OJ_PARSER_H */