oj 2.18.3 → 3.13.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1324 -0
- data/README.md +51 -204
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +49 -72
- data/ext/oj/cache.c +326 -0
- data/ext/oj/cache.h +21 -0
- data/ext/oj/cache8.c +61 -64
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +37 -68
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +221 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +231 -107
- data/ext/oj/custom.c +1125 -0
- data/ext/oj/debug.c +132 -0
- data/ext/oj/dump.c +935 -2513
- data/ext/oj/dump.h +108 -0
- data/ext/oj/dump_compat.c +936 -0
- data/ext/oj/dump_leaf.c +164 -0
- data/ext/oj/dump_object.c +761 -0
- data/ext/oj/dump_strict.c +410 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +40 -54
- data/ext/oj/err.h +52 -46
- data/ext/oj/extconf.rb +21 -30
- data/ext/oj/fast.c +1097 -1080
- data/ext/oj/intern.c +301 -0
- data/ext/oj/intern.h +26 -0
- data/ext/oj/mimic_json.c +893 -0
- data/ext/oj/object.c +549 -620
- data/ext/oj/odd.c +155 -167
- data/ext/oj/odd.h +37 -63
- data/ext/oj/oj.c +1661 -2063
- data/ext/oj/oj.h +341 -270
- data/ext/oj/parse.c +974 -737
- data/ext/oj/parse.h +105 -97
- data/ext/oj/parser.c +1526 -0
- data/ext/oj/parser.h +90 -0
- data/ext/oj/rails.c +1504 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +141 -163
- data/ext/oj/reader.h +75 -113
- data/ext/oj/resolve.c +45 -93
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +143 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +447 -511
- data/ext/oj/saj2.c +348 -0
- data/ext/oj/scp.c +91 -138
- data/ext/oj/sparse.c +793 -644
- data/ext/oj/stream_writer.c +331 -0
- data/ext/oj/strict.c +145 -109
- data/ext/oj/string_writer.c +493 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +28 -0
- data/ext/oj/usual.c +1254 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +62 -70
- data/ext/oj/val_stack.h +95 -129
- data/ext/oj/validate.c +51 -0
- data/ext/oj/wab.c +622 -0
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +17 -8
- data/lib/oj/error.rb +10 -11
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +158 -19
- data/lib/oj/state.rb +132 -0
- data/lib/oj/version.rb +2 -2
- data/lib/oj.rb +1 -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/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +327 -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
- data/test/activerecord/result_test.rb +32 -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/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +133 -0
- data/test/activesupport5/encoding_test.rb +500 -0
- data/test/activesupport5/encoding_test_cases.rb +98 -0
- data/test/activesupport5/test_helper.rb +72 -0
- data/test/activesupport5/time_zone_test_helpers.rb +39 -0
- data/test/activesupport6/abstract_unit.rb +44 -0
- data/test/activesupport6/decoding_test.rb +133 -0
- data/test/activesupport6/encoding_test.rb +507 -0
- data/test/activesupport6/encoding_test_cases.rb +98 -0
- data/test/activesupport6/test_common.rb +17 -0
- data/test/activesupport6/test_helper.rb +163 -0
- data/test/activesupport6/time_zone_test_helpers.rb +39 -0
- data/test/activesupport7/abstract_unit.rb +49 -0
- data/test/activesupport7/decoding_test.rb +125 -0
- data/test/activesupport7/encoding_test.rb +486 -0
- data/test/activesupport7/encoding_test_cases.rb +104 -0
- data/test/activesupport7/time_zone_test_helpers.rb +47 -0
- data/test/bar.rb +9 -0
- data/test/baz.rb +16 -0
- data/test/bug.rb +11 -46
- data/test/foo.rb +69 -16
- data/test/helper.rb +10 -1
- data/test/isolated/shared.rb +12 -8
- data/test/isolated/test_mimic_rails_after.rb +3 -3
- data/test/isolated/test_mimic_rails_before.rb +3 -3
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +153 -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 +397 -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 +26 -0
- data/test/mem.rb +33 -0
- data/test/perf.rb +1 -1
- data/test/perf_compat.rb +30 -28
- data/test/perf_dump.rb +50 -0
- data/test/perf_object.rb +1 -1
- data/test/perf_once.rb +58 -0
- data/test/perf_parser.rb +189 -0
- data/test/perf_scp.rb +11 -10
- data/test/perf_strict.rb +30 -19
- data/test/perf_wab.rb +131 -0
- data/test/prec.rb +23 -0
- data/test/sample.rb +0 -1
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +219 -102
- data/test/test_custom.rb +533 -0
- data/test/test_fast.rb +107 -35
- data/test/test_file.rb +19 -25
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +11 -1
- data/test/test_integer_range.rb +72 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +357 -70
- data/test/test_parser.rb +27 -0
- data/test/test_parser_saj.rb +245 -0
- data/test/test_parser_usual.rb +217 -0
- data/test/test_rails.rb +35 -0
- data/test/test_saj.rb +1 -1
- data/test/test_scp.rb +39 -2
- data/test/test_strict.rb +186 -7
- data/test/test_various.rb +160 -774
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +90 -2
- data/test/tests.rb +24 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- data/test/zoo.rb +13 -0
- metadata +194 -56
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/activesupport_datetime_test.rb +0 -23
- 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/io.rb +0 -48
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/mod.rb +0 -16
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/struct.rb +0 -29
- data/test/test_serializer.rb +0 -59
- data/test/write_timebars.rb +0 -31
data/ext/oj/circarray.c
CHANGED
@@ -1,93 +1,62 @@
|
|
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
|
-
CircArray
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
rb_raise(rb_eNoMemError, "not enough memory\n");
|
6
|
+
CircArray oj_circ_array_new(void) {
|
7
|
+
CircArray ca;
|
8
|
+
|
9
|
+
if (0 == (ca = ALLOC(struct _circArray))) {
|
10
|
+
rb_raise(rb_eNoMemError, "not enough memory\n");
|
39
11
|
}
|
40
12
|
ca->objs = ca->obj_array;
|
41
13
|
ca->size = sizeof(ca->obj_array) / sizeof(VALUE);
|
42
|
-
ca->cnt
|
43
|
-
|
14
|
+
ca->cnt = 0;
|
15
|
+
|
44
16
|
return ca;
|
45
17
|
}
|
46
18
|
|
47
|
-
void
|
48
|
-
oj_circ_array_free(CircArray ca) {
|
19
|
+
void oj_circ_array_free(CircArray ca) {
|
49
20
|
if (ca->objs != ca->obj_array) {
|
50
|
-
|
21
|
+
xfree(ca->objs);
|
51
22
|
}
|
52
23
|
xfree(ca);
|
53
24
|
}
|
54
25
|
|
55
|
-
void
|
56
|
-
oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
|
26
|
+
void oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
|
57
27
|
if (0 < id && 0 != ca) {
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
28
|
+
unsigned long i;
|
29
|
+
|
30
|
+
if (ca->size < id) {
|
31
|
+
unsigned long cnt = id + 512;
|
32
|
+
|
33
|
+
if (ca->objs == ca->obj_array) {
|
34
|
+
if (0 == (ca->objs = ALLOC_N(VALUE, cnt))) {
|
35
|
+
rb_raise(rb_eNoMemError, "not enough memory\n");
|
36
|
+
}
|
37
|
+
memcpy(ca->objs, ca->obj_array, sizeof(VALUE) * ca->cnt);
|
38
|
+
} else {
|
39
|
+
REALLOC_N(ca->objs, VALUE, cnt);
|
40
|
+
}
|
41
|
+
ca->size = cnt;
|
42
|
+
}
|
43
|
+
id--;
|
44
|
+
for (i = ca->cnt; i < id; i++) {
|
45
|
+
ca->objs[i] = Qnil;
|
46
|
+
}
|
47
|
+
ca->objs[id] = obj;
|
48
|
+
if (ca->cnt <= id) {
|
49
|
+
ca->cnt = id + 1;
|
50
|
+
}
|
81
51
|
}
|
82
52
|
}
|
83
53
|
|
84
54
|
VALUE
|
85
55
|
oj_circ_array_get(CircArray ca, unsigned long id) {
|
86
|
-
VALUE
|
56
|
+
VALUE obj = Qnil;
|
87
57
|
|
88
58
|
if (id <= ca->cnt && 0 != ca) {
|
89
|
-
|
59
|
+
obj = ca->objs[id - 1];
|
90
60
|
}
|
91
61
|
return obj;
|
92
62
|
}
|
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
|
-
} *CircArray;
|
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,221 @@
|
|
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
|
+
int neg = 0;
|
189
|
+
long num = attrs->num;
|
190
|
+
size_t cnt = 0;
|
191
|
+
|
192
|
+
if (0 > num) {
|
193
|
+
neg = 1;
|
194
|
+
num = -num;
|
195
|
+
}
|
196
|
+
*b-- = '\0';
|
197
|
+
if (0 < num) {
|
198
|
+
for (; 0 < num; num /= 10, b--) {
|
199
|
+
*b = (num % 10) + '0';
|
200
|
+
}
|
201
|
+
if (neg) {
|
202
|
+
*b = '-';
|
203
|
+
} else {
|
204
|
+
b++;
|
205
|
+
}
|
206
|
+
} else {
|
207
|
+
*b = '0';
|
208
|
+
}
|
209
|
+
cnt = sizeof(buf) - (b - buf) - 1;
|
210
|
+
assure_size(out, cnt);
|
211
|
+
APPEND_CHARS(out->cur, b, cnt);
|
212
|
+
}
|
213
|
+
} else {
|
214
|
+
oj_dump_compat_val(attrs->value, d3, out, true);
|
215
|
+
}
|
216
|
+
}
|
217
|
+
assure_size(out, depth * out->indent + 2);
|
218
|
+
fill_indent(out, depth);
|
219
|
+
*out->cur++ = '}';
|
220
|
+
*out->cur = '\0';
|
221
|
+
}
|
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 */
|