oj 2.18.3 → 3.13.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1324 -0
  3. data/README.md +51 -204
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +49 -72
  6. data/ext/oj/cache.c +326 -0
  7. data/ext/oj/cache.h +21 -0
  8. data/ext/oj/cache8.c +61 -64
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +37 -68
  11. data/ext/oj/circarray.h +16 -42
  12. data/ext/oj/code.c +221 -0
  13. data/ext/oj/code.h +40 -0
  14. data/ext/oj/compat.c +231 -107
  15. data/ext/oj/custom.c +1125 -0
  16. data/ext/oj/debug.c +132 -0
  17. data/ext/oj/dump.c +935 -2513
  18. data/ext/oj/dump.h +108 -0
  19. data/ext/oj/dump_compat.c +936 -0
  20. data/ext/oj/dump_leaf.c +164 -0
  21. data/ext/oj/dump_object.c +761 -0
  22. data/ext/oj/dump_strict.c +410 -0
  23. data/ext/oj/encode.h +7 -42
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +40 -54
  26. data/ext/oj/err.h +52 -46
  27. data/ext/oj/extconf.rb +21 -30
  28. data/ext/oj/fast.c +1097 -1080
  29. data/ext/oj/intern.c +301 -0
  30. data/ext/oj/intern.h +26 -0
  31. data/ext/oj/mimic_json.c +893 -0
  32. data/ext/oj/object.c +549 -620
  33. data/ext/oj/odd.c +155 -167
  34. data/ext/oj/odd.h +37 -63
  35. data/ext/oj/oj.c +1661 -2063
  36. data/ext/oj/oj.h +341 -270
  37. data/ext/oj/parse.c +974 -737
  38. data/ext/oj/parse.h +105 -97
  39. data/ext/oj/parser.c +1526 -0
  40. data/ext/oj/parser.h +90 -0
  41. data/ext/oj/rails.c +1504 -0
  42. data/ext/oj/rails.h +18 -0
  43. data/ext/oj/reader.c +141 -163
  44. data/ext/oj/reader.h +75 -113
  45. data/ext/oj/resolve.c +45 -93
  46. data/ext/oj/resolve.h +7 -34
  47. data/ext/oj/rxclass.c +143 -0
  48. data/ext/oj/rxclass.h +26 -0
  49. data/ext/oj/saj.c +447 -511
  50. data/ext/oj/saj2.c +348 -0
  51. data/ext/oj/scp.c +91 -138
  52. data/ext/oj/sparse.c +793 -644
  53. data/ext/oj/stream_writer.c +331 -0
  54. data/ext/oj/strict.c +145 -109
  55. data/ext/oj/string_writer.c +493 -0
  56. data/ext/oj/trace.c +72 -0
  57. data/ext/oj/trace.h +28 -0
  58. data/ext/oj/usual.c +1254 -0
  59. data/ext/oj/util.c +136 -0
  60. data/ext/oj/util.h +20 -0
  61. data/ext/oj/val_stack.c +62 -70
  62. data/ext/oj/val_stack.h +95 -129
  63. data/ext/oj/validate.c +51 -0
  64. data/ext/oj/wab.c +622 -0
  65. data/lib/oj/bag.rb +1 -0
  66. data/lib/oj/easy_hash.rb +17 -8
  67. data/lib/oj/error.rb +10 -11
  68. data/lib/oj/json.rb +176 -0
  69. data/lib/oj/mimic.rb +158 -19
  70. data/lib/oj/state.rb +132 -0
  71. data/lib/oj/version.rb +2 -2
  72. data/lib/oj.rb +1 -31
  73. data/pages/Advanced.md +22 -0
  74. data/pages/Compatibility.md +25 -0
  75. data/pages/Custom.md +23 -0
  76. data/pages/Encoding.md +65 -0
  77. data/pages/JsonGem.md +94 -0
  78. data/pages/Modes.md +161 -0
  79. data/pages/Options.md +327 -0
  80. data/pages/Parser.md +309 -0
  81. data/pages/Rails.md +167 -0
  82. data/pages/Security.md +20 -0
  83. data/pages/WAB.md +13 -0
  84. data/test/activerecord/result_test.rb +32 -0
  85. data/test/activesupport4/decoding_test.rb +108 -0
  86. data/test/activesupport4/encoding_test.rb +531 -0
  87. data/test/activesupport4/test_helper.rb +41 -0
  88. data/test/activesupport5/abstract_unit.rb +45 -0
  89. data/test/activesupport5/decoding_test.rb +133 -0
  90. data/test/activesupport5/encoding_test.rb +500 -0
  91. data/test/activesupport5/encoding_test_cases.rb +98 -0
  92. data/test/activesupport5/test_helper.rb +72 -0
  93. data/test/activesupport5/time_zone_test_helpers.rb +39 -0
  94. data/test/activesupport6/abstract_unit.rb +44 -0
  95. data/test/activesupport6/decoding_test.rb +133 -0
  96. data/test/activesupport6/encoding_test.rb +507 -0
  97. data/test/activesupport6/encoding_test_cases.rb +98 -0
  98. data/test/activesupport6/test_common.rb +17 -0
  99. data/test/activesupport6/test_helper.rb +163 -0
  100. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  101. data/test/activesupport7/abstract_unit.rb +49 -0
  102. data/test/activesupport7/decoding_test.rb +125 -0
  103. data/test/activesupport7/encoding_test.rb +486 -0
  104. data/test/activesupport7/encoding_test_cases.rb +104 -0
  105. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  106. data/test/bar.rb +9 -0
  107. data/test/baz.rb +16 -0
  108. data/test/bug.rb +11 -46
  109. data/test/foo.rb +69 -16
  110. data/test/helper.rb +10 -1
  111. data/test/isolated/shared.rb +12 -8
  112. data/test/isolated/test_mimic_rails_after.rb +3 -3
  113. data/test/isolated/test_mimic_rails_before.rb +3 -3
  114. data/test/json_gem/json_addition_test.rb +216 -0
  115. data/test/json_gem/json_common_interface_test.rb +153 -0
  116. data/test/json_gem/json_encoding_test.rb +107 -0
  117. data/test/json_gem/json_ext_parser_test.rb +20 -0
  118. data/test/json_gem/json_fixtures_test.rb +35 -0
  119. data/test/json_gem/json_generator_test.rb +397 -0
  120. data/test/json_gem/json_generic_object_test.rb +90 -0
  121. data/test/json_gem/json_parser_test.rb +470 -0
  122. data/test/json_gem/json_string_matching_test.rb +42 -0
  123. data/test/json_gem/test_helper.rb +26 -0
  124. data/test/mem.rb +33 -0
  125. data/test/perf.rb +1 -1
  126. data/test/perf_compat.rb +30 -28
  127. data/test/perf_dump.rb +50 -0
  128. data/test/perf_object.rb +1 -1
  129. data/test/perf_once.rb +58 -0
  130. data/test/perf_parser.rb +189 -0
  131. data/test/perf_scp.rb +11 -10
  132. data/test/perf_strict.rb +30 -19
  133. data/test/perf_wab.rb +131 -0
  134. data/test/prec.rb +23 -0
  135. data/test/sample.rb +0 -1
  136. data/test/sample_json.rb +1 -1
  137. data/test/test_compat.rb +219 -102
  138. data/test/test_custom.rb +533 -0
  139. data/test/test_fast.rb +107 -35
  140. data/test/test_file.rb +19 -25
  141. data/test/test_generate.rb +21 -0
  142. data/test/test_hash.rb +11 -1
  143. data/test/test_integer_range.rb +72 -0
  144. data/test/test_null.rb +376 -0
  145. data/test/test_object.rb +357 -70
  146. data/test/test_parser.rb +27 -0
  147. data/test/test_parser_saj.rb +245 -0
  148. data/test/test_parser_usual.rb +217 -0
  149. data/test/test_rails.rb +35 -0
  150. data/test/test_saj.rb +1 -1
  151. data/test/test_scp.rb +39 -2
  152. data/test/test_strict.rb +186 -7
  153. data/test/test_various.rb +160 -774
  154. data/test/test_wab.rb +307 -0
  155. data/test/test_writer.rb +90 -2
  156. data/test/tests.rb +24 -0
  157. data/test/tests_mimic.rb +14 -0
  158. data/test/tests_mimic_addition.rb +7 -0
  159. data/test/zoo.rb +13 -0
  160. metadata +194 -56
  161. data/ext/oj/hash.c +0 -163
  162. data/ext/oj/hash.h +0 -46
  163. data/ext/oj/hash_test.c +0 -512
  164. data/test/activesupport_datetime_test.rb +0 -23
  165. data/test/bug2.rb +0 -10
  166. data/test/bug3.rb +0 -46
  167. data/test/bug_fast.rb +0 -32
  168. data/test/bug_load.rb +0 -24
  169. data/test/crash.rb +0 -111
  170. data/test/curl/curl_oj.rb +0 -46
  171. data/test/curl/get_oj.rb +0 -24
  172. data/test/curl/just_curl.rb +0 -31
  173. data/test/curl/just_oj.rb +0 -51
  174. data/test/example.rb +0 -11
  175. data/test/io.rb +0 -48
  176. data/test/isolated/test_mimic_rails_datetime.rb +0 -27
  177. data/test/mod.rb +0 -16
  178. data/test/rails.rb +0 -50
  179. data/test/russian.rb +0 -18
  180. data/test/struct.rb +0 -29
  181. data/test/test_serializer.rb +0 -59
  182. data/test/write_timebars.rb +0 -31
data/ext/oj/circarray.c CHANGED
@@ -1,93 +1,62 @@
1
- /* circarray.c
2
- * Copyright (c) 2012, Peter Ohler
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
- oj_circ_array_new() {
35
- CircArray ca;
36
-
37
- if (0 == (ca = ALLOC(struct _CircArray))) {
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 = 0;
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
- xfree(ca->objs);
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
- unsigned long i;
59
-
60
- if (ca->size < id) {
61
- unsigned long cnt = id + 512;
62
-
63
- if (ca->objs == ca->obj_array) {
64
- if (0 == (ca->objs = ALLOC_N(VALUE, cnt))) {
65
- rb_raise(rb_eNoMemError, "not enough memory\n");
66
- }
67
- memcpy(ca->objs, ca->obj_array, sizeof(VALUE) * ca->cnt);
68
- } else {
69
- REALLOC_N(ca->objs, VALUE, cnt);
70
- }
71
- ca->size = cnt;
72
- }
73
- id--;
74
- for (i = ca->cnt; i < id; i++) {
75
- ca->objs[i] = Qnil;
76
- }
77
- ca->objs[id] = obj;
78
- if (ca->cnt <= id) {
79
- ca->cnt = id + 1;
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 obj = Qnil;
56
+ VALUE obj = Qnil;
87
57
 
88
58
  if (id <= ca->cnt && 0 != ca) {
89
- obj = ca->objs[id - 1];
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
- /* circarray.h
2
- * Copyright (c) 2012, Peter Ohler
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
3
+ // license details.
30
4
 
31
- #ifndef __OJ_CIRCARRAY_H__
32
- #define __OJ_CIRCARRAY_H__
5
+ #ifndef OJ_CIRCARRAY_H
6
+ #define OJ_CIRCARRAY_H
33
7
 
34
8
  #include "ruby.h"
35
9
 
36
- typedef struct _CircArray {
37
- VALUE obj_array[1024];
38
- VALUE *objs;
39
- unsigned long size; // allocated size or initial array size
40
- unsigned long cnt;
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 oj_circ_array_new(void);
44
- extern void oj_circ_array_free(CircArray ca);
45
- extern void oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id);
46
- extern VALUE oj_circ_array_get(CircArray ca, unsigned long id);
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 /* __OJ_CIRCARRAY_H__ */
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 */