oj 3.11.0 → 3.11.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/ext/oj/buf.h +34 -38
- data/ext/oj/cache8.c +59 -62
- data/ext/oj/cache8.h +8 -7
- data/ext/oj/circarray.c +33 -35
- data/ext/oj/circarray.h +11 -9
- data/ext/oj/code.c +170 -174
- data/ext/oj/code.h +21 -20
- data/ext/oj/compat.c +159 -166
- data/ext/oj/custom.c +802 -851
- data/ext/oj/dump.c +766 -778
- data/ext/oj/dump.h +49 -51
- data/ext/oj/dump_compat.c +1 -0
- data/ext/oj/dump_leaf.c +116 -157
- data/ext/oj/dump_object.c +609 -628
- data/ext/oj/dump_strict.c +318 -327
- data/ext/oj/encode.h +3 -4
- data/ext/oj/err.c +39 -25
- data/ext/oj/err.h +24 -15
- data/ext/oj/extconf.rb +2 -1
- data/ext/oj/fast.c +1008 -1038
- data/ext/oj/hash.c +62 -66
- data/ext/oj/hash.h +7 -6
- data/ext/oj/hash_test.c +450 -443
- data/ext/oj/mimic_json.c +413 -402
- data/ext/oj/object.c +559 -528
- data/ext/oj/odd.c +123 -128
- data/ext/oj/odd.h +27 -25
- data/ext/oj/oj.c +1131 -924
- data/ext/oj/oj.h +286 -298
- data/ext/oj/parse.c +938 -930
- data/ext/oj/parse.h +70 -69
- data/ext/oj/rails.c +836 -839
- data/ext/oj/rails.h +7 -7
- data/ext/oj/reader.c +135 -140
- data/ext/oj/reader.h +66 -79
- data/ext/oj/resolve.c +43 -43
- data/ext/oj/resolve.h +3 -2
- data/ext/oj/rxclass.c +67 -68
- data/ext/oj/rxclass.h +12 -10
- data/ext/oj/saj.c +451 -479
- data/ext/oj/scp.c +93 -103
- data/ext/oj/sparse.c +770 -730
- data/ext/oj/stream_writer.c +120 -149
- data/ext/oj/strict.c +71 -86
- data/ext/oj/string_writer.c +198 -243
- data/ext/oj/trace.c +29 -33
- data/ext/oj/trace.h +14 -11
- data/ext/oj/util.c +103 -103
- data/ext/oj/util.h +3 -2
- data/ext/oj/val_stack.c +47 -47
- data/ext/oj/val_stack.h +79 -86
- data/ext/oj/wab.c +291 -309
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +5 -4
- data/lib/oj/mimic.rb +0 -12
- data/lib/oj/version.rb +1 -1
- data/test/activerecord/result_test.rb +7 -2
- data/test/foo.rb +35 -32
- data/test/helper.rb +10 -0
- data/test/json_gem/json_generator_test.rb +15 -3
- data/test/json_gem/test_helper.rb +8 -0
- data/test/test_compat.rb +2 -2
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +10 -0
- data/test/test_scp.rb +1 -1
- metadata +4 -2
data/ext/oj/code.c
CHANGED
@@ -1,232 +1,228 @@
|
|
1
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.
|
2
3
|
|
3
4
|
#include "code.h"
|
5
|
+
|
4
6
|
#include "dump.h"
|
5
7
|
|
6
|
-
inline static VALUE
|
7
|
-
|
8
|
-
|
9
|
-
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);
|
10
11
|
|
11
12
|
if (rb_const_defined_at(mod, ci)) {
|
12
|
-
|
13
|
+
clas = rb_const_get_at(mod, ci);
|
13
14
|
}
|
14
15
|
return clas;
|
15
16
|
}
|
16
17
|
|
17
|
-
static VALUE
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
char
|
22
|
-
char
|
23
|
-
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;
|
24
24
|
|
25
25
|
clas = rb_cObject;
|
26
26
|
for (s = class_name; '\0' != *n; n++) {
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
+
}
|
42
42
|
}
|
43
43
|
*s = '\0';
|
44
44
|
|
45
45
|
return resolve_classname(clas, class_name);
|
46
46
|
}
|
47
47
|
|
48
|
-
bool
|
49
|
-
|
50
|
-
|
51
|
-
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;
|
52
51
|
|
53
52
|
for (; NULL != c->name; c++) {
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
+
}
|
64
63
|
}
|
65
64
|
return false;
|
66
65
|
}
|
67
66
|
|
68
67
|
VALUE
|
69
68
|
oj_code_load(Code codes, VALUE clas, VALUE args) {
|
70
|
-
Code
|
69
|
+
Code c = codes;
|
71
70
|
|
72
71
|
for (; NULL != c->name; c++) {
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
+
}
|
85
84
|
}
|
86
85
|
return Qnil;
|
87
86
|
}
|
88
87
|
|
89
|
-
void
|
90
|
-
|
91
|
-
Code c = codes;
|
88
|
+
void oj_code_set_active(Code codes, VALUE clas, bool active) {
|
89
|
+
Code c = codes;
|
92
90
|
|
93
91
|
for (; NULL != c->name; c++) {
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
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
|
+
}
|
106
104
|
}
|
107
105
|
}
|
108
106
|
|
109
|
-
bool
|
110
|
-
|
111
|
-
Code c = codes;
|
107
|
+
bool oj_code_has(Code codes, VALUE clas, bool encode) {
|
108
|
+
Code c = codes;
|
112
109
|
|
113
110
|
for (; NULL != c->name; c++) {
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
+
}
|
127
124
|
}
|
128
125
|
return false;
|
129
126
|
}
|
130
127
|
|
131
|
-
void
|
132
|
-
|
133
|
-
int
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
size_t
|
138
|
-
|
139
|
-
bool no_comma = true;
|
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;
|
140
136
|
|
141
137
|
assure_size(out, size);
|
142
138
|
*out->cur++ = '{';
|
143
139
|
|
144
140
|
if (with_class) {
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
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;
|
164
160
|
}
|
165
161
|
size = d3 * out->indent + 2;
|
166
162
|
for (; NULL != attrs->name; attrs++) {
|
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
|
-
|
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
|
-
|
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
|
+
}
|
227
223
|
}
|
228
224
|
assure_size(out, depth * out->indent + 2);
|
229
225
|
fill_indent(out, depth);
|
230
226
|
*out->cur++ = '}';
|
231
|
-
*out->cur
|
227
|
+
*out->cur = '\0';
|
232
228
|
}
|
data/ext/oj/code.h
CHANGED
@@ -1,4 +1,5 @@
|
|
1
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.
|
2
3
|
|
3
4
|
#ifndef OJ_CODE_H
|
4
5
|
#define OJ_CODE_H
|
@@ -7,33 +8,33 @@
|
|
7
8
|
|
8
9
|
#include "oj.h"
|
9
10
|
|
10
|
-
typedef void
|
11
|
-
typedef VALUE
|
11
|
+
typedef void (*EncodeFunc)(VALUE obj, int depth, Out out);
|
12
|
+
typedef VALUE (*DecodeFunc)(VALUE clas, VALUE args);
|
12
13
|
|
13
14
|
typedef struct _code {
|
14
|
-
const char
|
15
|
-
VALUE
|
16
|
-
EncodeFunc
|
17
|
-
DecodeFunc
|
18
|
-
bool
|
19
|
-
} *Code;
|
15
|
+
const char *name;
|
16
|
+
VALUE clas;
|
17
|
+
EncodeFunc encode;
|
18
|
+
DecodeFunc decode;
|
19
|
+
bool active; // For compat mode.
|
20
|
+
} * Code;
|
20
21
|
|
21
22
|
// Used by encode functions.
|
22
23
|
typedef struct _attr {
|
23
|
-
const char
|
24
|
-
int
|
25
|
-
VALUE
|
26
|
-
long
|
27
|
-
VALUE
|
28
|
-
} *Attr;
|
24
|
+
const char *name;
|
25
|
+
int len;
|
26
|
+
VALUE value;
|
27
|
+
long num;
|
28
|
+
VALUE time;
|
29
|
+
} * Attr;
|
29
30
|
|
30
|
-
extern bool
|
31
|
-
extern VALUE
|
32
|
-
extern void
|
33
|
-
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);
|
34
35
|
|
35
|
-
extern void
|
36
|
+
extern void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class);
|
36
37
|
|
37
|
-
extern struct _code
|
38
|
+
extern struct _code oj_compat_codes[];
|
38
39
|
|
39
40
|
#endif /* OJ_CODE_H */
|