oj 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +17 -23
- data/README.md +74 -425
- data/ext/oj/buf.h +103 -0
- data/ext/oj/cache8.c +4 -0
- data/ext/oj/circarray.c +68 -0
- data/ext/oj/circarray.h +23 -0
- data/ext/oj/code.c +227 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +243 -0
- data/ext/oj/custom.c +1097 -0
- data/ext/oj/dump.c +766 -1534
- data/ext/oj/dump.h +92 -0
- data/ext/oj/dump_compat.c +937 -0
- data/ext/oj/dump_leaf.c +254 -0
- data/ext/oj/dump_object.c +810 -0
- data/ext/oj/dump_rails.c +329 -0
- data/ext/oj/dump_strict.c +416 -0
- data/ext/oj/encode.h +51 -0
- data/ext/oj/err.c +57 -0
- data/ext/oj/err.h +70 -0
- data/ext/oj/extconf.rb +17 -7
- data/ext/oj/fast.c +213 -180
- data/ext/oj/hash.c +163 -0
- data/ext/oj/hash.h +46 -0
- data/ext/oj/hash_test.c +512 -0
- data/ext/oj/mimic_json.c +817 -0
- data/ext/oj/mimic_rails.c +806 -0
- data/ext/oj/mimic_rails.h +17 -0
- data/ext/oj/object.c +752 -0
- data/ext/oj/odd.c +230 -0
- data/ext/oj/odd.h +44 -0
- data/ext/oj/oj.c +1288 -929
- data/ext/oj/oj.h +240 -69
- data/ext/oj/parse.c +1014 -0
- data/ext/oj/parse.h +92 -0
- data/ext/oj/reader.c +223 -0
- data/ext/oj/reader.h +151 -0
- data/ext/oj/resolve.c +127 -0
- data/ext/oj/{cache.h → resolve.h} +6 -13
- data/ext/oj/rxclass.c +133 -0
- data/ext/oj/rxclass.h +27 -0
- data/ext/oj/saj.c +77 -175
- data/ext/oj/scp.c +224 -0
- data/ext/oj/sparse.c +911 -0
- data/ext/oj/stream_writer.c +301 -0
- data/ext/oj/strict.c +162 -0
- data/ext/oj/string_writer.c +480 -0
- data/ext/oj/val_stack.c +98 -0
- data/ext/oj/val_stack.h +188 -0
- data/lib/oj/active_support_helper.rb +41 -0
- data/lib/oj/bag.rb +6 -10
- data/lib/oj/easy_hash.rb +52 -0
- data/lib/oj/json.rb +172 -0
- data/lib/oj/mimic.rb +260 -5
- data/lib/oj/saj.rb +13 -10
- data/lib/oj/schandler.rb +142 -0
- data/lib/oj/state.rb +131 -0
- data/lib/oj/version.rb +1 -1
- data/lib/oj.rb +11 -23
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/JsonGem.md +79 -0
- data/pages/Modes.md +140 -0
- data/pages/Options.md +250 -0
- data/pages/Rails.md +60 -0
- data/pages/Security.md +20 -0
- data/test/_test_active.rb +76 -0
- data/test/_test_active_mimic.rb +96 -0
- data/test/_test_mimic_rails.rb +126 -0
- data/test/activesupport4/decoding_test.rb +105 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/decoding_test.rb +125 -0
- data/test/activesupport5/encoding_test.rb +483 -0
- data/test/activesupport5/encoding_test_cases.rb +90 -0
- data/test/activesupport5/test_helper.rb +50 -0
- data/test/activesupport5/time_zone_test_helpers.rb +24 -0
- data/test/helper.rb +27 -0
- data/test/isolated/shared.rb +310 -0
- data/test/isolated/test_mimic_after.rb +13 -0
- data/test/isolated/test_mimic_alone.rb +12 -0
- data/test/isolated/test_mimic_as_json.rb +45 -0
- data/test/isolated/test_mimic_before.rb +13 -0
- data/test/isolated/test_mimic_define.rb +28 -0
- data/test/isolated/test_mimic_rails_after.rb +22 -0
- data/test/isolated/test_mimic_rails_before.rb +21 -0
- data/test/isolated/test_mimic_redefine.rb +15 -0
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +143 -0
- data/test/json_gem/json_encoding_test.rb +109 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +383 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +18 -0
- data/test/perf_compat.rb +130 -0
- data/test/perf_fast.rb +9 -9
- data/test/perf_file.rb +64 -0
- data/test/{perf_obj.rb → perf_object.rb} +24 -10
- data/test/perf_scp.rb +151 -0
- data/test/perf_strict.rb +32 -113
- data/test/sample.rb +2 -3
- data/test/test_compat.rb +474 -0
- data/test/test_custom.rb +355 -0
- data/test/test_debian.rb +53 -0
- data/test/test_fast.rb +66 -16
- data/test/test_file.rb +237 -0
- data/test/test_gc.rb +49 -0
- data/test/test_hash.rb +29 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +1010 -0
- data/test/test_saj.rb +16 -16
- data/test/test_scp.rb +417 -0
- data/test/test_strict.rb +410 -0
- data/test/test_various.rb +815 -0
- data/test/test_writer.rb +308 -0
- data/test/tests.rb +9 -902
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- metadata +253 -38
- data/ext/oj/cache.c +0 -148
- data/ext/oj/foo.rb +0 -6
- data/ext/oj/load.c +0 -1049
- data/test/a.rb +0 -38
- data/test/perf1.rb +0 -64
- data/test/perf2.rb +0 -76
- data/test/perf_obj_old.rb +0 -213
- data/test/test_mimic.rb +0 -208
data/ext/oj/dump_leaf.c
ADDED
@@ -0,0 +1,254 @@
|
|
1
|
+
/* dump_leaf.c
|
2
|
+
* Copyright (c) 2012, 2017, Peter Ohler
|
3
|
+
* All rights reserved.
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include <errno.h>
|
7
|
+
|
8
|
+
#include "oj.h"
|
9
|
+
#include "dump.h"
|
10
|
+
|
11
|
+
static void dump_leaf(Leaf leaf, int depth, Out out);
|
12
|
+
|
13
|
+
extern void dump_cstr(const char *str, size_t cnt, int is_sym, int escape1, Out out);
|
14
|
+
|
15
|
+
static void
|
16
|
+
grow(Out out, size_t len) {
|
17
|
+
size_t size = out->end - out->buf;
|
18
|
+
long pos = out->cur - out->buf;
|
19
|
+
char *buf;
|
20
|
+
|
21
|
+
size *= 2;
|
22
|
+
if (size <= len * 2 + pos) {
|
23
|
+
size += len;
|
24
|
+
}
|
25
|
+
if (out->allocated) {
|
26
|
+
buf = REALLOC_N(out->buf, char, (size + BUFFER_EXTRA));
|
27
|
+
} else {
|
28
|
+
buf = ALLOC_N(char, (size + BUFFER_EXTRA));
|
29
|
+
out->allocated = 1;
|
30
|
+
memcpy(buf, out->buf, out->end - out->buf + BUFFER_EXTRA);
|
31
|
+
}
|
32
|
+
if (0 == buf) {
|
33
|
+
rb_raise(rb_eNoMemError, "Failed to create string. [%d:%s]\n", ENOSPC, strerror(ENOSPC));
|
34
|
+
}
|
35
|
+
out->buf = buf;
|
36
|
+
out->end = buf + size;
|
37
|
+
out->cur = out->buf + pos;
|
38
|
+
}
|
39
|
+
|
40
|
+
|
41
|
+
inline static void
|
42
|
+
dump_chars(const char *s, size_t size, Out out) {
|
43
|
+
if (out->end - out->cur <= (long)size) {
|
44
|
+
grow(out, size);
|
45
|
+
}
|
46
|
+
memcpy(out->cur, s, size);
|
47
|
+
out->cur += size;
|
48
|
+
*out->cur = '\0';
|
49
|
+
}
|
50
|
+
|
51
|
+
static void
|
52
|
+
dump_leaf_str(Leaf leaf, Out out) {
|
53
|
+
switch (leaf->value_type) {
|
54
|
+
case STR_VAL:
|
55
|
+
dump_cstr(leaf->str, strlen(leaf->str), 0, 0, out);
|
56
|
+
break;
|
57
|
+
case RUBY_VAL:
|
58
|
+
dump_cstr(rb_string_value_cstr(&leaf->value), RSTRING_LEN(leaf->value), 0, 0, out);
|
59
|
+
break;
|
60
|
+
case COL_VAL:
|
61
|
+
default:
|
62
|
+
rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
|
63
|
+
break;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
static void
|
68
|
+
dump_leaf_fixnum(Leaf leaf, Out out) {
|
69
|
+
switch (leaf->value_type) {
|
70
|
+
case STR_VAL:
|
71
|
+
dump_chars(leaf->str, strlen(leaf->str), out);
|
72
|
+
break;
|
73
|
+
case RUBY_VAL:
|
74
|
+
if (T_BIGNUM == rb_type(leaf->value)) {
|
75
|
+
oj_dump_bignum(leaf->value, 0, out, false);
|
76
|
+
} else {
|
77
|
+
oj_dump_fixnum(leaf->value, 0, out, false);
|
78
|
+
}
|
79
|
+
break;
|
80
|
+
case COL_VAL:
|
81
|
+
default:
|
82
|
+
rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
|
83
|
+
break;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
static void
|
88
|
+
dump_leaf_float(Leaf leaf, Out out) {
|
89
|
+
switch (leaf->value_type) {
|
90
|
+
case STR_VAL:
|
91
|
+
dump_chars(leaf->str, strlen(leaf->str), out);
|
92
|
+
break;
|
93
|
+
case RUBY_VAL:
|
94
|
+
oj_dump_float(leaf->value, 0, out, false);
|
95
|
+
break;
|
96
|
+
case COL_VAL:
|
97
|
+
default:
|
98
|
+
rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
|
99
|
+
break;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
static void
|
104
|
+
dump_leaf_array(Leaf leaf, int depth, Out out) {
|
105
|
+
size_t size;
|
106
|
+
int d2 = depth + 1;
|
107
|
+
|
108
|
+
size = 2;
|
109
|
+
if (out->end - out->cur <= (long)size) {
|
110
|
+
grow(out, size);
|
111
|
+
}
|
112
|
+
*out->cur++ = '[';
|
113
|
+
if (0 == leaf->elements) {
|
114
|
+
*out->cur++ = ']';
|
115
|
+
} else {
|
116
|
+
Leaf first = leaf->elements->next;
|
117
|
+
Leaf e = first;
|
118
|
+
|
119
|
+
size = d2 * out->indent + 2;
|
120
|
+
do {
|
121
|
+
if (out->end - out->cur <= (long)size) {
|
122
|
+
grow(out, size);
|
123
|
+
}
|
124
|
+
fill_indent(out, d2);
|
125
|
+
dump_leaf(e, d2, out);
|
126
|
+
if (e->next != first) {
|
127
|
+
*out->cur++ = ',';
|
128
|
+
}
|
129
|
+
e = e->next;
|
130
|
+
} while (e != first);
|
131
|
+
size = depth * out->indent + 1;
|
132
|
+
if (out->end - out->cur <= (long)size) {
|
133
|
+
grow(out, size);
|
134
|
+
}
|
135
|
+
fill_indent(out, depth);
|
136
|
+
*out->cur++ = ']';
|
137
|
+
}
|
138
|
+
*out->cur = '\0';
|
139
|
+
}
|
140
|
+
|
141
|
+
static void
|
142
|
+
dump_leaf_hash(Leaf leaf, int depth, Out out) {
|
143
|
+
size_t size;
|
144
|
+
int d2 = depth + 1;
|
145
|
+
|
146
|
+
size = 2;
|
147
|
+
if (out->end - out->cur <= (long)size) {
|
148
|
+
grow(out, size);
|
149
|
+
}
|
150
|
+
*out->cur++ = '{';
|
151
|
+
if (0 == leaf->elements) {
|
152
|
+
*out->cur++ = '}';
|
153
|
+
} else {
|
154
|
+
Leaf first = leaf->elements->next;
|
155
|
+
Leaf e = first;
|
156
|
+
|
157
|
+
size = d2 * out->indent + 2;
|
158
|
+
do {
|
159
|
+
if (out->end - out->cur <= (long)size) {
|
160
|
+
grow(out, size);
|
161
|
+
}
|
162
|
+
fill_indent(out, d2);
|
163
|
+
dump_cstr(e->key, strlen(e->key), 0, 0, out);
|
164
|
+
*out->cur++ = ':';
|
165
|
+
dump_leaf(e, d2, out);
|
166
|
+
if (e->next != first) {
|
167
|
+
*out->cur++ = ',';
|
168
|
+
}
|
169
|
+
e = e->next;
|
170
|
+
} while (e != first);
|
171
|
+
size = depth * out->indent + 1;
|
172
|
+
if (out->end - out->cur <= (long)size) {
|
173
|
+
grow(out, size);
|
174
|
+
}
|
175
|
+
fill_indent(out, depth);
|
176
|
+
*out->cur++ = '}';
|
177
|
+
}
|
178
|
+
*out->cur = '\0';
|
179
|
+
}
|
180
|
+
|
181
|
+
static void
|
182
|
+
dump_leaf(Leaf leaf, int depth, Out out) {
|
183
|
+
switch (leaf->rtype) {
|
184
|
+
case T_NIL:
|
185
|
+
oj_dump_nil(Qnil, 0, out, false);
|
186
|
+
break;
|
187
|
+
case T_TRUE:
|
188
|
+
oj_dump_true(Qtrue, 0, out, false);
|
189
|
+
break;
|
190
|
+
case T_FALSE:
|
191
|
+
oj_dump_false(Qfalse, 0, out, false);
|
192
|
+
break;
|
193
|
+
case T_STRING:
|
194
|
+
dump_leaf_str(leaf, out);
|
195
|
+
break;
|
196
|
+
case T_FIXNUM:
|
197
|
+
dump_leaf_fixnum(leaf, out);
|
198
|
+
break;
|
199
|
+
case T_FLOAT:
|
200
|
+
dump_leaf_float(leaf, out);
|
201
|
+
break;
|
202
|
+
case T_ARRAY:
|
203
|
+
dump_leaf_array(leaf, depth, out);
|
204
|
+
break;
|
205
|
+
case T_HASH:
|
206
|
+
dump_leaf_hash(leaf, depth, out);
|
207
|
+
break;
|
208
|
+
default:
|
209
|
+
rb_raise(rb_eTypeError, "Unexpected type %02x.\n", leaf->rtype);
|
210
|
+
break;
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
void
|
215
|
+
oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
|
216
|
+
if (0 == out->buf) {
|
217
|
+
out->buf = ALLOC_N(char, 4096);
|
218
|
+
out->end = out->buf + 4095 - BUFFER_EXTRA; // 1 less than end plus extra for possible errors
|
219
|
+
out->allocated = 1;
|
220
|
+
}
|
221
|
+
out->cur = out->buf;
|
222
|
+
out->circ_cnt = 0;
|
223
|
+
out->opts = copts;
|
224
|
+
out->hash_cnt = 0;
|
225
|
+
out->indent = copts->indent;
|
226
|
+
dump_leaf(leaf, 0, out);
|
227
|
+
}
|
228
|
+
|
229
|
+
void
|
230
|
+
oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
|
231
|
+
char buf[4096];
|
232
|
+
struct _Out out;
|
233
|
+
size_t size;
|
234
|
+
FILE *f;
|
235
|
+
|
236
|
+
out.buf = buf;
|
237
|
+
out.end = buf + sizeof(buf) - BUFFER_EXTRA;
|
238
|
+
out.allocated = 0;
|
239
|
+
out.omit_nil = copts->dump_opts.omit_nil;
|
240
|
+
oj_dump_leaf_to_json(leaf, copts, &out);
|
241
|
+
size = out.cur - out.buf;
|
242
|
+
if (0 == (f = fopen(path, "w"))) {
|
243
|
+
rb_raise(rb_eIOError, "%s\n", strerror(errno));
|
244
|
+
}
|
245
|
+
if (size != fwrite(out.buf, 1, size, f)) {
|
246
|
+
int err = ferror(f);
|
247
|
+
|
248
|
+
rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
|
249
|
+
}
|
250
|
+
if (out.allocated) {
|
251
|
+
xfree(out.buf);
|
252
|
+
}
|
253
|
+
fclose(f);
|
254
|
+
}
|