oj 2.18.5 → 3.16.11
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 +5 -5
- data/CHANGELOG.md +1452 -0
- data/README.md +53 -221
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +54 -72
- data/ext/oj/cache.c +329 -0
- data/ext/oj/cache.h +22 -0
- data/ext/oj/cache8.c +61 -63
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +38 -67
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +214 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +194 -110
- data/ext/oj/custom.c +1074 -0
- data/ext/oj/debug.c +126 -0
- data/ext/oj/dump.c +1276 -2494
- data/ext/oj/dump.h +110 -0
- data/ext/oj/dump_compat.c +897 -0
- data/ext/oj/dump_leaf.c +162 -0
- data/ext/oj/dump_object.c +710 -0
- data/ext/oj/dump_strict.c +399 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +28 -53
- data/ext/oj/err.h +49 -46
- data/ext/oj/extconf.rb +33 -32
- data/ext/oj/fast.c +1082 -1098
- data/ext/oj/intern.c +313 -0
- data/ext/oj/intern.h +22 -0
- data/ext/oj/mem.c +318 -0
- data/ext/oj/mem.h +53 -0
- data/ext/oj/mimic_json.c +919 -0
- data/ext/oj/object.c +545 -625
- data/ext/oj/odd.c +158 -168
- data/ext/oj/odd.h +32 -58
- data/ext/oj/oj.c +1727 -2080
- data/ext/oj/oj.h +334 -259
- data/ext/oj/parse.c +974 -753
- data/ext/oj/parse.h +97 -90
- data/ext/oj/parser.c +1600 -0
- data/ext/oj/parser.h +103 -0
- data/ext/oj/rails.c +1478 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +136 -163
- data/ext/oj/reader.h +76 -112
- data/ext/oj/resolve.c +45 -94
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +144 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +445 -511
- data/ext/oj/saj2.c +584 -0
- data/ext/oj/saj2.h +23 -0
- data/ext/oj/scp.c +82 -143
- data/ext/oj/simd.h +10 -0
- data/ext/oj/sparse.c +749 -644
- data/ext/oj/stream_writer.c +329 -0
- data/ext/oj/strict.c +114 -112
- data/ext/oj/string_writer.c +517 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +55 -0
- data/ext/oj/usual.c +1218 -0
- data/ext/oj/usual.h +69 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +75 -72
- data/ext/oj/val_stack.h +94 -127
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +586 -0
- data/lib/oj/active_support_helper.rb +1 -3
- data/lib/oj/bag.rb +8 -1
- data/lib/oj/easy_hash.rb +21 -13
- data/lib/oj/error.rb +10 -12
- data/lib/oj/json.rb +188 -0
- data/lib/oj/mimic.rb +165 -26
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/schandler.rb +5 -4
- data/lib/oj/state.rb +135 -0
- data/lib/oj/version.rb +2 -3
- data/lib/oj.rb +3 -31
- 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/InstallOptions.md +20 -0
- data/pages/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +337 -0
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +167 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- metadata +126 -163
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/_test_active.rb +0 -76
- data/test/_test_active_mimic.rb +0 -96
- data/test/_test_mimic_rails.rb +0 -126
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug.rb +0 -51
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/files.rb +0 -29
- data/test/foo.rb +0 -24
- data/test/helper.rb +0 -27
- data/test/io.rb +0 -48
- data/test/isolated/shared.rb +0 -310
- data/test/isolated/test_mimic_after.rb +0 -13
- data/test/isolated/test_mimic_alone.rb +0 -12
- data/test/isolated/test_mimic_as_json.rb +0 -45
- data/test/isolated/test_mimic_before.rb +0 -13
- data/test/isolated/test_mimic_define.rb +0 -28
- data/test/isolated/test_mimic_rails_after.rb +0 -22
- data/test/isolated/test_mimic_rails_before.rb +0 -21
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/isolated/test_mimic_redefine.rb +0 -15
- data/test/mod.rb +0 -16
- data/test/perf.rb +0 -107
- data/test/perf_compat.rb +0 -128
- data/test/perf_fast.rb +0 -164
- data/test/perf_file.rb +0 -64
- data/test/perf_object.rb +0 -138
- data/test/perf_saj.rb +0 -109
- data/test/perf_scp.rb +0 -151
- data/test/perf_simple.rb +0 -287
- data/test/perf_strict.rb +0 -128
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/sample/change.rb +0 -14
- data/test/sample/dir.rb +0 -19
- data/test/sample/doc.rb +0 -36
- data/test/sample/file.rb +0 -48
- data/test/sample/group.rb +0 -16
- data/test/sample/hasprops.rb +0 -16
- data/test/sample/layer.rb +0 -12
- data/test/sample/line.rb +0 -20
- data/test/sample/oval.rb +0 -10
- data/test/sample/rect.rb +0 -10
- data/test/sample/shape.rb +0 -35
- data/test/sample/text.rb +0 -20
- data/test/sample.rb +0 -55
- data/test/sample_json.rb +0 -37
- data/test/struct.rb +0 -29
- data/test/test_compat.rb +0 -398
- data/test/test_debian.rb +0 -53
- data/test/test_fast.rb +0 -458
- data/test/test_file.rb +0 -245
- data/test/test_gc.rb +0 -49
- data/test/test_hash.rb +0 -29
- data/test/test_object.rb +0 -745
- data/test/test_saj.rb +0 -186
- data/test/test_scp.rb +0 -396
- data/test/test_serializer.rb +0 -59
- data/test/test_strict.rb +0 -254
- data/test/test_various.rb +0 -1383
- data/test/test_writer.rb +0 -308
- data/test/write_timebars.rb +0 -31
data/ext/oj/circarray.c
CHANGED
@@ -1,93 +1,64 @@
|
|
1
|
-
|
2
|
-
|
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
|
-
*/
|
1
|
+
// Copyright (c) 2012 Peter Ohler. All rights reserved.
|
2
|
+
// Licensed under the MIT License. See LICENSE file in the project root for license details.
|
30
3
|
|
31
4
|
#include "circarray.h"
|
32
5
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
6
|
+
#include "mem.h"
|
7
|
+
|
8
|
+
CircArray oj_circ_array_new(void) {
|
9
|
+
CircArray ca;
|
10
|
+
|
11
|
+
if (0 == (ca = OJ_R_ALLOC(struct _circArray))) {
|
12
|
+
rb_raise(rb_eNoMemError, "not enough memory\n");
|
39
13
|
}
|
40
14
|
ca->objs = ca->obj_array;
|
41
15
|
ca->size = sizeof(ca->obj_array) / sizeof(VALUE);
|
42
|
-
ca->cnt
|
43
|
-
|
16
|
+
ca->cnt = 0;
|
17
|
+
|
44
18
|
return ca;
|
45
19
|
}
|
46
20
|
|
47
|
-
void
|
48
|
-
oj_circ_array_free(CircArray ca) {
|
21
|
+
void oj_circ_array_free(CircArray ca) {
|
49
22
|
if (ca->objs != ca->obj_array) {
|
50
|
-
|
23
|
+
OJ_R_FREE(ca->objs);
|
51
24
|
}
|
52
|
-
|
25
|
+
OJ_R_FREE(ca);
|
53
26
|
}
|
54
27
|
|
55
|
-
void
|
56
|
-
oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
|
28
|
+
void oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
|
57
29
|
if (0 < id && 0 != ca) {
|
58
|
-
|
30
|
+
unsigned long i;
|
59
31
|
|
60
|
-
|
61
|
-
|
32
|
+
if (ca->size < id) {
|
33
|
+
unsigned long cnt = id + 512;
|
62
34
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
35
|
+
if (ca->objs == ca->obj_array) {
|
36
|
+
if (0 == (ca->objs = OJ_R_ALLOC_N(VALUE, cnt))) {
|
37
|
+
rb_raise(rb_eNoMemError, "not enough memory\n");
|
38
|
+
}
|
39
|
+
memcpy(ca->objs, ca->obj_array, sizeof(VALUE) * ca->cnt);
|
40
|
+
} else {
|
41
|
+
OJ_R_REALLOC_N(ca->objs, VALUE, cnt);
|
42
|
+
}
|
43
|
+
ca->size = cnt;
|
44
|
+
}
|
45
|
+
id--;
|
46
|
+
for (i = ca->cnt; i < id; i++) {
|
47
|
+
ca->objs[i] = Qnil;
|
48
|
+
}
|
49
|
+
ca->objs[id] = obj;
|
50
|
+
if (ca->cnt <= id) {
|
51
|
+
ca->cnt = id + 1;
|
52
|
+
}
|
81
53
|
}
|
82
54
|
}
|
83
55
|
|
84
56
|
VALUE
|
85
57
|
oj_circ_array_get(CircArray ca, unsigned long id) {
|
86
|
-
VALUE
|
58
|
+
VALUE obj = Qnil;
|
87
59
|
|
88
60
|
if (id <= ca->cnt && 0 != ca) {
|
89
|
-
|
61
|
+
obj = ca->objs[id - 1];
|
90
62
|
}
|
91
63
|
return obj;
|
92
64
|
}
|
93
|
-
|
data/ext/oj/circarray.h
CHANGED
@@ -1,48 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
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
|
-
*/
|
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.
|
30
4
|
|
31
|
-
#ifndef
|
32
|
-
#define
|
5
|
+
#ifndef OJ_CIRCARRAY_H
|
6
|
+
#define OJ_CIRCARRAY_H
|
33
7
|
|
34
8
|
#include "ruby.h"
|
35
9
|
|
36
|
-
typedef struct
|
37
|
-
VALUE
|
38
|
-
VALUE
|
39
|
-
unsigned long
|
40
|
-
unsigned long
|
41
|
-
}
|
10
|
+
typedef struct _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;
|
42
16
|
|
43
|
-
extern CircArray
|
44
|
-
extern void
|
45
|
-
extern void
|
46
|
-
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);
|
47
21
|
|
48
|
-
#endif /*
|
22
|
+
#endif /* OJ_CIRCARRAY_H */
|
data/ext/oj/code.c
ADDED
@@ -0,0 +1,214 @@
|
|
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.
|
3
|
+
|
4
|
+
#include "code.h"
|
5
|
+
|
6
|
+
#include "dump.h"
|
7
|
+
|
8
|
+
inline static VALUE resolve_classname(VALUE mod, const char *classname) {
|
9
|
+
VALUE clas = Qundef;
|
10
|
+
ID ci = rb_intern(classname);
|
11
|
+
|
12
|
+
if (rb_const_defined_at(mod, ci)) {
|
13
|
+
clas = rb_const_get_at(mod, ci);
|
14
|
+
}
|
15
|
+
return clas;
|
16
|
+
}
|
17
|
+
|
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
|
+
|
25
|
+
clas = rb_cObject;
|
26
|
+
for (s = class_name; '\0' != *n; n++) {
|
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
|
+
}
|
43
|
+
*s = '\0';
|
44
|
+
|
45
|
+
return resolve_classname(clas, class_name);
|
46
|
+
}
|
47
|
+
|
48
|
+
bool oj_code_dump(Code codes, VALUE obj, int depth, Out out) {
|
49
|
+
VALUE clas = rb_obj_class(obj);
|
50
|
+
Code c = codes;
|
51
|
+
|
52
|
+
for (; NULL != c->name; c++) {
|
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
|
+
}
|
63
|
+
}
|
64
|
+
return false;
|
65
|
+
}
|
66
|
+
|
67
|
+
VALUE
|
68
|
+
oj_code_load(Code codes, VALUE clas, VALUE args) {
|
69
|
+
Code c = codes;
|
70
|
+
|
71
|
+
for (; NULL != c->name; c++) {
|
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
|
+
}
|
84
|
+
}
|
85
|
+
return Qnil;
|
86
|
+
}
|
87
|
+
|
88
|
+
void oj_code_set_active(Code codes, VALUE clas, bool active) {
|
89
|
+
Code c = codes;
|
90
|
+
|
91
|
+
for (; NULL != c->name; c++) {
|
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
|
+
}
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
bool oj_code_has(Code codes, VALUE clas, bool encode) {
|
108
|
+
Code c = codes;
|
109
|
+
|
110
|
+
for (; NULL != c->name; c++) {
|
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
|
+
}
|
124
|
+
}
|
125
|
+
return false;
|
126
|
+
}
|
127
|
+
|
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
|
+
|
137
|
+
assure_size(out, size);
|
138
|
+
*out->cur++ = '{';
|
139
|
+
|
140
|
+
if (with_class) {
|
141
|
+
fill_indent(out, d2);
|
142
|
+
*out->cur++ = '"';
|
143
|
+
APPEND_CHARS(out->cur, out->opts->create_id, out->opts->create_id_len);
|
144
|
+
*out->cur++ = '"';
|
145
|
+
if (0 < out->opts->dump_opts.before_size) {
|
146
|
+
APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
|
147
|
+
}
|
148
|
+
*out->cur++ = ':';
|
149
|
+
if (0 < out->opts->dump_opts.after_size) {
|
150
|
+
APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
|
151
|
+
}
|
152
|
+
*out->cur++ = '"';
|
153
|
+
APPEND_CHARS(out->cur, classname, len);
|
154
|
+
*out->cur++ = '"';
|
155
|
+
no_comma = false;
|
156
|
+
}
|
157
|
+
size = d3 * out->indent + 2;
|
158
|
+
for (; NULL != attrs->name; attrs++) {
|
159
|
+
assure_size(out, size + attrs->len + sep_len + 2);
|
160
|
+
if (no_comma) {
|
161
|
+
no_comma = false;
|
162
|
+
} else {
|
163
|
+
*out->cur++ = ',';
|
164
|
+
}
|
165
|
+
fill_indent(out, d2);
|
166
|
+
*out->cur++ = '"';
|
167
|
+
APPEND_CHARS(out->cur, attrs->name, attrs->len);
|
168
|
+
*out->cur++ = '"';
|
169
|
+
if (0 < out->opts->dump_opts.before_size) {
|
170
|
+
APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
|
171
|
+
}
|
172
|
+
*out->cur++ = ':';
|
173
|
+
if (0 < out->opts->dump_opts.after_size) {
|
174
|
+
APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
|
175
|
+
}
|
176
|
+
if (Qundef == attrs->value) {
|
177
|
+
if (Qundef != attrs->time) {
|
178
|
+
switch (out->opts->time_format) {
|
179
|
+
case RubyTime: oj_dump_ruby_time(attrs->time, out); break;
|
180
|
+
case XmlTime: oj_dump_xml_time(attrs->time, out); break;
|
181
|
+
case UnixZTime: oj_dump_time(attrs->time, out, true); break;
|
182
|
+
case UnixTime:
|
183
|
+
default: oj_dump_time(attrs->time, out, false); break;
|
184
|
+
}
|
185
|
+
} else {
|
186
|
+
char buf[32];
|
187
|
+
char *b = buf + sizeof(buf) - 1;
|
188
|
+
bool neg = false;
|
189
|
+
long num = attrs->num;
|
190
|
+
size_t cnt = 0;
|
191
|
+
|
192
|
+
if (0 > num) {
|
193
|
+
neg = true;
|
194
|
+
num = -num;
|
195
|
+
}
|
196
|
+
*b-- = '\0';
|
197
|
+
if (0 < num) {
|
198
|
+
b = oj_longlong_to_string(num, neg, b);
|
199
|
+
} else {
|
200
|
+
*b = '0';
|
201
|
+
}
|
202
|
+
cnt = sizeof(buf) - (b - buf) - 1;
|
203
|
+
assure_size(out, cnt);
|
204
|
+
APPEND_CHARS(out->cur, b, cnt);
|
205
|
+
}
|
206
|
+
} else {
|
207
|
+
oj_dump_compat_val(attrs->value, d3, out, true);
|
208
|
+
}
|
209
|
+
}
|
210
|
+
assure_size(out, depth * out->indent + 2);
|
211
|
+
fill_indent(out, depth);
|
212
|
+
*out->cur++ = '}';
|
213
|
+
*out->cur = '\0';
|
214
|
+
}
|
data/ext/oj/code.h
ADDED
@@ -0,0 +1,40 @@
|
|
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.
|
3
|
+
|
4
|
+
#ifndef OJ_CODE_H
|
5
|
+
#define OJ_CODE_H
|
6
|
+
|
7
|
+
#include <ruby.h>
|
8
|
+
|
9
|
+
#include "oj.h"
|
10
|
+
|
11
|
+
typedef void (*EncodeFunc)(VALUE obj, int depth, Out out);
|
12
|
+
typedef VALUE (*DecodeFunc)(VALUE clas, VALUE args);
|
13
|
+
|
14
|
+
typedef struct _code {
|
15
|
+
const char *name;
|
16
|
+
VALUE clas;
|
17
|
+
EncodeFunc encode;
|
18
|
+
DecodeFunc decode;
|
19
|
+
bool active; // For compat mode.
|
20
|
+
} *Code;
|
21
|
+
|
22
|
+
// Used by encode functions.
|
23
|
+
typedef struct _attr {
|
24
|
+
const char *name;
|
25
|
+
int len;
|
26
|
+
VALUE value;
|
27
|
+
long num;
|
28
|
+
VALUE time;
|
29
|
+
} *Attr;
|
30
|
+
|
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);
|
35
|
+
|
36
|
+
extern void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class);
|
37
|
+
|
38
|
+
extern struct _code oj_compat_codes[];
|
39
|
+
|
40
|
+
#endif /* OJ_CODE_H */
|