oj 3.10.6 → 3.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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/circarray.h
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
*/
|
1
|
+
// Copyright (c) 2012 Peter Ohler. All rights reserved.
|
2
|
+
// Licensed under the MIT License. See LICENSE file in the project root for
|
3
|
+
// license details.
|
5
4
|
|
6
5
|
#ifndef OJ_CIRCARRAY_H
|
7
6
|
#define OJ_CIRCARRAY_H
|
@@ -9,15 +8,15 @@
|
|
9
8
|
#include "ruby.h"
|
10
9
|
|
11
10
|
typedef struct _circArray {
|
12
|
-
VALUE
|
13
|
-
VALUE
|
14
|
-
unsigned long
|
15
|
-
unsigned long
|
16
|
-
} *CircArray;
|
11
|
+
VALUE obj_array[1024];
|
12
|
+
VALUE * objs;
|
13
|
+
unsigned long size; // allocated size or initial array size
|
14
|
+
unsigned long cnt;
|
15
|
+
} * CircArray;
|
17
16
|
|
18
|
-
extern CircArray
|
19
|
-
extern void
|
20
|
-
extern void
|
21
|
-
extern VALUE
|
17
|
+
extern CircArray oj_circ_array_new(void);
|
18
|
+
extern void oj_circ_array_free(CircArray ca);
|
19
|
+
extern void oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id);
|
20
|
+
extern VALUE oj_circ_array_get(CircArray ca, unsigned long id);
|
22
21
|
|
23
22
|
#endif /* OJ_CIRCARRAY_H */
|
data/ext/oj/code.c
CHANGED
@@ -1,235 +1,228 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
* All rights reserved.
|
4
|
-
*/
|
1
|
+
// Copyright (c) 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 "code.h"
|
5
|
+
|
7
6
|
#include "dump.h"
|
8
7
|
|
9
|
-
inline static VALUE
|
10
|
-
|
11
|
-
|
12
|
-
ID ci = rb_intern(classname);
|
8
|
+
inline static VALUE resolve_classname(VALUE mod, const char *classname) {
|
9
|
+
VALUE clas = Qundef;
|
10
|
+
ID ci = rb_intern(classname);
|
13
11
|
|
14
12
|
if (rb_const_defined_at(mod, ci)) {
|
15
|
-
|
13
|
+
clas = rb_const_get_at(mod, ci);
|
16
14
|
}
|
17
15
|
return clas;
|
18
16
|
}
|
19
17
|
|
20
|
-
static VALUE
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
char
|
25
|
-
char
|
26
|
-
const char *n = name;
|
18
|
+
static VALUE path2class(const char *name) {
|
19
|
+
char class_name[1024];
|
20
|
+
VALUE clas;
|
21
|
+
char * end = class_name + sizeof(class_name) - 1;
|
22
|
+
char * s;
|
23
|
+
const char *n = name;
|
27
24
|
|
28
25
|
clas = rb_cObject;
|
29
26
|
for (s = class_name; '\0' != *n; n++) {
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
27
|
+
if (':' == *n) {
|
28
|
+
*s = '\0';
|
29
|
+
n++;
|
30
|
+
if (':' != *n) {
|
31
|
+
return Qundef;
|
32
|
+
}
|
33
|
+
if (Qundef == (clas = resolve_classname(clas, class_name))) {
|
34
|
+
return Qundef;
|
35
|
+
}
|
36
|
+
s = class_name;
|
37
|
+
} else if (end <= s) {
|
38
|
+
return Qundef;
|
39
|
+
} else {
|
40
|
+
*s++ = *n;
|
41
|
+
}
|
45
42
|
}
|
46
43
|
*s = '\0';
|
47
44
|
|
48
45
|
return resolve_classname(clas, class_name);
|
49
46
|
}
|
50
47
|
|
51
|
-
bool
|
52
|
-
|
53
|
-
|
54
|
-
Code c = codes;
|
48
|
+
bool oj_code_dump(Code codes, VALUE obj, int depth, Out out) {
|
49
|
+
VALUE clas = rb_obj_class(obj);
|
50
|
+
Code c = codes;
|
55
51
|
|
56
52
|
for (; NULL != c->name; c++) {
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
53
|
+
if (Qundef == c->clas) { // indicates not defined
|
54
|
+
continue;
|
55
|
+
}
|
56
|
+
if (Qnil == c->clas) {
|
57
|
+
c->clas = path2class(c->name);
|
58
|
+
}
|
59
|
+
if (clas == c->clas && c->active) {
|
60
|
+
c->encode(obj, depth, out);
|
61
|
+
return true;
|
62
|
+
}
|
67
63
|
}
|
68
64
|
return false;
|
69
65
|
}
|
70
66
|
|
71
67
|
VALUE
|
72
68
|
oj_code_load(Code codes, VALUE clas, VALUE args) {
|
73
|
-
Code
|
69
|
+
Code c = codes;
|
74
70
|
|
75
71
|
for (; NULL != c->name; c++) {
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
72
|
+
if (Qundef == c->clas) { // indicates not defined
|
73
|
+
continue;
|
74
|
+
}
|
75
|
+
if (Qnil == c->clas) {
|
76
|
+
c->clas = path2class(c->name);
|
77
|
+
}
|
78
|
+
if (clas == c->clas) {
|
79
|
+
if (NULL == c->decode) {
|
80
|
+
break;
|
81
|
+
}
|
82
|
+
return c->decode(clas, args);
|
83
|
+
}
|
88
84
|
}
|
89
85
|
return Qnil;
|
90
86
|
}
|
91
87
|
|
92
|
-
void
|
93
|
-
|
94
|
-
Code c = codes;
|
88
|
+
void oj_code_set_active(Code codes, VALUE clas, bool active) {
|
89
|
+
Code c = codes;
|
95
90
|
|
96
91
|
for (; NULL != c->name; c++) {
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
92
|
+
if (Qundef == c->clas) { // indicates not defined
|
93
|
+
continue;
|
94
|
+
}
|
95
|
+
if (Qnil == c->clas) {
|
96
|
+
c->clas = path2class(c->name);
|
97
|
+
}
|
98
|
+
if (clas == c->clas || Qnil == clas) {
|
99
|
+
c->active = active;
|
100
|
+
if (Qnil != clas) {
|
101
|
+
break;
|
102
|
+
}
|
103
|
+
}
|
109
104
|
}
|
110
105
|
}
|
111
106
|
|
112
|
-
bool
|
113
|
-
|
114
|
-
Code c = codes;
|
107
|
+
bool oj_code_has(Code codes, VALUE clas, bool encode) {
|
108
|
+
Code c = codes;
|
115
109
|
|
116
110
|
for (; NULL != c->name; c++) {
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
111
|
+
if (Qundef == c->clas) { // indicates not defined
|
112
|
+
continue;
|
113
|
+
}
|
114
|
+
if (Qnil == c->clas) {
|
115
|
+
c->clas = path2class(c->name);
|
116
|
+
}
|
117
|
+
if (clas == c->clas) {
|
118
|
+
if (encode) {
|
119
|
+
return c->active && NULL != c->encode;
|
120
|
+
} else {
|
121
|
+
return c->active && NULL != c->decode;
|
122
|
+
}
|
123
|
+
}
|
130
124
|
}
|
131
125
|
return false;
|
132
126
|
}
|
133
127
|
|
134
|
-
void
|
135
|
-
|
136
|
-
int
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
size_t
|
141
|
-
|
142
|
-
|
143
|
-
|
128
|
+
void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class) {
|
129
|
+
int d2 = depth + 1;
|
130
|
+
int d3 = d2 + 1;
|
131
|
+
size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
|
132
|
+
const char *classname = rb_obj_classname(obj);
|
133
|
+
size_t len = strlen(classname);
|
134
|
+
size_t size = d2 * out->indent + 10 + len + out->opts->create_id_len + sep_len;
|
135
|
+
bool no_comma = true;
|
136
|
+
|
144
137
|
assure_size(out, size);
|
145
138
|
*out->cur++ = '{';
|
146
139
|
|
147
140
|
if (with_class) {
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
141
|
+
fill_indent(out, d2);
|
142
|
+
*out->cur++ = '"';
|
143
|
+
memcpy(out->cur, out->opts->create_id, out->opts->create_id_len);
|
144
|
+
out->cur += out->opts->create_id_len;
|
145
|
+
*out->cur++ = '"';
|
146
|
+
if (0 < out->opts->dump_opts.before_size) {
|
147
|
+
strcpy(out->cur, out->opts->dump_opts.before_sep);
|
148
|
+
out->cur += out->opts->dump_opts.before_size;
|
149
|
+
}
|
150
|
+
*out->cur++ = ':';
|
151
|
+
if (0 < out->opts->dump_opts.after_size) {
|
152
|
+
strcpy(out->cur, out->opts->dump_opts.after_sep);
|
153
|
+
out->cur += out->opts->dump_opts.after_size;
|
154
|
+
}
|
155
|
+
*out->cur++ = '"';
|
156
|
+
memcpy(out->cur, classname, len);
|
157
|
+
out->cur += len;
|
158
|
+
*out->cur++ = '"';
|
159
|
+
no_comma = false;
|
167
160
|
}
|
168
161
|
size = d3 * out->indent + 2;
|
169
162
|
for (; NULL != attrs->name; attrs++) {
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
163
|
+
assure_size(out, size + attrs->len + sep_len + 2);
|
164
|
+
if (no_comma) {
|
165
|
+
no_comma = false;
|
166
|
+
} else {
|
167
|
+
*out->cur++ = ',';
|
168
|
+
}
|
169
|
+
fill_indent(out, d2);
|
170
|
+
*out->cur++ = '"';
|
171
|
+
memcpy(out->cur, attrs->name, attrs->len);
|
172
|
+
out->cur += attrs->len;
|
173
|
+
*out->cur++ = '"';
|
174
|
+
if (0 < out->opts->dump_opts.before_size) {
|
175
|
+
strcpy(out->cur, out->opts->dump_opts.before_sep);
|
176
|
+
out->cur += out->opts->dump_opts.before_size;
|
177
|
+
}
|
178
|
+
*out->cur++ = ':';
|
179
|
+
if (0 < out->opts->dump_opts.after_size) {
|
180
|
+
strcpy(out->cur, out->opts->dump_opts.after_sep);
|
181
|
+
out->cur += out->opts->dump_opts.after_size;
|
182
|
+
}
|
183
|
+
if (Qundef == attrs->value) {
|
184
|
+
if (Qundef != attrs->time) {
|
185
|
+
switch (out->opts->time_format) {
|
186
|
+
case RubyTime: oj_dump_ruby_time(attrs->time, out); break;
|
187
|
+
case XmlTime: oj_dump_xml_time(attrs->time, out); break;
|
188
|
+
case UnixZTime: oj_dump_time(attrs->time, out, true); break;
|
189
|
+
case UnixTime:
|
190
|
+
default: oj_dump_time(attrs->time, out, false); break;
|
191
|
+
}
|
192
|
+
} else {
|
193
|
+
char buf[32];
|
194
|
+
char *b = buf + sizeof(buf) - 1;
|
195
|
+
int neg = 0;
|
196
|
+
long num = attrs->num;
|
197
|
+
|
198
|
+
if (0 > num) {
|
199
|
+
neg = 1;
|
200
|
+
num = -num;
|
201
|
+
}
|
202
|
+
*b-- = '\0';
|
203
|
+
if (0 < num) {
|
204
|
+
for (; 0 < num; num /= 10, b--) {
|
205
|
+
*b = (num % 10) + '0';
|
206
|
+
}
|
207
|
+
if (neg) {
|
208
|
+
*b = '-';
|
209
|
+
} else {
|
210
|
+
b++;
|
211
|
+
}
|
212
|
+
} else {
|
213
|
+
*b = '0';
|
214
|
+
}
|
215
|
+
assure_size(out, (sizeof(buf) - (b - buf)));
|
216
|
+
for (; '\0' != *b; b++) {
|
217
|
+
*out->cur++ = *b;
|
218
|
+
}
|
219
|
+
}
|
220
|
+
} else {
|
221
|
+
oj_dump_compat_val(attrs->value, d3, out, true);
|
222
|
+
}
|
230
223
|
}
|
231
224
|
assure_size(out, depth * out->indent + 2);
|
232
225
|
fill_indent(out, depth);
|
233
226
|
*out->cur++ = '}';
|
234
|
-
*out->cur
|
227
|
+
*out->cur = '\0';
|
235
228
|
}
|
data/ext/oj/code.h
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
* All rights reserved.
|
4
|
-
*/
|
1
|
+
// Copyright (c) 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
|
#ifndef OJ_CODE_H
|
7
5
|
#define OJ_CODE_H
|
@@ -10,33 +8,33 @@
|
|
10
8
|
|
11
9
|
#include "oj.h"
|
12
10
|
|
13
|
-
typedef void
|
14
|
-
typedef VALUE
|
11
|
+
typedef void (*EncodeFunc)(VALUE obj, int depth, Out out);
|
12
|
+
typedef VALUE (*DecodeFunc)(VALUE clas, VALUE args);
|
15
13
|
|
16
14
|
typedef struct _code {
|
17
|
-
const char
|
18
|
-
VALUE
|
19
|
-
EncodeFunc
|
20
|
-
DecodeFunc
|
21
|
-
bool
|
22
|
-
} *Code;
|
15
|
+
const char *name;
|
16
|
+
VALUE clas;
|
17
|
+
EncodeFunc encode;
|
18
|
+
DecodeFunc decode;
|
19
|
+
bool active; // For compat mode.
|
20
|
+
} * Code;
|
23
21
|
|
24
22
|
// Used by encode functions.
|
25
23
|
typedef struct _attr {
|
26
|
-
const char
|
27
|
-
int
|
28
|
-
VALUE
|
29
|
-
long
|
30
|
-
VALUE
|
31
|
-
} *Attr;
|
24
|
+
const char *name;
|
25
|
+
int len;
|
26
|
+
VALUE value;
|
27
|
+
long num;
|
28
|
+
VALUE time;
|
29
|
+
} * Attr;
|
32
30
|
|
33
|
-
extern bool
|
34
|
-
extern VALUE
|
35
|
-
extern void
|
36
|
-
extern bool
|
31
|
+
extern bool oj_code_dump(Code codes, VALUE obj, int depth, Out out);
|
32
|
+
extern VALUE oj_code_load(Code codes, VALUE clas, VALUE args);
|
33
|
+
extern void oj_code_set_active(Code codes, VALUE clas, bool active);
|
34
|
+
extern bool oj_code_has(Code codes, VALUE clas, bool encode);
|
37
35
|
|
38
|
-
extern void
|
36
|
+
extern void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class);
|
39
37
|
|
40
|
-
extern struct _code
|
38
|
+
extern struct _code oj_compat_codes[];
|
41
39
|
|
42
40
|
#endif /* OJ_CODE_H */
|