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
@@ -3,6 +3,7 @@
3
3
 
4
4
  #include "dump.h"
5
5
  #include "encode.h"
6
+ #include "mem.h"
6
7
 
7
8
  extern VALUE Oj;
8
9
 
@@ -20,7 +21,7 @@ static void push_type(StrWriter sw, DumpType type) {
20
21
  if (sw->types_end <= sw->types + sw->depth + 1) {
21
22
  size_t size = (sw->types_end - sw->types) * 2;
22
23
 
23
- REALLOC_N(sw->types, char, size);
24
+ OJ_R_REALLOC_N(sw->types, char, size);
24
25
  sw->types_end = sw->types + size;
25
26
  }
26
27
  sw->depth++;
@@ -43,7 +44,7 @@ static void maybe_comma(StrWriter sw) {
43
44
  void oj_str_writer_init(StrWriter sw, int buf_size) {
44
45
  sw->opts = oj_default_options;
45
46
  sw->depth = 0;
46
- sw->types = ALLOC_N(char, 256);
47
+ sw->types = OJ_R_ALLOC_N(char, 256);
47
48
  sw->types_end = sw->types + 256;
48
49
  *sw->types = '\0';
49
50
  sw->keyWritten = 0;
@@ -55,10 +56,10 @@ void oj_str_writer_init(StrWriter sw, int buf_size) {
55
56
  }
56
57
  // Must be allocated. Using the out.stack_buffer results in double frees
57
58
  // and I haven't figured out why yet.
58
- sw->out.buf = ALLOC_N(char, buf_size);
59
- sw->out.cur = sw->out.buf;
60
- sw->out.end = sw->out.buf + buf_size - BUFFER_EXTRA;
61
- sw->out.allocated = true;
59
+ sw->out.buf = OJ_R_ALLOC_N(char, buf_size);
60
+ sw->out.cur = sw->out.buf;
61
+ sw->out.end = sw->out.buf + buf_size - BUFFER_EXTRA;
62
+ sw->out.allocated = true;
62
63
 
63
64
  *sw->out.cur = '\0';
64
65
  sw->out.circ_cache = NULL;
@@ -69,7 +70,6 @@ void oj_str_writer_init(StrWriter sw, int buf_size) {
69
70
  sw->out.depth = 0;
70
71
  sw->out.argc = 0;
71
72
  sw->out.argv = NULL;
72
- sw->out.caller = 0;
73
73
  sw->out.ropts = NULL;
74
74
  sw->out.omit_nil = oj_default_options.dump_opts.omit_nil;
75
75
  }
@@ -225,7 +225,7 @@ void oj_str_writer_pop_all(StrWriter sw) {
225
225
  }
226
226
  }
227
227
 
228
- static void str_writer_free(void *ptr) {
228
+ static void string_writer_free(void *ptr) {
229
229
  StrWriter sw;
230
230
 
231
231
  if (0 == ptr) {
@@ -235,8 +235,25 @@ static void str_writer_free(void *ptr) {
235
235
 
236
236
  oj_out_free(&sw->out);
237
237
 
238
- xfree(sw->types);
239
- xfree(ptr);
238
+ OJ_R_FREE(sw->types);
239
+ OJ_R_FREE(ptr);
240
+ }
241
+
242
+ static const rb_data_type_t oj_string_writer_type = {
243
+ "Oj/string_writer",
244
+ {
245
+ NULL,
246
+ string_writer_free,
247
+ NULL,
248
+ },
249
+ 0,
250
+ 0,
251
+ };
252
+
253
+ StrWriter oj_str_writer_unwrap(VALUE writer) {
254
+ StrWriter sw;
255
+ TypedData_Get_Struct(writer, struct _strWriter, &oj_string_writer_type, sw);
256
+ return sw;
240
257
  }
241
258
 
242
259
  /* Document-method: new
@@ -256,7 +273,7 @@ static void str_writer_free(void *ptr) {
256
273
  * - *options* [_Hash_] formatting options
257
274
  */
258
275
  static VALUE str_writer_new(int argc, VALUE *argv, VALUE self) {
259
- StrWriter sw = ALLOC(struct _strWriter);
276
+ StrWriter sw = OJ_R_ALLOC(struct _strWriter);
260
277
 
261
278
  oj_str_writer_init(sw, 0);
262
279
  if (1 == argc) {
@@ -266,7 +283,7 @@ static VALUE str_writer_new(int argc, VALUE *argv, VALUE self) {
266
283
  sw->out.argv = argv + 1;
267
284
  sw->out.indent = sw->opts.indent;
268
285
 
269
- return Data_Wrap_Struct(oj_string_writer_class, 0, str_writer_free, sw);
286
+ return TypedData_Wrap_Struct(oj_string_writer_class, &oj_string_writer_type, sw);
270
287
  }
271
288
 
272
289
  /* Document-method: push_key
@@ -278,9 +295,9 @@ static VALUE str_writer_new(int argc, VALUE *argv, VALUE self) {
278
295
  * - *key* [_String_] the key pending for the next push
279
296
  */
280
297
  static VALUE str_writer_push_key(VALUE self, VALUE key) {
281
- StrWriter sw = (StrWriter)DATA_PTR(self);
298
+ StrWriter sw;
299
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
282
300
 
283
- rb_check_type(key, T_STRING);
284
301
  oj_str_writer_push_key(sw, StringValuePtr(key));
285
302
 
286
303
  return Qnil;
@@ -294,7 +311,8 @@ static VALUE str_writer_push_key(VALUE self, VALUE key) {
294
311
  * - *key* [_String_] the key if adding to an object in the JSON document
295
312
  */
296
313
  static VALUE str_writer_push_object(int argc, VALUE *argv, VALUE self) {
297
- StrWriter sw = (StrWriter)DATA_PTR(self);
314
+ StrWriter sw;
315
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
298
316
 
299
317
  switch (argc) {
300
318
  case 0: oj_str_writer_push_object(sw, 0); break;
@@ -302,7 +320,6 @@ static VALUE str_writer_push_object(int argc, VALUE *argv, VALUE self) {
302
320
  if (Qnil == argv[0]) {
303
321
  oj_str_writer_push_object(sw, 0);
304
322
  } else {
305
- rb_check_type(argv[0], T_STRING);
306
323
  oj_str_writer_push_object(sw, StringValuePtr(argv[0]));
307
324
  }
308
325
  break;
@@ -323,7 +340,8 @@ static VALUE str_writer_push_object(int argc, VALUE *argv, VALUE self) {
323
340
  * - *key* [_String_] the key if adding to an object in the JSON document
324
341
  */
325
342
  static VALUE str_writer_push_array(int argc, VALUE *argv, VALUE self) {
326
- StrWriter sw = (StrWriter)DATA_PTR(self);
343
+ StrWriter sw;
344
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
327
345
 
328
346
  switch (argc) {
329
347
  case 0: oj_str_writer_push_array(sw, 0); break;
@@ -331,7 +349,6 @@ static VALUE str_writer_push_array(int argc, VALUE *argv, VALUE self) {
331
349
  if (Qnil == argv[0]) {
332
350
  oj_str_writer_push_array(sw, 0);
333
351
  } else {
334
- rb_check_type(argv[0], T_STRING);
335
352
  oj_str_writer_push_array(sw, StringValuePtr(argv[0]));
336
353
  }
337
354
  break;
@@ -352,14 +369,16 @@ static VALUE str_writer_push_array(int argc, VALUE *argv, VALUE self) {
352
369
  * - *key* [_String_] the key if adding to an object in the JSON document
353
370
  */
354
371
  static VALUE str_writer_push_value(int argc, VALUE *argv, VALUE self) {
372
+ StrWriter sw;
373
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
374
+
355
375
  switch (argc) {
356
- case 1: oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0); break;
376
+ case 1: oj_str_writer_push_value(sw, *argv, 0); break;
357
377
  case 2:
358
378
  if (Qnil == argv[1]) {
359
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
379
+ oj_str_writer_push_value(sw, *argv, 0);
360
380
  } else {
361
- rb_check_type(argv[1], T_STRING);
362
- oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, StringValuePtr(argv[1]));
381
+ oj_str_writer_push_value(sw, *argv, StringValuePtr(argv[1]));
363
382
  }
364
383
  break;
365
384
  default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
@@ -377,17 +396,16 @@ static VALUE str_writer_push_value(int argc, VALUE *argv, VALUE self) {
377
396
  * - *key* [_String_] the key if adding to an object in the JSON document
378
397
  */
379
398
  static VALUE str_writer_push_json(int argc, VALUE *argv, VALUE self) {
380
- rb_check_type(argv[0], T_STRING);
399
+ StrWriter sw;
400
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
401
+
381
402
  switch (argc) {
382
- case 1: oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0); break;
403
+ case 1: oj_str_writer_push_json(sw, StringValuePtr(*argv), 0); break;
383
404
  case 2:
384
405
  if (Qnil == argv[1]) {
385
- oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
406
+ oj_str_writer_push_json(sw, StringValuePtr(*argv), 0);
386
407
  } else {
387
- rb_check_type(argv[1], T_STRING);
388
- oj_str_writer_push_json((StrWriter)DATA_PTR(self),
389
- StringValuePtr(*argv),
390
- StringValuePtr(argv[1]));
408
+ oj_str_writer_push_json(sw, StringValuePtr(*argv), StringValuePtr(argv[1]));
391
409
  }
392
410
  break;
393
411
  default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
@@ -401,7 +419,10 @@ static VALUE str_writer_push_json(int argc, VALUE *argv, VALUE self) {
401
419
  * currently open.
402
420
  */
403
421
  static VALUE str_writer_pop(VALUE self) {
404
- oj_str_writer_pop((StrWriter)DATA_PTR(self));
422
+ StrWriter sw;
423
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
424
+
425
+ oj_str_writer_pop(sw);
405
426
  return Qnil;
406
427
  }
407
428
 
@@ -412,7 +433,10 @@ static VALUE str_writer_pop(VALUE self) {
412
433
  * currently open.
413
434
  */
414
435
  static VALUE str_writer_pop_all(VALUE self) {
415
- oj_str_writer_pop_all((StrWriter)DATA_PTR(self));
436
+ StrWriter sw;
437
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
438
+
439
+ oj_str_writer_pop_all(sw);
416
440
 
417
441
  return Qnil;
418
442
  }
@@ -423,7 +447,8 @@ static VALUE str_writer_pop_all(VALUE self) {
423
447
  * Reset the writer back to the empty state.
424
448
  */
425
449
  static VALUE str_writer_reset(VALUE self) {
426
- StrWriter sw = (StrWriter)DATA_PTR(self);
450
+ StrWriter sw;
451
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
427
452
 
428
453
  sw->depth = 0;
429
454
  *sw->types = '\0';
@@ -442,8 +467,9 @@ static VALUE str_writer_reset(VALUE self) {
442
467
  * *return* [_String_]
443
468
  */
444
469
  static VALUE str_writer_to_s(VALUE self) {
445
- StrWriter sw = (StrWriter)DATA_PTR(self);
446
- VALUE rstr = rb_str_new(sw->out.buf, sw->out.cur - sw->out.buf);
470
+ StrWriter sw;
471
+ TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
472
+ VALUE rstr = rb_str_new(sw->out.buf, sw->out.cur - sw->out.buf);
447
473
 
448
474
  return oj_encode(rstr);
449
475
  }
data/ext/oj/trace.h CHANGED
@@ -17,12 +17,39 @@ typedef enum {
17
17
 
18
18
  struct _parseInfo;
19
19
 
20
- extern void
21
- oj_trace(const char *func, VALUE obj, const char *file, int line, int depth, TraceWhere where);
20
+ extern void oj_trace(const char *func, VALUE obj, const char *file, int line, int depth, TraceWhere where);
22
21
  extern void oj_trace_parse_in(const char *func, struct _parseInfo *pi, const char *file, int line);
23
- extern void
24
- oj_trace_parse_call(const char *func, struct _parseInfo *pi, const char *file, int line, VALUE obj);
22
+ extern void oj_trace_parse_call(const char *func, struct _parseInfo *pi, const char *file, int line, VALUE obj);
25
23
  extern void oj_trace_parse_hash_end(struct _parseInfo *pi, const char *file, int line);
26
24
  extern void oj_trace_parse_array_end(struct _parseInfo *pi, const char *file, int line);
27
25
 
26
+ #ifdef OJ_ENABLE_TRACE_LOG
27
+ #define TRACE(option, func, obj, depth, where) \
28
+ if (RB_UNLIKELY(Yes == option)) { \
29
+ oj_trace(func, obj, __FILE__, __LINE__, depth, where); \
30
+ }
31
+ #define TRACE_PARSE_IN(option, func, pi) \
32
+ if (RB_UNLIKELY(Yes == option)) { \
33
+ oj_trace_parse_in(func, pi, __FILE__, __LINE__); \
34
+ }
35
+ #define TRACE_PARSE_CALL(option, func, pi, obj) \
36
+ if (RB_UNLIKELY(Yes == option)) { \
37
+ oj_trace_parse_call(func, pi, __FILE__, __LINE__, obj); \
38
+ }
39
+ #define TRACE_PARSE_HASH_END(option, pi) \
40
+ if (RB_UNLIKELY(Yes == option)) { \
41
+ oj_trace_parse_hash_end(pi, __FILE__, __LINE__); \
42
+ }
43
+ #define TRACE_PARSE_ARRAY_END(option, pi) \
44
+ if (RB_UNLIKELY(Yes == option)) { \
45
+ oj_trace_parse_array_end(pi, __FILE__, __LINE__); \
46
+ }
47
+ #else
48
+ #define TRACE(option, func, obj, depth, where)
49
+ #define TRACE_PARSE_IN(option, func, pi)
50
+ #define TRACE_PARSE_CALL(option, func, pi, obj)
51
+ #define TRACE_PARSE_HASH_END(option, pi)
52
+ #define TRACE_PARSE_ARRAY_END(option, pi)
53
+ #endif
54
+
28
55
  #endif /* OJ_TRACE_H */