oj 3.7.4 → 3.13.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1352 -0
- data/README.md +29 -8
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +53 -72
- data/ext/oj/cache.c +326 -0
- data/ext/oj/cache.h +21 -0
- data/ext/oj/cache8.c +61 -64
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +37 -43
- data/ext/oj/circarray.h +16 -17
- data/ext/oj/code.c +165 -179
- data/ext/oj/code.h +27 -29
- data/ext/oj/compat.c +174 -194
- data/ext/oj/custom.c +809 -866
- data/ext/oj/debug.c +132 -0
- data/ext/oj/dump.c +848 -863
- data/ext/oj/dump.h +81 -67
- data/ext/oj/dump_compat.c +85 -123
- data/ext/oj/dump_leaf.c +100 -188
- data/ext/oj/dump_object.c +527 -656
- data/ext/oj/dump_strict.c +315 -338
- data/ext/oj/encode.h +7 -34
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +40 -29
- data/ext/oj/err.h +48 -48
- data/ext/oj/extconf.rb +17 -4
- data/ext/oj/fast.c +1070 -1087
- data/ext/oj/intern.c +301 -0
- data/ext/oj/intern.h +26 -0
- data/ext/oj/mimic_json.c +469 -436
- data/ext/oj/object.c +525 -593
- data/ext/oj/odd.c +154 -138
- data/ext/oj/odd.h +37 -38
- data/ext/oj/oj.c +1325 -986
- data/ext/oj/oj.h +333 -316
- data/ext/oj/parse.c +1002 -846
- data/ext/oj/parse.h +92 -87
- data/ext/oj/parser.c +1557 -0
- data/ext/oj/parser.h +91 -0
- data/ext/oj/rails.c +888 -878
- data/ext/oj/rails.h +11 -14
- data/ext/oj/reader.c +141 -147
- data/ext/oj/reader.h +73 -89
- data/ext/oj/resolve.c +41 -62
- data/ext/oj/resolve.h +7 -9
- data/ext/oj/rxclass.c +71 -75
- data/ext/oj/rxclass.h +18 -19
- data/ext/oj/saj.c +443 -486
- data/ext/oj/saj2.c +602 -0
- data/ext/oj/scp.c +88 -113
- data/ext/oj/sparse.c +787 -709
- data/ext/oj/stream_writer.c +133 -159
- data/ext/oj/strict.c +127 -118
- data/ext/oj/string_writer.c +230 -249
- data/ext/oj/trace.c +34 -41
- data/ext/oj/trace.h +19 -19
- data/ext/oj/usual.c +1254 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +59 -67
- data/ext/oj/val_stack.h +91 -129
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +342 -353
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +5 -4
- data/lib/oj/error.rb +1 -1
- data/lib/oj/json.rb +1 -1
- data/lib/oj/mimic.rb +48 -14
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/state.rb +8 -7
- data/lib/oj/version.rb +2 -2
- data/lib/oj.rb +0 -8
- data/pages/Compatibility.md +1 -1
- data/pages/JsonGem.md +15 -0
- data/pages/Modes.md +53 -46
- data/pages/Options.md +72 -11
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +73 -22
- data/pages/Security.md +1 -1
- data/test/activerecord/result_test.rb +7 -2
- data/test/activesupport5/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +68 -60
- data/test/activesupport5/encoding_test.rb +111 -96
- data/test/activesupport5/encoding_test_cases.rb +33 -25
- data/test/activesupport5/test_helper.rb +43 -21
- data/test/activesupport5/time_zone_test_helpers.rb +18 -3
- data/test/activesupport6/abstract_unit.rb +44 -0
- data/test/activesupport6/decoding_test.rb +133 -0
- data/test/activesupport6/encoding_test.rb +507 -0
- data/test/activesupport6/encoding_test_cases.rb +98 -0
- data/test/activesupport6/test_common.rb +17 -0
- data/test/activesupport6/test_helper.rb +163 -0
- data/test/activesupport6/time_zone_test_helpers.rb +39 -0
- data/test/activesupport7/abstract_unit.rb +49 -0
- data/test/activesupport7/decoding_test.rb +125 -0
- data/test/activesupport7/encoding_test.rb +486 -0
- data/test/activesupport7/encoding_test_cases.rb +104 -0
- data/test/activesupport7/time_zone_test_helpers.rb +47 -0
- data/test/bar.rb +6 -12
- data/test/baz.rb +16 -0
- data/test/bug.rb +16 -0
- data/test/foo.rb +69 -75
- data/test/helper.rb +16 -0
- data/test/json_gem/json_common_interface_test.rb +8 -3
- data/test/json_gem/json_generator_test.rb +18 -4
- data/test/json_gem/json_parser_test.rb +9 -0
- data/test/json_gem/test_helper.rb +12 -0
- data/test/mem.rb +33 -0
- data/test/perf.rb +1 -1
- data/test/perf_dump.rb +50 -0
- data/test/perf_once.rb +58 -0
- data/test/perf_parser.rb +189 -0
- data/test/perf_scp.rb +11 -10
- data/test/perf_strict.rb +17 -23
- data/test/prec.rb +23 -0
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +46 -10
- data/test/test_custom.rb +147 -8
- data/test/test_fast.rb +62 -2
- data/test/test_file.rb +25 -2
- data/test/test_gc.rb +13 -0
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +11 -1
- data/test/test_integer_range.rb +7 -2
- data/test/test_object.rb +85 -9
- data/test/test_parser.rb +27 -0
- data/test/test_parser_saj.rb +335 -0
- data/test/test_parser_usual.rb +217 -0
- data/test/test_rails.rb +35 -0
- data/test/test_saj.rb +1 -1
- data/test/test_scp.rb +5 -5
- data/test/test_strict.rb +26 -1
- data/test/test_various.rb +87 -65
- data/test/test_wab.rb +2 -0
- data/test/test_writer.rb +19 -2
- data/test/tests.rb +1 -1
- data/test/zoo.rb +13 -0
- metadata +60 -110
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
data/ext/oj/dump_leaf.c
CHANGED
@@ -1,252 +1,164 @@
|
|
1
|
-
|
2
|
-
|
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
|
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
|
-
|
41
|
-
|
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
|
-
|
57
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
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
|
-
|
103
|
-
|
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
|
-
|
108
|
-
grow(out, size);
|
109
|
-
}
|
57
|
+
assure_size(out, size);
|
110
58
|
*out->cur++ = '[';
|
111
59
|
if (0 == leaf->elements) {
|
112
|
-
|
60
|
+
*out->cur++ = ']';
|
113
61
|
} else {
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
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
|
-
|
141
|
-
|
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
|
-
|
146
|
-
grow(out, size);
|
147
|
-
}
|
88
|
+
assure_size(out, size);
|
148
89
|
*out->cur++ = '{';
|
149
90
|
if (0 == leaf->elements) {
|
150
|
-
|
91
|
+
*out->cur++ = '}';
|
151
92
|
} else {
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
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
|
-
|
184
|
-
|
185
|
-
case
|
186
|
-
|
187
|
-
|
188
|
-
case
|
189
|
-
|
190
|
-
|
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
|
-
|
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
|
134
|
+
out->cur = out->buf;
|
220
135
|
out->circ_cnt = 0;
|
221
|
-
out->opts
|
136
|
+
out->opts = copts;
|
222
137
|
out->hash_cnt = 0;
|
223
|
-
out->indent
|
138
|
+
out->indent = copts->indent;
|
224
139
|
dump_leaf(leaf, 0, out);
|
225
140
|
}
|
226
141
|
|
227
|
-
void
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
out.
|
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
|
-
|
153
|
+
rb_raise(rb_eIOError, "%s\n", strerror(errno));
|
242
154
|
}
|
243
155
|
if (size != fwrite(out.buf, 1, size, f)) {
|
244
|
-
|
156
|
+
int err = ferror(f);
|
245
157
|
|
246
|
-
|
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
|
}
|