oj 3.7.4 → 3.13.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1352 -0
  3. data/README.md +29 -8
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +53 -72
  6. data/ext/oj/cache.c +326 -0
  7. data/ext/oj/cache.h +21 -0
  8. data/ext/oj/cache8.c +61 -64
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +37 -43
  11. data/ext/oj/circarray.h +16 -17
  12. data/ext/oj/code.c +165 -179
  13. data/ext/oj/code.h +27 -29
  14. data/ext/oj/compat.c +174 -194
  15. data/ext/oj/custom.c +809 -866
  16. data/ext/oj/debug.c +132 -0
  17. data/ext/oj/dump.c +848 -863
  18. data/ext/oj/dump.h +81 -67
  19. data/ext/oj/dump_compat.c +85 -123
  20. data/ext/oj/dump_leaf.c +100 -188
  21. data/ext/oj/dump_object.c +527 -656
  22. data/ext/oj/dump_strict.c +315 -338
  23. data/ext/oj/encode.h +7 -34
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +40 -29
  26. data/ext/oj/err.h +48 -48
  27. data/ext/oj/extconf.rb +17 -4
  28. data/ext/oj/fast.c +1070 -1087
  29. data/ext/oj/intern.c +301 -0
  30. data/ext/oj/intern.h +26 -0
  31. data/ext/oj/mimic_json.c +469 -436
  32. data/ext/oj/object.c +525 -593
  33. data/ext/oj/odd.c +154 -138
  34. data/ext/oj/odd.h +37 -38
  35. data/ext/oj/oj.c +1325 -986
  36. data/ext/oj/oj.h +333 -316
  37. data/ext/oj/parse.c +1002 -846
  38. data/ext/oj/parse.h +92 -87
  39. data/ext/oj/parser.c +1557 -0
  40. data/ext/oj/parser.h +91 -0
  41. data/ext/oj/rails.c +888 -878
  42. data/ext/oj/rails.h +11 -14
  43. data/ext/oj/reader.c +141 -147
  44. data/ext/oj/reader.h +73 -89
  45. data/ext/oj/resolve.c +41 -62
  46. data/ext/oj/resolve.h +7 -9
  47. data/ext/oj/rxclass.c +71 -75
  48. data/ext/oj/rxclass.h +18 -19
  49. data/ext/oj/saj.c +443 -486
  50. data/ext/oj/saj2.c +602 -0
  51. data/ext/oj/scp.c +88 -113
  52. data/ext/oj/sparse.c +787 -709
  53. data/ext/oj/stream_writer.c +133 -159
  54. data/ext/oj/strict.c +127 -118
  55. data/ext/oj/string_writer.c +230 -249
  56. data/ext/oj/trace.c +34 -41
  57. data/ext/oj/trace.h +19 -19
  58. data/ext/oj/usual.c +1254 -0
  59. data/ext/oj/util.c +136 -0
  60. data/ext/oj/util.h +20 -0
  61. data/ext/oj/val_stack.c +59 -67
  62. data/ext/oj/val_stack.h +91 -129
  63. data/ext/oj/validate.c +46 -0
  64. data/ext/oj/wab.c +342 -353
  65. data/lib/oj/bag.rb +1 -0
  66. data/lib/oj/easy_hash.rb +5 -4
  67. data/lib/oj/error.rb +1 -1
  68. data/lib/oj/json.rb +1 -1
  69. data/lib/oj/mimic.rb +48 -14
  70. data/lib/oj/saj.rb +20 -6
  71. data/lib/oj/state.rb +8 -7
  72. data/lib/oj/version.rb +2 -2
  73. data/lib/oj.rb +0 -8
  74. data/pages/Compatibility.md +1 -1
  75. data/pages/JsonGem.md +15 -0
  76. data/pages/Modes.md +53 -46
  77. data/pages/Options.md +72 -11
  78. data/pages/Parser.md +309 -0
  79. data/pages/Rails.md +73 -22
  80. data/pages/Security.md +1 -1
  81. data/test/activerecord/result_test.rb +7 -2
  82. data/test/activesupport5/abstract_unit.rb +45 -0
  83. data/test/activesupport5/decoding_test.rb +68 -60
  84. data/test/activesupport5/encoding_test.rb +111 -96
  85. data/test/activesupport5/encoding_test_cases.rb +33 -25
  86. data/test/activesupport5/test_helper.rb +43 -21
  87. data/test/activesupport5/time_zone_test_helpers.rb +18 -3
  88. data/test/activesupport6/abstract_unit.rb +44 -0
  89. data/test/activesupport6/decoding_test.rb +133 -0
  90. data/test/activesupport6/encoding_test.rb +507 -0
  91. data/test/activesupport6/encoding_test_cases.rb +98 -0
  92. data/test/activesupport6/test_common.rb +17 -0
  93. data/test/activesupport6/test_helper.rb +163 -0
  94. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  95. data/test/activesupport7/abstract_unit.rb +49 -0
  96. data/test/activesupport7/decoding_test.rb +125 -0
  97. data/test/activesupport7/encoding_test.rb +486 -0
  98. data/test/activesupport7/encoding_test_cases.rb +104 -0
  99. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  100. data/test/bar.rb +6 -12
  101. data/test/baz.rb +16 -0
  102. data/test/bug.rb +16 -0
  103. data/test/foo.rb +69 -75
  104. data/test/helper.rb +16 -0
  105. data/test/json_gem/json_common_interface_test.rb +8 -3
  106. data/test/json_gem/json_generator_test.rb +18 -4
  107. data/test/json_gem/json_parser_test.rb +9 -0
  108. data/test/json_gem/test_helper.rb +12 -0
  109. data/test/mem.rb +33 -0
  110. data/test/perf.rb +1 -1
  111. data/test/perf_dump.rb +50 -0
  112. data/test/perf_once.rb +58 -0
  113. data/test/perf_parser.rb +189 -0
  114. data/test/perf_scp.rb +11 -10
  115. data/test/perf_strict.rb +17 -23
  116. data/test/prec.rb +23 -0
  117. data/test/sample_json.rb +1 -1
  118. data/test/test_compat.rb +46 -10
  119. data/test/test_custom.rb +147 -8
  120. data/test/test_fast.rb +62 -2
  121. data/test/test_file.rb +25 -2
  122. data/test/test_gc.rb +13 -0
  123. data/test/test_generate.rb +21 -0
  124. data/test/test_hash.rb +11 -1
  125. data/test/test_integer_range.rb +7 -2
  126. data/test/test_object.rb +85 -9
  127. data/test/test_parser.rb +27 -0
  128. data/test/test_parser_saj.rb +335 -0
  129. data/test/test_parser_usual.rb +217 -0
  130. data/test/test_rails.rb +35 -0
  131. data/test/test_saj.rb +1 -1
  132. data/test/test_scp.rb +5 -5
  133. data/test/test_strict.rb +26 -1
  134. data/test/test_various.rb +87 -65
  135. data/test/test_wab.rb +2 -0
  136. data/test/test_writer.rb +19 -2
  137. data/test/tests.rb +1 -1
  138. data/test/zoo.rb +13 -0
  139. metadata +60 -110
  140. data/ext/oj/hash.c +0 -163
  141. data/ext/oj/hash.h +0 -46
  142. data/ext/oj/hash_test.c +0 -512
data/ext/oj/dump_leaf.c CHANGED
@@ -1,252 +1,164 @@
1
- /* dump_leaf.c
2
- * Copyright (c) 2012, 2017, Peter Ohler
3
- * All rights reserved.
4
- */
1
+ // Copyright (c) 2012, 2017 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
5
3
 
6
4
  #include <errno.h>
7
5
 
8
- #include "oj.h"
9
6
  #include "dump.h"
7
+ #include "oj.h"
10
8
 
11
- static void dump_leaf(Leaf leaf, int depth, Out out);
12
-
13
- static void
14
- grow(Out out, size_t len) {
15
- size_t size = out->end - out->buf;
16
- long pos = out->cur - out->buf;
17
- char *buf;
18
-
19
- size *= 2;
20
- if (size <= len * 2 + pos) {
21
- size += len;
22
- }
23
- if (out->allocated) {
24
- buf = REALLOC_N(out->buf, char, (size + BUFFER_EXTRA));
25
- } else {
26
- buf = ALLOC_N(char, (size + BUFFER_EXTRA));
27
- out->allocated = true;
28
- memcpy(buf, out->buf, out->end - out->buf + BUFFER_EXTRA);
29
- }
30
- if (0 == buf) {
31
- rb_raise(rb_eNoMemError, "Failed to create string. [%d:%s]\n", ENOSPC, strerror(ENOSPC));
32
- }
33
- out->buf = buf;
34
- out->end = buf + size;
35
- out->cur = out->buf + pos;
36
- }
37
-
9
+ static void dump_leaf(Leaf leaf, int depth, Out out);
38
10
 
39
- inline static void
40
- dump_chars(const char *s, size_t size, Out out) {
41
- if (out->end - out->cur <= (long)size) {
42
- grow(out, size);
43
- }
44
- memcpy(out->cur, s, size);
45
- out->cur += size;
11
+ inline static void dump_chars(const char *s, size_t size, Out out) {
12
+ assure_size(out, size);
13
+ APPEND_CHARS(out->cur, s, size);
46
14
  *out->cur = '\0';
47
15
  }
48
16
 
49
- static void
50
- dump_leaf_str(Leaf leaf, Out out) {
17
+ static void dump_leaf_str(Leaf leaf, Out out) {
51
18
  switch (leaf->value_type) {
52
- case STR_VAL:
53
- oj_dump_cstr(leaf->str, strlen(leaf->str), 0, 0, out);
54
- break;
19
+ case STR_VAL: oj_dump_cstr(leaf->str, strlen(leaf->str), 0, 0, out); break;
55
20
  case RUBY_VAL:
56
- oj_dump_cstr(rb_string_value_cstr(&leaf->value), (int)RSTRING_LEN(leaf->value), 0, 0, out);
57
- break;
21
+ oj_dump_cstr(rb_string_value_cstr(&leaf->value), (int)RSTRING_LEN(leaf->value), 0, 0, out);
22
+ break;
58
23
  case COL_VAL:
59
- default:
60
- rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
61
- break;
24
+ default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
62
25
  }
63
26
  }
64
27
 
65
- static void
66
- dump_leaf_fixnum(Leaf leaf, Out out) {
28
+ static void dump_leaf_fixnum(Leaf leaf, Out out) {
67
29
  switch (leaf->value_type) {
68
- case STR_VAL:
69
- dump_chars(leaf->str, strlen(leaf->str), out);
70
- break;
30
+ case STR_VAL: dump_chars(leaf->str, strlen(leaf->str), out); break;
71
31
  case RUBY_VAL:
72
- if (T_BIGNUM == rb_type(leaf->value)) {
73
- oj_dump_bignum(leaf->value, 0, out, false);
74
- } else {
75
- oj_dump_fixnum(leaf->value, 0, out, false);
76
- }
77
- break;
32
+ if (T_BIGNUM == rb_type(leaf->value)) {
33
+ oj_dump_bignum(leaf->value, 0, out, false);
34
+ } else {
35
+ oj_dump_fixnum(leaf->value, 0, out, false);
36
+ }
37
+ break;
78
38
  case COL_VAL:
79
- default:
80
- rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
81
- break;
39
+ default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
82
40
  }
83
41
  }
84
42
 
85
- static void
86
- dump_leaf_float(Leaf leaf, Out out) {
43
+ static void dump_leaf_float(Leaf leaf, Out out) {
87
44
  switch (leaf->value_type) {
88
- case STR_VAL:
89
- dump_chars(leaf->str, strlen(leaf->str), out);
90
- break;
91
- case RUBY_VAL:
92
- oj_dump_float(leaf->value, 0, out, false);
93
- break;
45
+ case STR_VAL: dump_chars(leaf->str, strlen(leaf->str), out); break;
46
+ case RUBY_VAL: oj_dump_float(leaf->value, 0, out, false); break;
94
47
  case COL_VAL:
95
- default:
96
- rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
97
- break;
48
+ default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
98
49
  }
99
50
  }
100
51
 
101
- static void
102
- dump_leaf_array(Leaf leaf, int depth, Out out) {
103
- size_t size;
104
- int d2 = depth + 1;
52
+ static void dump_leaf_array(Leaf leaf, int depth, Out out) {
53
+ size_t size;
54
+ int d2 = depth + 1;
105
55
 
106
56
  size = 2;
107
- if (out->end - out->cur <= (long)size) {
108
- grow(out, size);
109
- }
57
+ assure_size(out, size);
110
58
  *out->cur++ = '[';
111
59
  if (0 == leaf->elements) {
112
- *out->cur++ = ']';
60
+ *out->cur++ = ']';
113
61
  } else {
114
- Leaf first = leaf->elements->next;
115
- Leaf e = first;
116
-
117
- size = d2 * out->indent + 2;
118
- do {
119
- if (out->end - out->cur <= (long)size) {
120
- grow(out, size);
121
- }
122
- fill_indent(out, d2);
123
- dump_leaf(e, d2, out);
124
- if (e->next != first) {
125
- *out->cur++ = ',';
126
- }
127
- e = e->next;
128
- } while (e != first);
129
- size = depth * out->indent + 1;
130
- if (out->end - out->cur <= (long)size) {
131
- grow(out, size);
132
- }
133
- fill_indent(out, depth);
134
- *out->cur++ = ']';
62
+ Leaf first = leaf->elements->next;
63
+ Leaf e = first;
64
+
65
+ size = d2 * out->indent + 2;
66
+ do {
67
+ assure_size(out, size);
68
+ fill_indent(out, d2);
69
+ dump_leaf(e, d2, out);
70
+ if (e->next != first) {
71
+ *out->cur++ = ',';
72
+ }
73
+ e = e->next;
74
+ } while (e != first);
75
+ size = depth * out->indent + 1;
76
+ assure_size(out, size);
77
+ fill_indent(out, depth);
78
+ *out->cur++ = ']';
135
79
  }
136
80
  *out->cur = '\0';
137
81
  }
138
82
 
139
- static void
140
- dump_leaf_hash(Leaf leaf, int depth, Out out) {
141
- size_t size;
142
- int d2 = depth + 1;
83
+ static void dump_leaf_hash(Leaf leaf, int depth, Out out) {
84
+ size_t size;
85
+ int d2 = depth + 1;
143
86
 
144
87
  size = 2;
145
- if (out->end - out->cur <= (long)size) {
146
- grow(out, size);
147
- }
88
+ assure_size(out, size);
148
89
  *out->cur++ = '{';
149
90
  if (0 == leaf->elements) {
150
- *out->cur++ = '}';
91
+ *out->cur++ = '}';
151
92
  } else {
152
- Leaf first = leaf->elements->next;
153
- Leaf e = first;
154
-
155
- size = d2 * out->indent + 2;
156
- do {
157
- if (out->end - out->cur <= (long)size) {
158
- grow(out, size);
159
- }
160
- fill_indent(out, d2);
161
- oj_dump_cstr(e->key, strlen(e->key), 0, 0, out);
162
- *out->cur++ = ':';
163
- dump_leaf(e, d2, out);
164
- if (e->next != first) {
165
- *out->cur++ = ',';
166
- }
167
- e = e->next;
168
- } while (e != first);
169
- size = depth * out->indent + 1;
170
- if (out->end - out->cur <= (long)size) {
171
- grow(out, size);
172
- }
173
- fill_indent(out, depth);
174
- *out->cur++ = '}';
93
+ Leaf first = leaf->elements->next;
94
+ Leaf e = first;
95
+
96
+ size = d2 * out->indent + 2;
97
+ do {
98
+ assure_size(out, size);
99
+ fill_indent(out, d2);
100
+ oj_dump_cstr(e->key, strlen(e->key), 0, 0, out);
101
+ *out->cur++ = ':';
102
+ dump_leaf(e, d2, out);
103
+ if (e->next != first) {
104
+ *out->cur++ = ',';
105
+ }
106
+ e = e->next;
107
+ } while (e != first);
108
+ size = depth * out->indent + 1;
109
+ assure_size(out, size);
110
+ fill_indent(out, depth);
111
+ *out->cur++ = '}';
175
112
  }
176
113
  *out->cur = '\0';
177
114
  }
178
115
 
179
- static void
180
- dump_leaf(Leaf leaf, int depth, Out out) {
116
+ static void dump_leaf(Leaf leaf, int depth, Out out) {
181
117
  switch (leaf->rtype) {
182
- case T_NIL:
183
- oj_dump_nil(Qnil, 0, out, false);
184
- break;
185
- case T_TRUE:
186
- oj_dump_true(Qtrue, 0, out, false);
187
- break;
188
- case T_FALSE:
189
- oj_dump_false(Qfalse, 0, out, false);
190
- break;
191
- case T_STRING:
192
- dump_leaf_str(leaf, out);
193
- break;
194
- case T_FIXNUM:
195
- dump_leaf_fixnum(leaf, out);
196
- break;
197
- case T_FLOAT:
198
- dump_leaf_float(leaf, out);
199
- break;
200
- case T_ARRAY:
201
- dump_leaf_array(leaf, depth, out);
202
- break;
203
- case T_HASH:
204
- dump_leaf_hash(leaf, depth, out);
205
- break;
206
- default:
207
- rb_raise(rb_eTypeError, "Unexpected type %02x.\n", leaf->rtype);
208
- break;
118
+ case T_NIL: oj_dump_nil(Qnil, 0, out, false); break;
119
+ case T_TRUE: oj_dump_true(Qtrue, 0, out, false); break;
120
+ case T_FALSE: oj_dump_false(Qfalse, 0, out, false); break;
121
+ case T_STRING: dump_leaf_str(leaf, out); break;
122
+ case T_FIXNUM: dump_leaf_fixnum(leaf, out); break;
123
+ case T_FLOAT: dump_leaf_float(leaf, out); break;
124
+ case T_ARRAY: dump_leaf_array(leaf, depth, out); break;
125
+ case T_HASH: dump_leaf_hash(leaf, depth, out); break;
126
+ default: rb_raise(rb_eTypeError, "Unexpected type %02x.\n", leaf->rtype); break;
209
127
  }
210
128
  }
211
129
 
212
- void
213
- oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
130
+ void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
214
131
  if (0 == out->buf) {
215
- out->buf = ALLOC_N(char, 4096);
216
- out->end = out->buf + 4095 - BUFFER_EXTRA; // 1 less than end plus extra for possible errors
217
- out->allocated = true;
132
+ oj_out_init(out);
218
133
  }
219
- out->cur = out->buf;
134
+ out->cur = out->buf;
220
135
  out->circ_cnt = 0;
221
- out->opts = copts;
136
+ out->opts = copts;
222
137
  out->hash_cnt = 0;
223
- out->indent = copts->indent;
138
+ out->indent = copts->indent;
224
139
  dump_leaf(leaf, 0, out);
225
140
  }
226
141
 
227
- void
228
- oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
229
- char buf[4096];
230
- struct _Out out;
231
- size_t size;
232
- FILE *f;
233
-
234
- out.buf = buf;
235
- out.end = buf + sizeof(buf) - BUFFER_EXTRA;
236
- out.allocated = false;
237
- out.omit_nil = copts->dump_opts.omit_nil;
142
+ void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
143
+ struct _out out;
144
+ size_t size;
145
+ FILE * f;
146
+
147
+ oj_out_init(&out);
148
+
149
+ out.omit_nil = copts->dump_opts.omit_nil;
238
150
  oj_dump_leaf_to_json(leaf, copts, &out);
239
151
  size = out.cur - out.buf;
240
152
  if (0 == (f = fopen(path, "w"))) {
241
- rb_raise(rb_eIOError, "%s\n", strerror(errno));
153
+ rb_raise(rb_eIOError, "%s\n", strerror(errno));
242
154
  }
243
155
  if (size != fwrite(out.buf, 1, size, f)) {
244
- int err = ferror(f);
156
+ int err = ferror(f);
245
157
 
246
- rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
247
- }
248
- if (out.allocated) {
249
- xfree(out.buf);
158
+ rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
250
159
  }
160
+
161
+ oj_out_free(&out);
162
+
251
163
  fclose(f);
252
164
  }