oj 3.13.9 → 3.16.1

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 (161) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +101 -0
  3. data/README.md +13 -2
  4. data/ext/oj/buf.h +11 -6
  5. data/ext/oj/cache.c +25 -24
  6. data/ext/oj/cache8.c +10 -9
  7. data/ext/oj/circarray.c +8 -6
  8. data/ext/oj/circarray.h +2 -2
  9. data/ext/oj/code.c +19 -33
  10. data/ext/oj/code.h +2 -2
  11. data/ext/oj/compat.c +20 -60
  12. data/ext/oj/custom.c +76 -155
  13. data/ext/oj/debug.c +3 -9
  14. data/ext/oj/dump.c +203 -213
  15. data/ext/oj/dump.h +26 -12
  16. data/ext/oj/dump_compat.c +565 -642
  17. data/ext/oj/dump_leaf.c +17 -63
  18. data/ext/oj/dump_object.c +59 -181
  19. data/ext/oj/dump_strict.c +24 -48
  20. data/ext/oj/encoder.c +43 -0
  21. data/ext/oj/err.c +2 -13
  22. data/ext/oj/err.h +9 -12
  23. data/ext/oj/extconf.rb +18 -7
  24. data/ext/oj/fast.c +83 -108
  25. data/ext/oj/intern.c +52 -50
  26. data/ext/oj/intern.h +4 -8
  27. data/ext/oj/mem.c +318 -0
  28. data/ext/oj/mem.h +53 -0
  29. data/ext/oj/mimic_json.c +104 -81
  30. data/ext/oj/object.c +50 -67
  31. data/ext/oj/odd.c +89 -67
  32. data/ext/oj/odd.h +15 -15
  33. data/ext/oj/oj.c +171 -106
  34. data/ext/oj/oj.h +96 -74
  35. data/ext/oj/parse.c +169 -189
  36. data/ext/oj/parse.h +23 -24
  37. data/ext/oj/parser.c +89 -34
  38. data/ext/oj/parser.h +20 -9
  39. data/ext/oj/rails.c +86 -151
  40. data/ext/oj/rails.h +1 -1
  41. data/ext/oj/reader.c +12 -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 +21 -32
  47. data/ext/oj/saj2.c +329 -93
  48. data/ext/oj/saj2.h +23 -0
  49. data/ext/oj/scp.c +3 -14
  50. data/ext/oj/sparse.c +26 -70
  51. data/ext/oj/stream_writer.c +12 -22
  52. data/ext/oj/strict.c +20 -52
  53. data/ext/oj/string_writer.c +21 -22
  54. data/ext/oj/trace.h +31 -4
  55. data/ext/oj/usual.c +105 -150
  56. data/ext/oj/usual.h +68 -0
  57. data/ext/oj/util.h +1 -1
  58. data/ext/oj/val_stack.c +1 -1
  59. data/ext/oj/val_stack.h +8 -7
  60. data/ext/oj/validate.c +21 -26
  61. data/ext/oj/wab.c +32 -69
  62. data/lib/oj/active_support_helper.rb +1 -3
  63. data/lib/oj/bag.rb +7 -1
  64. data/lib/oj/easy_hash.rb +4 -5
  65. data/lib/oj/error.rb +0 -1
  66. data/lib/oj/json.rb +162 -150
  67. data/lib/oj/mimic.rb +6 -2
  68. data/lib/oj/saj.rb +20 -6
  69. data/lib/oj/state.rb +9 -6
  70. data/lib/oj/version.rb +1 -2
  71. data/lib/oj.rb +2 -0
  72. data/pages/Compatibility.md +1 -1
  73. data/pages/InstallOptions.md +20 -0
  74. data/pages/JsonGem.md +15 -0
  75. data/pages/Modes.md +6 -3
  76. data/pages/Options.md +10 -0
  77. data/pages/Rails.md +12 -0
  78. data/test/_test_active.rb +8 -9
  79. data/test/_test_active_mimic.rb +7 -8
  80. data/test/_test_mimic_rails.rb +17 -20
  81. data/test/activerecord/result_test.rb +5 -6
  82. data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
  83. data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
  84. data/test/{activesupport5 → activesupport7}/encoding_test.rb +20 -34
  85. data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
  86. data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
  87. data/test/files.rb +15 -15
  88. data/test/foo.rb +15 -15
  89. data/test/helper.rb +11 -8
  90. data/test/isolated/shared.rb +3 -2
  91. data/test/json_gem/json_addition_test.rb +2 -2
  92. data/test/json_gem/json_common_interface_test.rb +8 -6
  93. data/test/json_gem/json_encoding_test.rb +0 -0
  94. data/test/json_gem/json_ext_parser_test.rb +1 -0
  95. data/test/json_gem/json_fixtures_test.rb +3 -2
  96. data/test/json_gem/json_generator_test.rb +49 -37
  97. data/test/json_gem/json_generic_object_test.rb +11 -11
  98. data/test/json_gem/json_parser_test.rb +54 -47
  99. data/test/json_gem/json_string_matching_test.rb +9 -9
  100. data/test/json_gem/test_helper.rb +7 -3
  101. data/test/mem.rb +13 -12
  102. data/test/perf.rb +21 -26
  103. data/test/perf_compat.rb +31 -33
  104. data/test/perf_dump.rb +50 -0
  105. data/test/perf_fast.rb +80 -82
  106. data/test/perf_file.rb +27 -29
  107. data/test/perf_object.rb +65 -69
  108. data/test/perf_once.rb +12 -11
  109. data/test/perf_parser.rb +42 -48
  110. data/test/perf_saj.rb +46 -54
  111. data/test/perf_scp.rb +57 -69
  112. data/test/perf_simple.rb +41 -39
  113. data/test/perf_strict.rb +68 -70
  114. data/test/perf_wab.rb +67 -69
  115. data/test/prec.rb +3 -3
  116. data/test/sample/change.rb +0 -1
  117. data/test/sample/dir.rb +0 -1
  118. data/test/sample/doc.rb +0 -1
  119. data/test/sample/file.rb +0 -1
  120. data/test/sample/group.rb +0 -1
  121. data/test/sample/hasprops.rb +0 -1
  122. data/test/sample/layer.rb +0 -1
  123. data/test/sample/rect.rb +0 -1
  124. data/test/sample/shape.rb +0 -1
  125. data/test/sample/text.rb +0 -1
  126. data/test/sample.rb +16 -16
  127. data/test/sample_json.rb +8 -8
  128. data/test/test_compat.rb +95 -43
  129. data/test/test_custom.rb +72 -51
  130. data/test/test_debian.rb +7 -10
  131. data/test/test_fast.rb +102 -87
  132. data/test/test_file.rb +41 -30
  133. data/test/test_gc.rb +16 -5
  134. data/test/test_generate.rb +5 -5
  135. data/test/test_hash.rb +4 -4
  136. data/test/test_integer_range.rb +9 -9
  137. data/test/test_null.rb +20 -20
  138. data/test/test_object.rb +85 -96
  139. data/test/test_parser.rb +6 -22
  140. data/test/test_parser_debug.rb +27 -0
  141. data/test/test_parser_saj.rb +115 -23
  142. data/test/test_parser_usual.rb +6 -6
  143. data/test/test_rails.rb +2 -2
  144. data/test/test_saj.rb +10 -8
  145. data/test/test_scp.rb +37 -39
  146. data/test/test_strict.rb +40 -32
  147. data/test/test_various.rb +163 -84
  148. data/test/test_wab.rb +48 -44
  149. data/test/test_writer.rb +47 -47
  150. data/test/tests.rb +13 -5
  151. data/test/tests_mimic.rb +12 -3
  152. data/test/tests_mimic_addition.rb +12 -3
  153. metadata +34 -144
  154. data/test/activesupport4/decoding_test.rb +0 -108
  155. data/test/activesupport4/encoding_test.rb +0 -531
  156. data/test/activesupport4/test_helper.rb +0 -41
  157. data/test/activesupport5/test_helper.rb +0 -72
  158. data/test/bar.rb +0 -16
  159. data/test/baz.rb +0 -16
  160. data/test/bug.rb +0 -16
  161. data/test/zoo.rb +0 -13
data/ext/oj/dump_leaf.c CHANGED
@@ -8,45 +8,16 @@
8
8
 
9
9
  static void dump_leaf(Leaf leaf, int depth, Out out);
10
10
 
11
- static void grow(Out out, size_t len) {
12
- size_t size = out->end - out->buf;
13
- long pos = out->cur - out->buf;
14
- char * buf;
15
-
16
- size *= 2;
17
- if (size <= len * 2 + pos) {
18
- size += len;
19
- }
20
- if (out->allocated) {
21
- buf = REALLOC_N(out->buf, char, (size + BUFFER_EXTRA));
22
- } else {
23
- buf = ALLOC_N(char, (size + BUFFER_EXTRA));
24
- out->allocated = true;
25
- memcpy(buf, out->buf, out->end - out->buf + BUFFER_EXTRA);
26
- }
27
- if (0 == buf) {
28
- rb_raise(rb_eNoMemError, "Failed to create string. [%d:%s]\n", ENOSPC, strerror(ENOSPC));
29
- }
30
- out->buf = buf;
31
- out->end = buf + size;
32
- out->cur = out->buf + pos;
33
- }
34
-
35
11
  inline static void dump_chars(const char *s, size_t size, Out out) {
36
- if (out->end - out->cur <= (long)size) {
37
- grow(out, size);
38
- }
39
- memcpy(out->cur, s, size);
40
- out->cur += size;
12
+ assure_size(out, size);
13
+ APPEND_CHARS(out->cur, s, size);
41
14
  *out->cur = '\0';
42
15
  }
43
16
 
44
17
  static void dump_leaf_str(Leaf leaf, Out out) {
45
18
  switch (leaf->value_type) {
46
19
  case STR_VAL: oj_dump_cstr(leaf->str, strlen(leaf->str), 0, 0, out); break;
47
- case RUBY_VAL:
48
- oj_dump_cstr(rb_string_value_cstr(&leaf->value), (int)RSTRING_LEN(leaf->value), 0, 0, out);
49
- break;
20
+ case RUBY_VAL: oj_dump_cstr(StringValueCStr(leaf->value), (int)RSTRING_LEN(leaf->value), 0, 0, out); break;
50
21
  case COL_VAL:
51
22
  default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
52
23
  }
@@ -81,9 +52,7 @@ static void dump_leaf_array(Leaf leaf, int depth, Out out) {
81
52
  int d2 = depth + 1;
82
53
 
83
54
  size = 2;
84
- if (out->end - out->cur <= (long)size) {
85
- grow(out, size);
86
- }
55
+ assure_size(out, size);
87
56
  *out->cur++ = '[';
88
57
  if (0 == leaf->elements) {
89
58
  *out->cur++ = ']';
@@ -93,9 +62,7 @@ static void dump_leaf_array(Leaf leaf, int depth, Out out) {
93
62
 
94
63
  size = d2 * out->indent + 2;
95
64
  do {
96
- if (out->end - out->cur <= (long)size) {
97
- grow(out, size);
98
- }
65
+ assure_size(out, size);
99
66
  fill_indent(out, d2);
100
67
  dump_leaf(e, d2, out);
101
68
  if (e->next != first) {
@@ -104,9 +71,7 @@ static void dump_leaf_array(Leaf leaf, int depth, Out out) {
104
71
  e = e->next;
105
72
  } while (e != first);
106
73
  size = depth * out->indent + 1;
107
- if (out->end - out->cur <= (long)size) {
108
- grow(out, size);
109
- }
74
+ assure_size(out, size);
110
75
  fill_indent(out, depth);
111
76
  *out->cur++ = ']';
112
77
  }
@@ -118,9 +83,7 @@ static void dump_leaf_hash(Leaf leaf, int depth, Out out) {
118
83
  int d2 = depth + 1;
119
84
 
120
85
  size = 2;
121
- if (out->end - out->cur <= (long)size) {
122
- grow(out, size);
123
- }
86
+ assure_size(out, size);
124
87
  *out->cur++ = '{';
125
88
  if (0 == leaf->elements) {
126
89
  *out->cur++ = '}';
@@ -130,9 +93,7 @@ static void dump_leaf_hash(Leaf leaf, int depth, Out out) {
130
93
 
131
94
  size = d2 * out->indent + 2;
132
95
  do {
133
- if (out->end - out->cur <= (long)size) {
134
- grow(out, size);
135
- }
96
+ assure_size(out, size);
136
97
  fill_indent(out, d2);
137
98
  oj_dump_cstr(e->key, strlen(e->key), 0, 0, out);
138
99
  *out->cur++ = ':';
@@ -143,9 +104,7 @@ static void dump_leaf_hash(Leaf leaf, int depth, Out out) {
143
104
  e = e->next;
144
105
  } while (e != first);
145
106
  size = depth * out->indent + 1;
146
- if (out->end - out->cur <= (long)size) {
147
- grow(out, size);
148
- }
107
+ assure_size(out, size);
149
108
  fill_indent(out, depth);
150
109
  *out->cur++ = '}';
151
110
  }
@@ -168,10 +127,7 @@ static void dump_leaf(Leaf leaf, int depth, Out out) {
168
127
 
169
128
  void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
170
129
  if (0 == out->buf) {
171
- out->buf = ALLOC_N(char, 4096);
172
- out->end = out->buf + 4095 -
173
- BUFFER_EXTRA; // 1 less than end plus extra for possible errors
174
- out->allocated = true;
130
+ oj_out_init(out);
175
131
  }
176
132
  out->cur = out->buf;
177
133
  out->circ_cnt = 0;
@@ -182,15 +138,13 @@ void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
182
138
  }
183
139
 
184
140
  void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
185
- char buf[4096];
186
141
  struct _out out;
187
142
  size_t size;
188
- FILE * f;
143
+ FILE *f;
189
144
 
190
- out.buf = buf;
191
- out.end = buf + sizeof(buf) - BUFFER_EXTRA;
192
- out.allocated = false;
193
- out.omit_nil = copts->dump_opts.omit_nil;
145
+ oj_out_init(&out);
146
+
147
+ out.omit_nil = copts->dump_opts.omit_nil;
194
148
  oj_dump_leaf_to_json(leaf, copts, &out);
195
149
  size = out.cur - out.buf;
196
150
  if (0 == (f = fopen(path, "w"))) {
@@ -201,8 +155,8 @@ void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
201
155
 
202
156
  rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
203
157
  }
204
- if (out.allocated) {
205
- xfree(out.buf);
206
- }
158
+
159
+ oj_out_free(&out);
160
+
207
161
  fclose(f);
208
162
  }