oj 3.7.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +96 -0
- data/ext/oj/buf.h +103 -0
- data/ext/oj/cache8.c +107 -0
- data/ext/oj/cache8.h +48 -0
- data/ext/oj/circarray.c +68 -0
- data/ext/oj/circarray.h +23 -0
- data/ext/oj/code.c +235 -0
- data/ext/oj/code.h +42 -0
- data/ext/oj/compat.c +299 -0
- data/ext/oj/custom.c +1188 -0
- data/ext/oj/dump.c +1232 -0
- data/ext/oj/dump.h +94 -0
- data/ext/oj/dump_compat.c +973 -0
- data/ext/oj/dump_leaf.c +252 -0
- data/ext/oj/dump_object.c +837 -0
- data/ext/oj/dump_strict.c +433 -0
- data/ext/oj/encode.h +45 -0
- data/ext/oj/err.c +57 -0
- data/ext/oj/err.h +70 -0
- data/ext/oj/extconf.rb +47 -0
- data/ext/oj/fast.c +1771 -0
- 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 +873 -0
- data/ext/oj/object.c +771 -0
- data/ext/oj/odd.c +231 -0
- data/ext/oj/odd.h +44 -0
- data/ext/oj/oj.c +1694 -0
- data/ext/oj/oj.h +381 -0
- data/ext/oj/parse.c +1085 -0
- data/ext/oj/parse.h +111 -0
- data/ext/oj/rails.c +1485 -0
- data/ext/oj/rails.h +21 -0
- data/ext/oj/reader.c +231 -0
- data/ext/oj/reader.h +151 -0
- data/ext/oj/resolve.c +102 -0
- data/ext/oj/resolve.h +14 -0
- data/ext/oj/rxclass.c +147 -0
- data/ext/oj/rxclass.h +27 -0
- data/ext/oj/saj.c +714 -0
- data/ext/oj/scp.c +224 -0
- data/ext/oj/sparse.c +910 -0
- data/ext/oj/stream_writer.c +363 -0
- data/ext/oj/strict.c +212 -0
- data/ext/oj/string_writer.c +512 -0
- data/ext/oj/trace.c +79 -0
- data/ext/oj/trace.h +28 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +19 -0
- data/ext/oj/val_stack.c +118 -0
- data/ext/oj/val_stack.h +185 -0
- data/ext/oj/wab.c +631 -0
- data/lib/oj.rb +21 -0
- data/lib/oj/active_support_helper.rb +41 -0
- data/lib/oj/bag.rb +88 -0
- data/lib/oj/easy_hash.rb +52 -0
- data/lib/oj/error.rb +22 -0
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +267 -0
- data/lib/oj/saj.rb +66 -0
- data/lib/oj/schandler.rb +142 -0
- data/lib/oj/state.rb +131 -0
- data/lib/oj/version.rb +5 -0
- 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 +154 -0
- data/pages/Options.md +266 -0
- data/pages/Rails.md +116 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -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/activerecord/result_test.rb +27 -0
- data/test/activesupport4/decoding_test.rb +108 -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 +485 -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/big.rb +15 -0
- data/test/files.rb +29 -0
- data/test/foo.rb +33 -0
- data/test/helper.rb +26 -0
- data/test/isolated/shared.rb +308 -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 +148 -0
- data/test/json_gem/json_encoding_test.rb +107 -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/mem.rb +35 -0
- data/test/perf.rb +107 -0
- data/test/perf_compat.rb +130 -0
- data/test/perf_fast.rb +164 -0
- data/test/perf_file.rb +64 -0
- data/test/perf_object.rb +138 -0
- data/test/perf_saj.rb +109 -0
- data/test/perf_scp.rb +151 -0
- data/test/perf_simple.rb +287 -0
- data/test/perf_strict.rb +145 -0
- data/test/perf_wab.rb +131 -0
- data/test/sample.rb +54 -0
- data/test/sample/change.rb +14 -0
- data/test/sample/dir.rb +19 -0
- data/test/sample/doc.rb +36 -0
- data/test/sample/file.rb +48 -0
- data/test/sample/group.rb +16 -0
- data/test/sample/hasprops.rb +16 -0
- data/test/sample/layer.rb +12 -0
- data/test/sample/line.rb +20 -0
- data/test/sample/oval.rb +10 -0
- data/test/sample/rect.rb +10 -0
- data/test/sample/shape.rb +35 -0
- data/test/sample/text.rb +20 -0
- data/test/sample_json.rb +37 -0
- data/test/test_compat.rb +509 -0
- data/test/test_custom.rb +406 -0
- data/test/test_debian.rb +53 -0
- data/test/test_fast.rb +470 -0
- data/test/test_file.rb +239 -0
- data/test/test_gc.rb +49 -0
- data/test/test_hash.rb +29 -0
- data/test/test_integer_range.rb +73 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +1018 -0
- data/test/test_saj.rb +186 -0
- data/test/test_scp.rb +433 -0
- data/test/test_strict.rb +410 -0
- data/test/test_various.rb +739 -0
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +380 -0
- data/test/tests.rb +24 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- metadata +359 -0
@@ -0,0 +1,433 @@
|
|
1
|
+
/* dump_strict.c
|
2
|
+
* Copyright (c) 2012, 2017, Peter Ohler
|
3
|
+
* All rights reserved.
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include <stdlib.h>
|
7
|
+
#include <time.h>
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <string.h>
|
10
|
+
#include <math.h>
|
11
|
+
#include <unistd.h>
|
12
|
+
#include <errno.h>
|
13
|
+
|
14
|
+
#include "dump.h"
|
15
|
+
#include "trace.h"
|
16
|
+
|
17
|
+
// Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
|
18
|
+
#define OJ_INFINITY (1.0/0.0)
|
19
|
+
|
20
|
+
typedef unsigned long ulong;
|
21
|
+
|
22
|
+
static const char inf_val[] = INF_VAL;
|
23
|
+
static const char ninf_val[] = NINF_VAL;
|
24
|
+
static const char nan_val[] = NAN_VAL;
|
25
|
+
|
26
|
+
static void
|
27
|
+
raise_strict(VALUE obj) {
|
28
|
+
rb_raise(rb_eTypeError, "Failed to dump %s Object to JSON in strict mode.\n", rb_class2name(rb_obj_class(obj)));
|
29
|
+
}
|
30
|
+
|
31
|
+
// Removed dependencies on math due to problems with CentOS 5.4.
|
32
|
+
static void
|
33
|
+
dump_float(VALUE obj, int depth, Out out, bool as_ok) {
|
34
|
+
char buf[64];
|
35
|
+
char *b;
|
36
|
+
double d = rb_num2dbl(obj);
|
37
|
+
int cnt = 0;
|
38
|
+
|
39
|
+
if (0.0 == d) {
|
40
|
+
b = buf;
|
41
|
+
*b++ = '0';
|
42
|
+
*b++ = '.';
|
43
|
+
*b++ = '0';
|
44
|
+
*b++ = '\0';
|
45
|
+
cnt = 3;
|
46
|
+
} else {
|
47
|
+
NanDump nd = out->opts->dump_opts.nan_dump;
|
48
|
+
|
49
|
+
if (AutoNan == nd) {
|
50
|
+
nd = RaiseNan;
|
51
|
+
}
|
52
|
+
if (OJ_INFINITY == d) {
|
53
|
+
switch (nd) {
|
54
|
+
case RaiseNan:
|
55
|
+
case WordNan:
|
56
|
+
raise_strict(obj);
|
57
|
+
break;
|
58
|
+
case NullNan:
|
59
|
+
strcpy(buf, "null");
|
60
|
+
cnt = 4;
|
61
|
+
break;
|
62
|
+
case HugeNan:
|
63
|
+
default:
|
64
|
+
strcpy(buf, inf_val);
|
65
|
+
cnt = sizeof(inf_val) - 1;
|
66
|
+
break;
|
67
|
+
}
|
68
|
+
} else if (-OJ_INFINITY == d) {
|
69
|
+
switch (nd) {
|
70
|
+
case RaiseNan:
|
71
|
+
case WordNan:
|
72
|
+
raise_strict(obj);
|
73
|
+
break;
|
74
|
+
case NullNan:
|
75
|
+
strcpy(buf, "null");
|
76
|
+
cnt = 4;
|
77
|
+
break;
|
78
|
+
case HugeNan:
|
79
|
+
default:
|
80
|
+
strcpy(buf, ninf_val);
|
81
|
+
cnt = sizeof(ninf_val) - 1;
|
82
|
+
break;
|
83
|
+
}
|
84
|
+
} else if (isnan(d)) {
|
85
|
+
switch (nd) {
|
86
|
+
case RaiseNan:
|
87
|
+
case WordNan:
|
88
|
+
raise_strict(obj);
|
89
|
+
break;
|
90
|
+
case NullNan:
|
91
|
+
strcpy(buf, "null");
|
92
|
+
cnt = 4;
|
93
|
+
break;
|
94
|
+
case HugeNan:
|
95
|
+
default:
|
96
|
+
strcpy(buf, nan_val);
|
97
|
+
cnt = sizeof(nan_val) - 1;
|
98
|
+
break;
|
99
|
+
}
|
100
|
+
} else if (d == (double)(long long int)d) {
|
101
|
+
cnt = snprintf(buf, sizeof(buf), "%.1f", d);
|
102
|
+
} else if (0 == out->opts->float_prec) {
|
103
|
+
volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
|
104
|
+
|
105
|
+
cnt = (int)RSTRING_LEN(rstr);
|
106
|
+
if ((int)sizeof(buf) <= cnt) {
|
107
|
+
cnt = sizeof(buf) - 1;
|
108
|
+
}
|
109
|
+
strncpy(buf, rb_string_value_ptr((VALUE*)&rstr), cnt);
|
110
|
+
buf[cnt] = '\0';
|
111
|
+
} else {
|
112
|
+
cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, out->opts->float_fmt);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
assure_size(out, cnt);
|
116
|
+
for (b = buf; '\0' != *b; b++) {
|
117
|
+
*out->cur++ = *b;
|
118
|
+
}
|
119
|
+
*out->cur = '\0';
|
120
|
+
}
|
121
|
+
|
122
|
+
static void
|
123
|
+
dump_array(VALUE a, int depth, Out out, bool as_ok) {
|
124
|
+
size_t size;
|
125
|
+
int i, cnt;
|
126
|
+
int d2 = depth + 1;
|
127
|
+
|
128
|
+
if (Yes == out->opts->circular) {
|
129
|
+
if (0 > oj_check_circular(a, out)) {
|
130
|
+
oj_dump_nil(Qnil, 0, out, false);
|
131
|
+
return;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
cnt = (int)RARRAY_LEN(a);
|
135
|
+
*out->cur++ = '[';
|
136
|
+
size = 2;
|
137
|
+
assure_size(out, size);
|
138
|
+
if (0 == cnt) {
|
139
|
+
*out->cur++ = ']';
|
140
|
+
} else {
|
141
|
+
if (out->opts->dump_opts.use) {
|
142
|
+
size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
|
143
|
+
} else {
|
144
|
+
size = d2 * out->indent + 2;
|
145
|
+
}
|
146
|
+
cnt--;
|
147
|
+
for (i = 0; i <= cnt; i++) {
|
148
|
+
assure_size(out, size);
|
149
|
+
if (out->opts->dump_opts.use) {
|
150
|
+
if (0 < out->opts->dump_opts.array_size) {
|
151
|
+
strcpy(out->cur, out->opts->dump_opts.array_nl);
|
152
|
+
out->cur += out->opts->dump_opts.array_size;
|
153
|
+
}
|
154
|
+
if (0 < out->opts->dump_opts.indent_size) {
|
155
|
+
int i;
|
156
|
+
for (i = d2; 0 < i; i--) {
|
157
|
+
strcpy(out->cur, out->opts->dump_opts.indent_str);
|
158
|
+
out->cur += out->opts->dump_opts.indent_size;
|
159
|
+
}
|
160
|
+
}
|
161
|
+
} else {
|
162
|
+
fill_indent(out, d2);
|
163
|
+
}
|
164
|
+
if (NullMode == out->opts->mode) {
|
165
|
+
oj_dump_null_val(rb_ary_entry(a, i), d2, out);
|
166
|
+
} else {
|
167
|
+
oj_dump_strict_val(rb_ary_entry(a, i), d2, out);
|
168
|
+
}
|
169
|
+
if (i < cnt) {
|
170
|
+
*out->cur++ = ',';
|
171
|
+
}
|
172
|
+
}
|
173
|
+
size = depth * out->indent + 1;
|
174
|
+
assure_size(out, size);
|
175
|
+
if (out->opts->dump_opts.use) {
|
176
|
+
//printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size, out->opts->dump_opts->indent);
|
177
|
+
if (0 < out->opts->dump_opts.array_size) {
|
178
|
+
strcpy(out->cur, out->opts->dump_opts.array_nl);
|
179
|
+
out->cur += out->opts->dump_opts.array_size;
|
180
|
+
}
|
181
|
+
if (0 < out->opts->dump_opts.indent_size) {
|
182
|
+
int i;
|
183
|
+
|
184
|
+
for (i = depth; 0 < i; i--) {
|
185
|
+
strcpy(out->cur, out->opts->dump_opts.indent_str);
|
186
|
+
out->cur += out->opts->dump_opts.indent_size;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
} else {
|
190
|
+
fill_indent(out, depth);
|
191
|
+
}
|
192
|
+
*out->cur++ = ']';
|
193
|
+
}
|
194
|
+
*out->cur = '\0';
|
195
|
+
}
|
196
|
+
|
197
|
+
static int
|
198
|
+
hash_cb(VALUE key, VALUE value, Out out) {
|
199
|
+
int depth = out->depth;
|
200
|
+
long size;
|
201
|
+
int rtype = rb_type(key);
|
202
|
+
|
203
|
+
if (rtype != T_STRING && rtype != T_SYMBOL) {
|
204
|
+
rb_raise(rb_eTypeError, "In :strict and :null mode all Hash keys must be Strings or Symbols, not %s.\n", rb_class2name(rb_obj_class(key)));
|
205
|
+
}
|
206
|
+
if (out->omit_nil && Qnil == value) {
|
207
|
+
return ST_CONTINUE;
|
208
|
+
}
|
209
|
+
if (!out->opts->dump_opts.use) {
|
210
|
+
size = depth * out->indent + 1;
|
211
|
+
assure_size(out, size);
|
212
|
+
fill_indent(out, depth);
|
213
|
+
if (rtype == T_STRING) {
|
214
|
+
oj_dump_str(key, 0, out, false);
|
215
|
+
} else {
|
216
|
+
oj_dump_sym(key, 0, out, false);
|
217
|
+
}
|
218
|
+
*out->cur++ = ':';
|
219
|
+
} else {
|
220
|
+
size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
|
221
|
+
assure_size(out, size);
|
222
|
+
if (0 < out->opts->dump_opts.hash_size) {
|
223
|
+
strcpy(out->cur, out->opts->dump_opts.hash_nl);
|
224
|
+
out->cur += out->opts->dump_opts.hash_size;
|
225
|
+
}
|
226
|
+
if (0 < out->opts->dump_opts.indent_size) {
|
227
|
+
int i;
|
228
|
+
for (i = depth; 0 < i; i--) {
|
229
|
+
strcpy(out->cur, out->opts->dump_opts.indent_str);
|
230
|
+
out->cur += out->opts->dump_opts.indent_size;
|
231
|
+
}
|
232
|
+
}
|
233
|
+
if (rtype == T_STRING) {
|
234
|
+
oj_dump_str(key, 0, out, false);
|
235
|
+
} else {
|
236
|
+
oj_dump_sym(key, 0, out, false);
|
237
|
+
}
|
238
|
+
size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
|
239
|
+
assure_size(out, size);
|
240
|
+
if (0 < out->opts->dump_opts.before_size) {
|
241
|
+
strcpy(out->cur, out->opts->dump_opts.before_sep);
|
242
|
+
out->cur += out->opts->dump_opts.before_size;
|
243
|
+
}
|
244
|
+
*out->cur++ = ':';
|
245
|
+
if (0 < out->opts->dump_opts.after_size) {
|
246
|
+
strcpy(out->cur, out->opts->dump_opts.after_sep);
|
247
|
+
out->cur += out->opts->dump_opts.after_size;
|
248
|
+
}
|
249
|
+
}
|
250
|
+
if (NullMode == out->opts->mode) {
|
251
|
+
oj_dump_null_val(value, depth, out);
|
252
|
+
} else {
|
253
|
+
oj_dump_strict_val(value, depth, out);
|
254
|
+
}
|
255
|
+
out->depth = depth;
|
256
|
+
*out->cur++ = ',';
|
257
|
+
|
258
|
+
return ST_CONTINUE;
|
259
|
+
}
|
260
|
+
|
261
|
+
static void
|
262
|
+
dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
|
263
|
+
int cnt;
|
264
|
+
size_t size;
|
265
|
+
|
266
|
+
if (Yes == out->opts->circular) {
|
267
|
+
if (0 > oj_check_circular(obj, out)) {
|
268
|
+
oj_dump_nil(Qnil, 0, out, false);
|
269
|
+
return;
|
270
|
+
}
|
271
|
+
}
|
272
|
+
cnt = (int)RHASH_SIZE(obj);
|
273
|
+
size = depth * out->indent + 2;
|
274
|
+
assure_size(out, 2);
|
275
|
+
*out->cur++ = '{';
|
276
|
+
if (0 == cnt) {
|
277
|
+
*out->cur++ = '}';
|
278
|
+
} else {
|
279
|
+
out->depth = depth + 1;
|
280
|
+
rb_hash_foreach(obj, hash_cb, (VALUE)out);
|
281
|
+
if (',' == *(out->cur - 1)) {
|
282
|
+
out->cur--; // backup to overwrite last comma
|
283
|
+
}
|
284
|
+
if (!out->opts->dump_opts.use) {
|
285
|
+
assure_size(out, size);
|
286
|
+
fill_indent(out, depth);
|
287
|
+
} else {
|
288
|
+
size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
|
289
|
+
assure_size(out, size);
|
290
|
+
if (0 < out->opts->dump_opts.hash_size) {
|
291
|
+
strcpy(out->cur, out->opts->dump_opts.hash_nl);
|
292
|
+
out->cur += out->opts->dump_opts.hash_size;
|
293
|
+
}
|
294
|
+
if (0 < out->opts->dump_opts.indent_size) {
|
295
|
+
int i;
|
296
|
+
|
297
|
+
for (i = depth; 0 < i; i--) {
|
298
|
+
strcpy(out->cur, out->opts->dump_opts.indent_str);
|
299
|
+
out->cur += out->opts->dump_opts.indent_size;
|
300
|
+
}
|
301
|
+
}
|
302
|
+
}
|
303
|
+
*out->cur++ = '}';
|
304
|
+
}
|
305
|
+
*out->cur = '\0';
|
306
|
+
}
|
307
|
+
|
308
|
+
static void
|
309
|
+
dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
|
310
|
+
VALUE clas = rb_obj_class(obj);
|
311
|
+
|
312
|
+
if (oj_bigdecimal_class == clas) {
|
313
|
+
volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
|
314
|
+
|
315
|
+
oj_dump_raw(rb_string_value_ptr((VALUE*)&rstr), (int)RSTRING_LEN(rstr), out);
|
316
|
+
} else {
|
317
|
+
raise_strict(obj);
|
318
|
+
}
|
319
|
+
}
|
320
|
+
|
321
|
+
static void
|
322
|
+
dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
|
323
|
+
VALUE clas = rb_obj_class(obj);
|
324
|
+
|
325
|
+
if (oj_bigdecimal_class == clas) {
|
326
|
+
volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
|
327
|
+
|
328
|
+
oj_dump_raw(rb_string_value_ptr((VALUE*)&rstr), (int)RSTRING_LEN(rstr), out);
|
329
|
+
} else {
|
330
|
+
oj_dump_nil(Qnil, depth, out, false);
|
331
|
+
}
|
332
|
+
}
|
333
|
+
|
334
|
+
static DumpFunc strict_funcs[] = {
|
335
|
+
NULL, // RUBY_T_NONE = 0x00,
|
336
|
+
dump_data_strict, // RUBY_T_OBJECT = 0x01,
|
337
|
+
NULL, // RUBY_T_CLASS = 0x02,
|
338
|
+
NULL, // RUBY_T_MODULE = 0x03,
|
339
|
+
dump_float, // RUBY_T_FLOAT = 0x04,
|
340
|
+
oj_dump_str, // RUBY_T_STRING = 0x05,
|
341
|
+
NULL, // RUBY_T_REGEXP = 0x06,
|
342
|
+
dump_array, // RUBY_T_ARRAY = 0x07,
|
343
|
+
dump_hash, // RUBY_T_HASH = 0x08,
|
344
|
+
NULL, // RUBY_T_STRUCT = 0x09,
|
345
|
+
oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
|
346
|
+
NULL, // RUBY_T_FILE = 0x0b,
|
347
|
+
dump_data_strict, // RUBY_T_DATA = 0x0c,
|
348
|
+
NULL, // RUBY_T_MATCH = 0x0d,
|
349
|
+
NULL, // RUBY_T_COMPLEX = 0x0e,
|
350
|
+
NULL, // RUBY_T_RATIONAL = 0x0f,
|
351
|
+
NULL, // 0x10
|
352
|
+
oj_dump_nil, // RUBY_T_NIL = 0x11,
|
353
|
+
oj_dump_true, // RUBY_T_TRUE = 0x12,
|
354
|
+
oj_dump_false, // RUBY_T_FALSE = 0x13,
|
355
|
+
oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
|
356
|
+
oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
|
357
|
+
};
|
358
|
+
|
359
|
+
void
|
360
|
+
oj_dump_strict_val(VALUE obj, int depth, Out out) {
|
361
|
+
int type = rb_type(obj);
|
362
|
+
|
363
|
+
if (Yes == out->opts->trace) {
|
364
|
+
oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceIn);
|
365
|
+
}
|
366
|
+
if (MAX_DEPTH < depth) {
|
367
|
+
rb_raise(rb_eNoMemError, "Too deeply nested.\n");
|
368
|
+
}
|
369
|
+
if (0 < type && type <= RUBY_T_FIXNUM) {
|
370
|
+
DumpFunc f = strict_funcs[type];
|
371
|
+
|
372
|
+
if (NULL != f) {
|
373
|
+
f(obj, depth, out, false);
|
374
|
+
if (Yes == out->opts->trace) {
|
375
|
+
oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
|
376
|
+
}
|
377
|
+
return;
|
378
|
+
}
|
379
|
+
}
|
380
|
+
raise_strict(obj);
|
381
|
+
}
|
382
|
+
|
383
|
+
static DumpFunc null_funcs[] = {
|
384
|
+
NULL, // RUBY_T_NONE = 0x00,
|
385
|
+
dump_data_null, // RUBY_T_OBJECT = 0x01,
|
386
|
+
NULL, // RUBY_T_CLASS = 0x02,
|
387
|
+
NULL, // RUBY_T_MODULE = 0x03,
|
388
|
+
dump_float, // RUBY_T_FLOAT = 0x04,
|
389
|
+
oj_dump_str, // RUBY_T_STRING = 0x05,
|
390
|
+
NULL, // RUBY_T_REGEXP = 0x06,
|
391
|
+
dump_array, // RUBY_T_ARRAY = 0x07,
|
392
|
+
dump_hash, // RUBY_T_HASH = 0x08,
|
393
|
+
NULL, // RUBY_T_STRUCT = 0x09,
|
394
|
+
oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
|
395
|
+
NULL, // RUBY_T_FILE = 0x0b,
|
396
|
+
dump_data_null, // RUBY_T_DATA = 0x0c,
|
397
|
+
NULL, // RUBY_T_MATCH = 0x0d,
|
398
|
+
NULL, // RUBY_T_COMPLEX = 0x0e,
|
399
|
+
NULL, // RUBY_T_RATIONAL = 0x0f,
|
400
|
+
NULL, // 0x10
|
401
|
+
oj_dump_nil, // RUBY_T_NIL = 0x11,
|
402
|
+
oj_dump_true, // RUBY_T_TRUE = 0x12,
|
403
|
+
oj_dump_false, // RUBY_T_FALSE = 0x13,
|
404
|
+
oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
|
405
|
+
oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
|
406
|
+
};
|
407
|
+
|
408
|
+
void
|
409
|
+
oj_dump_null_val(VALUE obj, int depth, Out out) {
|
410
|
+
int type = rb_type(obj);
|
411
|
+
|
412
|
+
if (Yes == out->opts->trace) {
|
413
|
+
oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
|
414
|
+
}
|
415
|
+
if (MAX_DEPTH < depth) {
|
416
|
+
rb_raise(rb_eNoMemError, "Too deeply nested.\n");
|
417
|
+
}
|
418
|
+
if (0 < type && type <= RUBY_T_FIXNUM) {
|
419
|
+
DumpFunc f = null_funcs[type];
|
420
|
+
|
421
|
+
if (NULL != f) {
|
422
|
+
f(obj, depth, out, false);
|
423
|
+
if (Yes == out->opts->trace) {
|
424
|
+
oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
|
425
|
+
}
|
426
|
+
return;
|
427
|
+
}
|
428
|
+
}
|
429
|
+
oj_dump_nil(Qnil, depth, out, false);
|
430
|
+
if (Yes == out->opts->trace) {
|
431
|
+
oj_trace("dump", Qnil, __FILE__, __LINE__, depth, TraceOut);
|
432
|
+
}
|
433
|
+
}
|
data/ext/oj/encode.h
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
/* encode.h
|
2
|
+
* Copyright (c) 2011, Peter Ohler
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
7
|
+
*
|
8
|
+
* - Redistributions of source code must retain the above copyright notice, this
|
9
|
+
* list of conditions and the following disclaimer.
|
10
|
+
*
|
11
|
+
* - Redistributions in binary form must reproduce the above copyright notice,
|
12
|
+
* this list of conditions and the following disclaimer in the documentation
|
13
|
+
* and/or other materials provided with the distribution.
|
14
|
+
*
|
15
|
+
* - Neither the name of Peter Ohler nor the names of its contributors may be
|
16
|
+
* used to endorse or promote products derived from this software without
|
17
|
+
* specific prior written permission.
|
18
|
+
*
|
19
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
*/
|
30
|
+
|
31
|
+
#ifndef OJ_ENCODE_H
|
32
|
+
#define OJ_ENCODE_H
|
33
|
+
|
34
|
+
#include "ruby.h"
|
35
|
+
#include "ruby/encoding.h"
|
36
|
+
|
37
|
+
#include "oj.h"
|
38
|
+
|
39
|
+
static inline VALUE
|
40
|
+
oj_encode(VALUE rstr) {
|
41
|
+
rb_enc_associate(rstr, oj_utf8_encoding);
|
42
|
+
return rstr;
|
43
|
+
}
|
44
|
+
|
45
|
+
#endif /* OJ_ENCODE_H */
|