oj 3.10.6 → 3.12.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/ext/oj/buf.h +36 -68
- data/ext/oj/cache8.c +59 -62
- data/ext/oj/cache8.h +9 -36
- data/ext/oj/circarray.c +36 -42
- data/ext/oj/circarray.h +12 -13
- data/ext/oj/code.c +172 -179
- data/ext/oj/code.h +22 -24
- data/ext/oj/compat.c +176 -181
- data/ext/oj/custom.c +800 -864
- data/ext/oj/dump.c +774 -776
- data/ext/oj/dump.h +50 -55
- data/ext/oj/dump_compat.c +2 -4
- data/ext/oj/dump_leaf.c +118 -162
- data/ext/oj/dump_object.c +610 -632
- data/ext/oj/dump_strict.c +319 -331
- data/ext/oj/encode.h +4 -33
- data/ext/oj/err.c +40 -29
- data/ext/oj/err.h +25 -44
- data/ext/oj/extconf.rb +2 -1
- data/ext/oj/fast.c +1054 -1081
- data/ext/oj/hash.c +102 -97
- data/ext/oj/hash.h +10 -35
- data/ext/oj/hash_test.c +451 -472
- data/ext/oj/mimic_json.c +415 -402
- data/ext/oj/object.c +588 -532
- data/ext/oj/odd.c +124 -132
- data/ext/oj/odd.h +28 -29
- data/ext/oj/oj.c +1186 -906
- data/ext/oj/oj.h +289 -298
- data/ext/oj/parse.c +946 -870
- data/ext/oj/parse.h +81 -79
- data/ext/oj/rails.c +837 -842
- data/ext/oj/rails.h +8 -11
- data/ext/oj/reader.c +139 -147
- data/ext/oj/reader.h +68 -84
- data/ext/oj/resolve.c +44 -47
- data/ext/oj/resolve.h +4 -6
- data/ext/oj/rxclass.c +69 -73
- data/ext/oj/rxclass.h +13 -14
- data/ext/oj/saj.c +453 -484
- data/ext/oj/scp.c +88 -113
- data/ext/oj/sparse.c +783 -714
- data/ext/oj/stream_writer.c +123 -157
- data/ext/oj/strict.c +133 -106
- data/ext/oj/string_writer.c +199 -247
- data/ext/oj/trace.c +34 -41
- data/ext/oj/trace.h +15 -15
- data/ext/oj/util.c +104 -104
- data/ext/oj/util.h +4 -3
- data/ext/oj/val_stack.c +48 -76
- data/ext/oj/val_stack.h +80 -115
- data/ext/oj/wab.c +321 -325
- data/lib/oj.rb +0 -8
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +5 -4
- data/lib/oj/mimic.rb +47 -13
- data/lib/oj/version.rb +1 -1
- data/pages/Modes.md +1 -0
- data/pages/Options.md +23 -11
- data/test/activerecord/result_test.rb +7 -2
- data/test/foo.rb +8 -40
- data/test/helper.rb +10 -0
- data/test/json_gem/json_common_interface_test.rb +8 -3
- data/test/json_gem/json_generator_test.rb +15 -3
- data/test/json_gem/test_helper.rb +8 -0
- data/test/perf.rb +1 -1
- 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 +16 -3
- data/test/test_custom.rb +11 -0
- data/test/test_fast.rb +32 -2
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +10 -0
- data/test/test_rails.rb +9 -0
- data/test/test_scp.rb +1 -1
- data/test/test_various.rb +4 -2
- metadata +89 -85
data/ext/oj/dump_strict.c
CHANGED
@@ -1,434 +1,422 @@
|
|
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
|
-
#include <
|
7
|
-
#include <
|
4
|
+
#include <errno.h>
|
5
|
+
#include <math.h>
|
8
6
|
#include <stdio.h>
|
7
|
+
#include <stdlib.h>
|
9
8
|
#include <string.h>
|
10
|
-
#include <
|
9
|
+
#include <time.h>
|
11
10
|
#include <unistd.h>
|
12
|
-
#include <errno.h>
|
13
11
|
|
14
12
|
#include "dump.h"
|
15
13
|
#include "trace.h"
|
16
14
|
|
17
15
|
// Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
|
18
|
-
#define OJ_INFINITY (1.0/0.0)
|
16
|
+
#define OJ_INFINITY (1.0 / 0.0)
|
19
17
|
|
20
|
-
typedef unsigned long
|
18
|
+
typedef unsigned long ulong;
|
21
19
|
|
22
|
-
static const char
|
23
|
-
static const char
|
24
|
-
static const char
|
20
|
+
static const char inf_val[] = INF_VAL;
|
21
|
+
static const char ninf_val[] = NINF_VAL;
|
22
|
+
static const char nan_val[] = NAN_VAL;
|
25
23
|
|
26
|
-
static void
|
27
|
-
|
28
|
-
|
24
|
+
static void raise_strict(VALUE obj) {
|
25
|
+
rb_raise(rb_eTypeError,
|
26
|
+
"Failed to dump %s Object to JSON in strict mode.\n",
|
27
|
+
rb_class2name(rb_obj_class(obj)));
|
29
28
|
}
|
30
29
|
|
31
30
|
// Removed dependencies on math due to problems with CentOS 5.4.
|
32
|
-
static void
|
33
|
-
|
34
|
-
char
|
35
|
-
|
36
|
-
|
37
|
-
int cnt = 0;
|
31
|
+
static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
|
32
|
+
char buf[64];
|
33
|
+
char * b;
|
34
|
+
double d = rb_num2dbl(obj);
|
35
|
+
int cnt = 0;
|
38
36
|
|
39
37
|
if (0.0 == d) {
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
b = buf;
|
39
|
+
*b++ = '0';
|
40
|
+
*b++ = '.';
|
41
|
+
*b++ = '0';
|
42
|
+
*b++ = '\0';
|
43
|
+
cnt = 3;
|
46
44
|
} else {
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
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
|
-
}
|
45
|
+
NanDump nd = out->opts->dump_opts.nan_dump;
|
46
|
+
|
47
|
+
if (AutoNan == nd) {
|
48
|
+
nd = RaiseNan;
|
49
|
+
}
|
50
|
+
if (OJ_INFINITY == d) {
|
51
|
+
switch (nd) {
|
52
|
+
case RaiseNan:
|
53
|
+
case WordNan: raise_strict(obj); break;
|
54
|
+
case NullNan:
|
55
|
+
strcpy(buf, "null");
|
56
|
+
cnt = 4;
|
57
|
+
break;
|
58
|
+
case HugeNan:
|
59
|
+
default:
|
60
|
+
strcpy(buf, inf_val);
|
61
|
+
cnt = sizeof(inf_val) - 1;
|
62
|
+
break;
|
63
|
+
}
|
64
|
+
} else if (-OJ_INFINITY == d) {
|
65
|
+
switch (nd) {
|
66
|
+
case RaiseNan:
|
67
|
+
case WordNan: raise_strict(obj); break;
|
68
|
+
case NullNan:
|
69
|
+
strcpy(buf, "null");
|
70
|
+
cnt = 4;
|
71
|
+
break;
|
72
|
+
case HugeNan:
|
73
|
+
default:
|
74
|
+
strcpy(buf, ninf_val);
|
75
|
+
cnt = sizeof(ninf_val) - 1;
|
76
|
+
break;
|
77
|
+
}
|
78
|
+
} else if (isnan(d)) {
|
79
|
+
switch (nd) {
|
80
|
+
case RaiseNan:
|
81
|
+
case WordNan: raise_strict(obj); break;
|
82
|
+
case NullNan:
|
83
|
+
strcpy(buf, "null");
|
84
|
+
cnt = 4;
|
85
|
+
break;
|
86
|
+
case HugeNan:
|
87
|
+
default:
|
88
|
+
strcpy(buf, nan_val);
|
89
|
+
cnt = sizeof(nan_val) - 1;
|
90
|
+
break;
|
91
|
+
}
|
92
|
+
} else if (d == (double)(long long int)d) {
|
93
|
+
cnt = snprintf(buf, sizeof(buf), "%.1f", d);
|
94
|
+
} else if (0 == out->opts->float_prec) {
|
95
|
+
volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
|
96
|
+
|
97
|
+
cnt = (int)RSTRING_LEN(rstr);
|
98
|
+
if ((int)sizeof(buf) <= cnt) {
|
99
|
+
cnt = sizeof(buf) - 1;
|
100
|
+
}
|
101
|
+
strncpy(buf, rb_string_value_ptr((VALUE *)&rstr), cnt);
|
102
|
+
buf[cnt] = '\0';
|
103
|
+
} else {
|
104
|
+
cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, out->opts->float_fmt);
|
105
|
+
}
|
114
106
|
}
|
115
107
|
assure_size(out, cnt);
|
116
108
|
for (b = buf; '\0' != *b; b++) {
|
117
|
-
|
109
|
+
*out->cur++ = *b;
|
118
110
|
}
|
119
111
|
*out->cur = '\0';
|
120
112
|
}
|
121
113
|
|
122
|
-
static void
|
123
|
-
|
124
|
-
|
125
|
-
int
|
126
|
-
int d2 = depth + 1;
|
114
|
+
static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
|
115
|
+
size_t size;
|
116
|
+
int i, cnt;
|
117
|
+
int d2 = depth + 1;
|
127
118
|
|
128
119
|
if (Yes == out->opts->circular) {
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
120
|
+
if (0 > oj_check_circular(a, out)) {
|
121
|
+
oj_dump_nil(Qnil, 0, out, false);
|
122
|
+
return;
|
123
|
+
}
|
133
124
|
}
|
134
|
-
cnt
|
125
|
+
cnt = (int)RARRAY_LEN(a);
|
135
126
|
*out->cur++ = '[';
|
136
|
-
size
|
127
|
+
size = 2;
|
137
128
|
assure_size(out, size);
|
138
129
|
if (0 == cnt) {
|
139
|
-
|
130
|
+
*out->cur++ = ']';
|
140
131
|
} else {
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
132
|
+
if (out->opts->dump_opts.use) {
|
133
|
+
size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
|
134
|
+
} else {
|
135
|
+
size = d2 * out->indent + 2;
|
136
|
+
}
|
137
|
+
cnt--;
|
138
|
+
for (i = 0; i <= cnt; i++) {
|
139
|
+
assure_size(out, size);
|
140
|
+
if (out->opts->dump_opts.use) {
|
141
|
+
if (0 < out->opts->dump_opts.array_size) {
|
142
|
+
strcpy(out->cur, out->opts->dump_opts.array_nl);
|
143
|
+
out->cur += out->opts->dump_opts.array_size;
|
144
|
+
}
|
145
|
+
if (0 < out->opts->dump_opts.indent_size) {
|
146
|
+
int i;
|
147
|
+
for (i = d2; 0 < i; i--) {
|
148
|
+
strcpy(out->cur, out->opts->dump_opts.indent_str);
|
149
|
+
out->cur += out->opts->dump_opts.indent_size;
|
150
|
+
}
|
151
|
+
}
|
152
|
+
} else {
|
153
|
+
fill_indent(out, d2);
|
154
|
+
}
|
155
|
+
if (NullMode == out->opts->mode) {
|
156
|
+
oj_dump_null_val(rb_ary_entry(a, i), d2, out);
|
157
|
+
} else {
|
158
|
+
oj_dump_strict_val(rb_ary_entry(a, i), d2, out);
|
159
|
+
}
|
160
|
+
if (i < cnt) {
|
161
|
+
*out->cur++ = ',';
|
162
|
+
}
|
163
|
+
}
|
164
|
+
size = depth * out->indent + 1;
|
165
|
+
assure_size(out, size);
|
166
|
+
if (out->opts->dump_opts.use) {
|
167
|
+
// printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
|
168
|
+
// out->opts->dump_opts->indent);
|
169
|
+
if (0 < out->opts->dump_opts.array_size) {
|
170
|
+
strcpy(out->cur, out->opts->dump_opts.array_nl);
|
171
|
+
out->cur += out->opts->dump_opts.array_size;
|
172
|
+
}
|
173
|
+
if (0 < out->opts->dump_opts.indent_size) {
|
174
|
+
int i;
|
175
|
+
|
176
|
+
for (i = depth; 0 < i; i--) {
|
177
|
+
strcpy(out->cur, out->opts->dump_opts.indent_str);
|
178
|
+
out->cur += out->opts->dump_opts.indent_size;
|
179
|
+
}
|
180
|
+
}
|
181
|
+
} else {
|
182
|
+
fill_indent(out, depth);
|
183
|
+
}
|
184
|
+
*out->cur++ = ']';
|
193
185
|
}
|
194
186
|
*out->cur = '\0';
|
195
187
|
}
|
196
188
|
|
197
|
-
static int
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
int rtype = rb_type(key);
|
189
|
+
static int hash_cb(VALUE key, VALUE value, VALUE ov) {
|
190
|
+
Out out = (Out)ov;
|
191
|
+
int depth = out->depth;
|
192
|
+
long size;
|
193
|
+
int rtype = rb_type(key);
|
203
194
|
|
204
195
|
if (rtype != T_STRING && rtype != T_SYMBOL) {
|
205
|
-
|
196
|
+
rb_raise(rb_eTypeError,
|
197
|
+
"In :strict and :null mode all Hash keys must be Strings or Symbols, not %s.\n",
|
198
|
+
rb_class2name(rb_obj_class(key)));
|
206
199
|
}
|
207
200
|
if (out->omit_nil && Qnil == value) {
|
208
|
-
|
201
|
+
return ST_CONTINUE;
|
209
202
|
}
|
210
203
|
if (!out->opts->dump_opts.use) {
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
204
|
+
size = depth * out->indent + 1;
|
205
|
+
assure_size(out, size);
|
206
|
+
fill_indent(out, depth);
|
207
|
+
if (rtype == T_STRING) {
|
208
|
+
oj_dump_str(key, 0, out, false);
|
209
|
+
} else {
|
210
|
+
oj_dump_sym(key, 0, out, false);
|
211
|
+
}
|
212
|
+
*out->cur++ = ':';
|
220
213
|
} else {
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
214
|
+
size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
|
215
|
+
assure_size(out, size);
|
216
|
+
if (0 < out->opts->dump_opts.hash_size) {
|
217
|
+
strcpy(out->cur, out->opts->dump_opts.hash_nl);
|
218
|
+
out->cur += out->opts->dump_opts.hash_size;
|
219
|
+
}
|
220
|
+
if (0 < out->opts->dump_opts.indent_size) {
|
221
|
+
int i;
|
222
|
+
for (i = depth; 0 < i; i--) {
|
223
|
+
strcpy(out->cur, out->opts->dump_opts.indent_str);
|
224
|
+
out->cur += out->opts->dump_opts.indent_size;
|
225
|
+
}
|
226
|
+
}
|
227
|
+
if (rtype == T_STRING) {
|
228
|
+
oj_dump_str(key, 0, out, false);
|
229
|
+
} else {
|
230
|
+
oj_dump_sym(key, 0, out, false);
|
231
|
+
}
|
232
|
+
size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
|
233
|
+
assure_size(out, size);
|
234
|
+
if (0 < out->opts->dump_opts.before_size) {
|
235
|
+
strcpy(out->cur, out->opts->dump_opts.before_sep);
|
236
|
+
out->cur += out->opts->dump_opts.before_size;
|
237
|
+
}
|
238
|
+
*out->cur++ = ':';
|
239
|
+
if (0 < out->opts->dump_opts.after_size) {
|
240
|
+
strcpy(out->cur, out->opts->dump_opts.after_sep);
|
241
|
+
out->cur += out->opts->dump_opts.after_size;
|
242
|
+
}
|
250
243
|
}
|
251
244
|
if (NullMode == out->opts->mode) {
|
252
|
-
|
245
|
+
oj_dump_null_val(value, depth, out);
|
253
246
|
} else {
|
254
|
-
|
247
|
+
oj_dump_strict_val(value, depth, out);
|
255
248
|
}
|
256
|
-
out->depth
|
249
|
+
out->depth = depth;
|
257
250
|
*out->cur++ = ',';
|
258
251
|
|
259
252
|
return ST_CONTINUE;
|
260
253
|
}
|
261
254
|
|
262
|
-
static void
|
263
|
-
|
264
|
-
|
265
|
-
size_t size;
|
255
|
+
static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
|
256
|
+
int cnt;
|
257
|
+
size_t size;
|
266
258
|
|
267
259
|
if (Yes == out->opts->circular) {
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
260
|
+
if (0 > oj_check_circular(obj, out)) {
|
261
|
+
oj_dump_nil(Qnil, 0, out, false);
|
262
|
+
return;
|
263
|
+
}
|
272
264
|
}
|
273
|
-
cnt
|
265
|
+
cnt = (int)RHASH_SIZE(obj);
|
274
266
|
size = depth * out->indent + 2;
|
275
267
|
assure_size(out, 2);
|
276
268
|
*out->cur++ = '{';
|
277
269
|
if (0 == cnt) {
|
278
|
-
|
270
|
+
*out->cur++ = '}';
|
279
271
|
} else {
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
272
|
+
out->depth = depth + 1;
|
273
|
+
rb_hash_foreach(obj, hash_cb, (VALUE)out);
|
274
|
+
if (',' == *(out->cur - 1)) {
|
275
|
+
out->cur--; // backup to overwrite last comma
|
276
|
+
}
|
277
|
+
if (!out->opts->dump_opts.use) {
|
278
|
+
assure_size(out, size);
|
279
|
+
fill_indent(out, depth);
|
280
|
+
} else {
|
281
|
+
size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
|
282
|
+
assure_size(out, size);
|
283
|
+
if (0 < out->opts->dump_opts.hash_size) {
|
284
|
+
strcpy(out->cur, out->opts->dump_opts.hash_nl);
|
285
|
+
out->cur += out->opts->dump_opts.hash_size;
|
286
|
+
}
|
287
|
+
if (0 < out->opts->dump_opts.indent_size) {
|
288
|
+
int i;
|
289
|
+
|
290
|
+
for (i = depth; 0 < i; i--) {
|
291
|
+
strcpy(out->cur, out->opts->dump_opts.indent_str);
|
292
|
+
out->cur += out->opts->dump_opts.indent_size;
|
293
|
+
}
|
294
|
+
}
|
295
|
+
}
|
296
|
+
*out->cur++ = '}';
|
305
297
|
}
|
306
298
|
*out->cur = '\0';
|
307
299
|
}
|
308
300
|
|
309
|
-
static void
|
310
|
-
|
311
|
-
VALUE clas = rb_obj_class(obj);
|
301
|
+
static void dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
|
302
|
+
VALUE clas = rb_obj_class(obj);
|
312
303
|
|
313
304
|
if (oj_bigdecimal_class == clas) {
|
314
|
-
|
305
|
+
volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
|
315
306
|
|
316
|
-
|
307
|
+
oj_dump_raw(rb_string_value_ptr((VALUE *)&rstr), (int)RSTRING_LEN(rstr), out);
|
317
308
|
} else {
|
318
|
-
|
309
|
+
raise_strict(obj);
|
319
310
|
}
|
320
311
|
}
|
321
312
|
|
322
|
-
static void
|
323
|
-
|
324
|
-
VALUE clas = rb_obj_class(obj);
|
313
|
+
static void dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
|
314
|
+
VALUE clas = rb_obj_class(obj);
|
325
315
|
|
326
316
|
if (oj_bigdecimal_class == clas) {
|
327
|
-
|
317
|
+
volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
|
328
318
|
|
329
|
-
|
319
|
+
oj_dump_raw(rb_string_value_ptr((VALUE *)&rstr), (int)RSTRING_LEN(rstr), out);
|
330
320
|
} else {
|
331
|
-
|
321
|
+
oj_dump_nil(Qnil, depth, out, false);
|
332
322
|
}
|
333
323
|
}
|
334
324
|
|
335
|
-
static DumpFunc
|
336
|
-
NULL,
|
337
|
-
dump_data_strict,
|
338
|
-
NULL,
|
339
|
-
NULL,
|
340
|
-
dump_float,
|
341
|
-
oj_dump_str,
|
342
|
-
NULL,
|
343
|
-
dump_array,
|
344
|
-
dump_hash,
|
345
|
-
NULL,
|
346
|
-
oj_dump_bignum,
|
347
|
-
NULL,
|
348
|
-
dump_data_strict,
|
349
|
-
NULL,
|
350
|
-
NULL,
|
351
|
-
NULL,
|
352
|
-
NULL,
|
353
|
-
oj_dump_nil,
|
354
|
-
oj_dump_true,
|
355
|
-
oj_dump_false,
|
356
|
-
oj_dump_sym,
|
357
|
-
oj_dump_fixnum,
|
325
|
+
static DumpFunc strict_funcs[] = {
|
326
|
+
NULL, // RUBY_T_NONE = 0x00,
|
327
|
+
dump_data_strict, // RUBY_T_OBJECT = 0x01,
|
328
|
+
NULL, // RUBY_T_CLASS = 0x02,
|
329
|
+
NULL, // RUBY_T_MODULE = 0x03,
|
330
|
+
dump_float, // RUBY_T_FLOAT = 0x04,
|
331
|
+
oj_dump_str, // RUBY_T_STRING = 0x05,
|
332
|
+
NULL, // RUBY_T_REGEXP = 0x06,
|
333
|
+
dump_array, // RUBY_T_ARRAY = 0x07,
|
334
|
+
dump_hash, // RUBY_T_HASH = 0x08,
|
335
|
+
NULL, // RUBY_T_STRUCT = 0x09,
|
336
|
+
oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
|
337
|
+
NULL, // RUBY_T_FILE = 0x0b,
|
338
|
+
dump_data_strict, // RUBY_T_DATA = 0x0c,
|
339
|
+
NULL, // RUBY_T_MATCH = 0x0d,
|
340
|
+
NULL, // RUBY_T_COMPLEX = 0x0e,
|
341
|
+
NULL, // RUBY_T_RATIONAL = 0x0f,
|
342
|
+
NULL, // 0x10
|
343
|
+
oj_dump_nil, // RUBY_T_NIL = 0x11,
|
344
|
+
oj_dump_true, // RUBY_T_TRUE = 0x12,
|
345
|
+
oj_dump_false, // RUBY_T_FALSE = 0x13,
|
346
|
+
oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
|
347
|
+
oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
|
358
348
|
};
|
359
349
|
|
360
|
-
void
|
361
|
-
|
362
|
-
int type = rb_type(obj);
|
350
|
+
void oj_dump_strict_val(VALUE obj, int depth, Out out) {
|
351
|
+
int type = rb_type(obj);
|
363
352
|
|
364
353
|
if (Yes == out->opts->trace) {
|
365
|
-
|
354
|
+
oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceIn);
|
366
355
|
}
|
367
356
|
if (MAX_DEPTH < depth) {
|
368
|
-
|
357
|
+
rb_raise(rb_eNoMemError, "Too deeply nested.\n");
|
369
358
|
}
|
370
359
|
if (0 < type && type <= RUBY_T_FIXNUM) {
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
360
|
+
DumpFunc f = strict_funcs[type];
|
361
|
+
|
362
|
+
if (NULL != f) {
|
363
|
+
f(obj, depth, out, false);
|
364
|
+
if (Yes == out->opts->trace) {
|
365
|
+
oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
|
366
|
+
}
|
367
|
+
return;
|
368
|
+
}
|
380
369
|
}
|
381
370
|
raise_strict(obj);
|
382
371
|
}
|
383
372
|
|
384
|
-
static DumpFunc
|
385
|
-
NULL,
|
386
|
-
dump_data_null,
|
387
|
-
NULL,
|
388
|
-
NULL,
|
389
|
-
dump_float,
|
390
|
-
oj_dump_str,
|
391
|
-
NULL,
|
392
|
-
dump_array,
|
393
|
-
dump_hash,
|
394
|
-
NULL,
|
395
|
-
oj_dump_bignum,
|
396
|
-
NULL,
|
397
|
-
dump_data_null,
|
398
|
-
NULL,
|
399
|
-
NULL,
|
400
|
-
NULL,
|
401
|
-
NULL,
|
402
|
-
oj_dump_nil,
|
403
|
-
oj_dump_true,
|
404
|
-
oj_dump_false,
|
405
|
-
oj_dump_sym,
|
406
|
-
oj_dump_fixnum,
|
373
|
+
static DumpFunc null_funcs[] = {
|
374
|
+
NULL, // RUBY_T_NONE = 0x00,
|
375
|
+
dump_data_null, // RUBY_T_OBJECT = 0x01,
|
376
|
+
NULL, // RUBY_T_CLASS = 0x02,
|
377
|
+
NULL, // RUBY_T_MODULE = 0x03,
|
378
|
+
dump_float, // RUBY_T_FLOAT = 0x04,
|
379
|
+
oj_dump_str, // RUBY_T_STRING = 0x05,
|
380
|
+
NULL, // RUBY_T_REGEXP = 0x06,
|
381
|
+
dump_array, // RUBY_T_ARRAY = 0x07,
|
382
|
+
dump_hash, // RUBY_T_HASH = 0x08,
|
383
|
+
NULL, // RUBY_T_STRUCT = 0x09,
|
384
|
+
oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
|
385
|
+
NULL, // RUBY_T_FILE = 0x0b,
|
386
|
+
dump_data_null, // RUBY_T_DATA = 0x0c,
|
387
|
+
NULL, // RUBY_T_MATCH = 0x0d,
|
388
|
+
NULL, // RUBY_T_COMPLEX = 0x0e,
|
389
|
+
NULL, // RUBY_T_RATIONAL = 0x0f,
|
390
|
+
NULL, // 0x10
|
391
|
+
oj_dump_nil, // RUBY_T_NIL = 0x11,
|
392
|
+
oj_dump_true, // RUBY_T_TRUE = 0x12,
|
393
|
+
oj_dump_false, // RUBY_T_FALSE = 0x13,
|
394
|
+
oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
|
395
|
+
oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
|
407
396
|
};
|
408
397
|
|
409
|
-
void
|
410
|
-
|
411
|
-
int type = rb_type(obj);
|
398
|
+
void oj_dump_null_val(VALUE obj, int depth, Out out) {
|
399
|
+
int type = rb_type(obj);
|
412
400
|
|
413
401
|
if (Yes == out->opts->trace) {
|
414
|
-
|
402
|
+
oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
|
415
403
|
}
|
416
404
|
if (MAX_DEPTH < depth) {
|
417
|
-
|
405
|
+
rb_raise(rb_eNoMemError, "Too deeply nested.\n");
|
418
406
|
}
|
419
407
|
if (0 < type && type <= RUBY_T_FIXNUM) {
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
408
|
+
DumpFunc f = null_funcs[type];
|
409
|
+
|
410
|
+
if (NULL != f) {
|
411
|
+
f(obj, depth, out, false);
|
412
|
+
if (Yes == out->opts->trace) {
|
413
|
+
oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
|
414
|
+
}
|
415
|
+
return;
|
416
|
+
}
|
429
417
|
}
|
430
418
|
oj_dump_nil(Qnil, depth, out, false);
|
431
419
|
if (Yes == out->opts->trace) {
|
432
|
-
|
420
|
+
oj_trace("dump", Qnil, __FILE__, __LINE__, depth, TraceOut);
|
433
421
|
}
|
434
422
|
}
|