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/custom.c ADDED
@@ -0,0 +1,1125 @@
1
+ // Copyright (c) 2012, 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 <stdint.h>
5
+ #include <stdio.h>
6
+
7
+ #include "code.h"
8
+ #include "dump.h"
9
+ #include "encode.h"
10
+ #include "err.h"
11
+ #include "intern.h"
12
+ #include "odd.h"
13
+ #include "oj.h"
14
+ #include "parse.h"
15
+ #include "resolve.h"
16
+ #include "trace.h"
17
+ #include "util.h"
18
+
19
+ extern void oj_set_obj_ivar(Val parent, Val kval, VALUE value);
20
+ extern VALUE oj_parse_xml_time(const char *str, int len); // from object.c
21
+
22
+ static void dump_obj_str(VALUE obj, int depth, Out out) {
23
+ struct _attr attrs[] = {
24
+ {"s", 1, Qnil},
25
+ {NULL, 0, Qnil},
26
+ };
27
+ attrs->value = rb_funcall(obj, oj_to_s_id, 0);
28
+
29
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
30
+ }
31
+
32
+ static void dump_obj_as_str(VALUE obj, int depth, Out out) {
33
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
34
+ const char * str = RSTRING_PTR(rstr);
35
+
36
+ oj_dump_cstr(str, RSTRING_LEN(rstr), 0, 0, out);
37
+ }
38
+
39
+ static void bigdecimal_dump(VALUE obj, int depth, Out out) {
40
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
41
+ const char * str = RSTRING_PTR(rstr);
42
+ int len = (int)RSTRING_LEN(rstr);
43
+
44
+ if (0 == strcasecmp("Infinity", str)) {
45
+ str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, true, &len);
46
+ oj_dump_raw(str, len, out);
47
+ } else if (0 == strcasecmp("-Infinity", str)) {
48
+ str = oj_nan_str(obj, out->opts->dump_opts.nan_dump, out->opts->mode, false, &len);
49
+ oj_dump_raw(str, len, out);
50
+ } else if (No == out->opts->bigdec_as_num) {
51
+ oj_dump_cstr(str, len, 0, 0, out);
52
+ } else {
53
+ oj_dump_raw(str, len, out);
54
+ }
55
+ }
56
+
57
+ static ID real_id = 0;
58
+ static ID imag_id = 0;
59
+
60
+ static void complex_dump(VALUE obj, int depth, Out out) {
61
+ if (NULL != out->opts->create_id) {
62
+ struct _attr attrs[] = {
63
+ {"real", 4, Qnil},
64
+ {"imag", 4, Qnil},
65
+ {NULL, 0, Qnil},
66
+ };
67
+ if (0 == real_id) {
68
+ real_id = rb_intern("real");
69
+ imag_id = rb_intern("imag");
70
+ }
71
+ attrs[0].value = rb_funcall(obj, real_id, 0);
72
+ attrs[1].value = rb_funcall(obj, imag_id, 0);
73
+
74
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
75
+ } else {
76
+ dump_obj_as_str(obj, depth, out);
77
+ }
78
+ }
79
+
80
+ static VALUE complex_load(VALUE clas, VALUE args) {
81
+ if (0 == real_id) {
82
+ real_id = rb_intern("real");
83
+ imag_id = rb_intern("imag");
84
+ }
85
+ return rb_complex_new(rb_hash_aref(args, rb_id2str(real_id)),
86
+ rb_hash_aref(args, rb_id2str(imag_id)));
87
+ }
88
+
89
+ static void time_dump(VALUE obj, int depth, Out out) {
90
+ if (Yes == out->opts->create_ok) {
91
+ struct _attr attrs[] = {
92
+ {"time", 4, Qundef, 0, Qundef},
93
+ {NULL, 0, Qnil},
94
+ };
95
+ attrs->time = obj;
96
+
97
+ oj_code_attrs(obj, attrs, depth, out, true);
98
+ } else {
99
+ switch (out->opts->time_format) {
100
+ case RubyTime: oj_dump_ruby_time(obj, out); break;
101
+ case XmlTime: oj_dump_xml_time(obj, out); break;
102
+ case UnixZTime: oj_dump_time(obj, out, true); break;
103
+ case UnixTime:
104
+ default: oj_dump_time(obj, out, false); break;
105
+ }
106
+ }
107
+ }
108
+
109
+ static void date_dump(VALUE obj, int depth, Out out) {
110
+ if (Yes == out->opts->create_ok) {
111
+ struct _attr attrs[] = {
112
+ {"s", 1, Qnil},
113
+ {NULL, 0, Qnil},
114
+ };
115
+ attrs->value = rb_funcall(obj, rb_intern("iso8601"), 0);
116
+
117
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
118
+ } else {
119
+ volatile VALUE v;
120
+ volatile VALUE ov;
121
+
122
+ switch (out->opts->time_format) {
123
+ case RubyTime:
124
+ case XmlTime:
125
+ v = rb_funcall(obj, rb_intern("iso8601"), 0);
126
+ oj_dump_cstr(RSTRING_PTR(v), (int)RSTRING_LEN(v), 0, 0, out);
127
+ break;
128
+ case UnixZTime:
129
+ v = rb_funcall(obj, rb_intern("to_time"), 0);
130
+ if (oj_date_class == rb_obj_class(obj)) {
131
+ ov = rb_funcall(v, rb_intern("utc_offset"), 0);
132
+ v = rb_funcall(v, rb_intern("utc"), 0);
133
+ v = rb_funcall(v, rb_intern("+"), 1, ov);
134
+ oj_dump_time(v, out, false);
135
+ } else {
136
+ oj_dump_time(v, out, true);
137
+ }
138
+ break;
139
+ case UnixTime:
140
+ default:
141
+ v = rb_funcall(obj, rb_intern("to_time"), 0);
142
+ if (oj_date_class == rb_obj_class(obj)) {
143
+ ov = rb_funcall(v, rb_intern("utc_offset"), 0);
144
+ v = rb_funcall(v, rb_intern("utc"), 0);
145
+ v = rb_funcall(v, rb_intern("+"), 1, ov);
146
+ }
147
+ oj_dump_time(v, out, false);
148
+ break;
149
+ }
150
+ }
151
+ }
152
+
153
+ static VALUE date_load(VALUE clas, VALUE args) {
154
+ volatile VALUE v;
155
+
156
+ if (Qnil != (v = rb_hash_aref(args, rb_str_new2("s")))) {
157
+ return rb_funcall(oj_date_class, rb_intern("parse"), 1, v);
158
+ }
159
+ return Qnil;
160
+ }
161
+
162
+ static VALUE datetime_load(VALUE clas, VALUE args) {
163
+ volatile VALUE v;
164
+
165
+ if (Qnil != (v = rb_hash_aref(args, rb_str_new2("s")))) {
166
+ return rb_funcall(oj_datetime_class, rb_intern("parse"), 1, v);
167
+ }
168
+ return Qnil;
169
+ }
170
+
171
+ static ID table_id = 0;
172
+
173
+ static void openstruct_dump(VALUE obj, int depth, Out out) {
174
+ struct _attr attrs[] = {
175
+ {"table", 5, Qnil},
176
+ {NULL, 0, Qnil},
177
+ };
178
+ if (0 == table_id) {
179
+ table_id = rb_intern("table");
180
+ }
181
+ attrs->value = rb_funcall(obj, table_id, 0);
182
+
183
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
184
+ }
185
+
186
+ static VALUE openstruct_load(VALUE clas, VALUE args) {
187
+ if (0 == table_id) {
188
+ table_id = rb_intern("table");
189
+ }
190
+ return rb_funcall(clas, oj_new_id, 1, rb_hash_aref(args, rb_id2str(table_id)));
191
+ }
192
+
193
+ static void range_dump(VALUE obj, int depth, Out out) {
194
+ if (NULL != out->opts->create_id) {
195
+ struct _attr attrs[] = {
196
+ {"begin", 5, Qnil},
197
+ {"end", 3, Qnil},
198
+ {"exclude", 7, Qnil},
199
+ {NULL, 0, Qnil},
200
+ };
201
+ attrs[0].value = rb_funcall(obj, oj_begin_id, 0);
202
+ attrs[1].value = rb_funcall(obj, oj_end_id, 0);
203
+ attrs[2].value = rb_funcall(obj, oj_exclude_end_id, 0);
204
+
205
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
206
+ } else {
207
+ dump_obj_as_str(obj, depth, out);
208
+ }
209
+ }
210
+
211
+ static VALUE range_load(VALUE clas, VALUE args) {
212
+ VALUE nargs[3];
213
+
214
+ nargs[0] = rb_hash_aref(args, rb_id2str(oj_begin_id));
215
+ nargs[1] = rb_hash_aref(args, rb_id2str(oj_end_id));
216
+ nargs[2] = rb_hash_aref(args, rb_id2str(oj_exclude_end_id));
217
+
218
+ return rb_class_new_instance(3, nargs, rb_cRange);
219
+ }
220
+
221
+ static ID numerator_id = 0;
222
+ static ID denominator_id = 0;
223
+
224
+ static void rational_dump(VALUE obj, int depth, Out out) {
225
+ if (NULL != out->opts->create_id) {
226
+ struct _attr attrs[] = {
227
+ {"numerator", 9, Qnil},
228
+ {"denominator", 11, Qnil},
229
+ {NULL, 0, Qnil},
230
+ };
231
+ if (0 == numerator_id) {
232
+ numerator_id = rb_intern("numerator");
233
+ denominator_id = rb_intern("denominator");
234
+ }
235
+ attrs[0].value = rb_funcall(obj, numerator_id, 0);
236
+ attrs[1].value = rb_funcall(obj, denominator_id, 0);
237
+
238
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
239
+ } else {
240
+ dump_obj_as_str(obj, depth, out);
241
+ }
242
+ }
243
+
244
+ static VALUE rational_load(VALUE clas, VALUE args) {
245
+ if (0 == numerator_id) {
246
+ numerator_id = rb_intern("numerator");
247
+ denominator_id = rb_intern("denominator");
248
+ }
249
+ return rb_rational_new(rb_hash_aref(args, rb_id2str(numerator_id)),
250
+ rb_hash_aref(args, rb_id2str(denominator_id)));
251
+ }
252
+
253
+ static VALUE regexp_load(VALUE clas, VALUE args) {
254
+ volatile VALUE v;
255
+
256
+ if (Qnil != (v = rb_hash_aref(args, rb_str_new2("s")))) {
257
+ return rb_funcall(rb_cRegexp, oj_new_id, 1, v);
258
+ }
259
+ return Qnil;
260
+ }
261
+
262
+ static VALUE time_load(VALUE clas, VALUE args) {
263
+ // Value should have already been replaced in one of the hash_set_xxx
264
+ // functions.
265
+ return args;
266
+ }
267
+
268
+ static struct _code codes[] = {
269
+ {"BigDecimal", Qnil, bigdecimal_dump, NULL, true},
270
+ {"Complex", Qnil, complex_dump, complex_load, true},
271
+ {"Date", Qnil, date_dump, date_load, true},
272
+ {"DateTime", Qnil, date_dump, datetime_load, true},
273
+ {"OpenStruct", Qnil, openstruct_dump, openstruct_load, true},
274
+ {"Range", Qnil, range_dump, range_load, true},
275
+ {"Rational", Qnil, rational_dump, rational_load, true},
276
+ {"Regexp", Qnil, dump_obj_str, regexp_load, true},
277
+ {"Time", Qnil, time_dump, time_load, true},
278
+ {NULL, Qundef, NULL, NULL, false},
279
+ };
280
+
281
+ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
282
+ Out out = (Out)ov;
283
+ int depth = out->depth;
284
+
285
+ if (dump_ignore(out->opts, value)) {
286
+ return ST_CONTINUE;
287
+ }
288
+ if (out->omit_nil && Qnil == value) {
289
+ return ST_CONTINUE;
290
+ }
291
+ if (!out->opts->dump_opts.use) {
292
+ assure_size(out, depth * out->indent + 1);
293
+ fill_indent(out, depth);
294
+ } else {
295
+ assure_size(out,
296
+ depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1);
297
+ if (0 < out->opts->dump_opts.hash_size) {
298
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
299
+ }
300
+ if (0 < out->opts->dump_opts.indent_size) {
301
+ int i;
302
+
303
+ for (i = depth; 0 < i; i--) {
304
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
305
+ }
306
+ }
307
+ }
308
+ switch (rb_type(key)) {
309
+ case T_STRING: oj_dump_str(key, 0, out, false); break;
310
+ case T_SYMBOL: oj_dump_sym(key, 0, out, false); break;
311
+ default: oj_dump_str(rb_funcall(key, oj_to_s_id, 0), 0, out, false); break;
312
+ }
313
+ if (!out->opts->dump_opts.use) {
314
+ *out->cur++ = ':';
315
+ } else {
316
+ assure_size(out, out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2);
317
+ if (0 < out->opts->dump_opts.before_size) {
318
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
319
+ }
320
+ *out->cur++ = ':';
321
+ if (0 < out->opts->dump_opts.after_size) {
322
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
323
+ }
324
+ }
325
+ oj_dump_custom_val(value, depth, out, true);
326
+ out->depth = depth;
327
+ *out->cur++ = ',';
328
+
329
+ return ST_CONTINUE;
330
+ }
331
+
332
+ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
333
+ int cnt;
334
+ long id = oj_check_circular(obj, out);
335
+
336
+ if (0 > id) {
337
+ oj_dump_nil(Qnil, depth, out, false);
338
+ return;
339
+ }
340
+ cnt = (int)RHASH_SIZE(obj);
341
+ assure_size(out, 2);
342
+ if (0 == cnt) {
343
+ APPEND_CHARS(out->cur, "{}", 2);
344
+ } else {
345
+ *out->cur++ = '{';
346
+ out->depth = depth + 1;
347
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
348
+ if (',' == *(out->cur - 1)) {
349
+ out->cur--; // backup to overwrite last comma
350
+ }
351
+ if (!out->opts->dump_opts.use) {
352
+ assure_size(out, depth * out->indent + 2);
353
+ fill_indent(out, depth);
354
+ } else {
355
+ assure_size(
356
+ out,
357
+ depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1);
358
+ if (0 < out->opts->dump_opts.hash_size) {
359
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
360
+ }
361
+ if (0 < out->opts->dump_opts.indent_size) {
362
+ int i;
363
+
364
+ for (i = depth; 0 < i; i--) {
365
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
366
+ }
367
+ }
368
+ }
369
+ *out->cur++ = '}';
370
+ }
371
+ *out->cur = '\0';
372
+ }
373
+
374
+ static void dump_odd(VALUE obj, Odd odd, VALUE clas, int depth, Out out) {
375
+ ID * idp;
376
+ AttrGetFunc * fp;
377
+ volatile VALUE v;
378
+ const char * name;
379
+ size_t size;
380
+ int d2 = depth + 1;
381
+
382
+ assure_size(out, 2);
383
+ *out->cur++ = '{';
384
+ if (NULL != out->opts->create_id && Yes == out->opts->create_ok) {
385
+ const char *classname = rb_class2name(clas);
386
+ int clen = (int)strlen(classname);
387
+ size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
388
+
389
+ size = d2 * out->indent + 10 + clen + out->opts->create_id_len + sep_len;
390
+ assure_size(out, size);
391
+ fill_indent(out, d2);
392
+ *out->cur++ = '"';
393
+ APPEND_CHARS(out->cur, out->opts->create_id, out->opts->create_id_len);
394
+ *out->cur++ = '"';
395
+ if (0 < out->opts->dump_opts.before_size) {
396
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
397
+ }
398
+ *out->cur++ = ':';
399
+ if (0 < out->opts->dump_opts.after_size) {
400
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
401
+ }
402
+ *out->cur++ = '"';
403
+ APPEND_CHARS(out->cur, classname, clen);
404
+ APPEND_CHARS(out->cur, "\",", 2);
405
+ }
406
+ if (odd->raw) {
407
+ v = rb_funcall(obj, *odd->attrs, 0);
408
+ if (Qundef == v || T_STRING != rb_type(v)) {
409
+ rb_raise(rb_eEncodingError, "Invalid type for raw JSON.\n");
410
+ } else {
411
+ const char *s = RSTRING_PTR(v);
412
+ int len = (int)RSTRING_LEN(v);
413
+ const char *name = rb_id2name(*odd->attrs);
414
+ size_t nlen = strlen(name);
415
+
416
+ size = len + d2 * out->indent + nlen + 10;
417
+ assure_size(out, size);
418
+ fill_indent(out, d2);
419
+ *out->cur++ = '"';
420
+ APPEND_CHARS(out->cur, name, nlen);
421
+ APPEND_CHARS(out->cur, "\":", 2);
422
+ APPEND_CHARS(out->cur, s, len);
423
+ *out->cur = '\0';
424
+ }
425
+ } else {
426
+ size = d2 * out->indent + 1;
427
+ for (idp = odd->attrs, fp = odd->attrFuncs; 0 != *idp; idp++, fp++) {
428
+ size_t nlen;
429
+
430
+ assure_size(out, size);
431
+ name = rb_id2name(*idp);
432
+ nlen = strlen(name);
433
+ if (0 != *fp) {
434
+ v = (*fp)(obj);
435
+ } else if (0 == strchr(name, '.')) {
436
+ v = rb_funcall(obj, *idp, 0);
437
+ } else {
438
+ char nbuf[256];
439
+ char *n2 = nbuf;
440
+ char *n;
441
+ char *end;
442
+ ID i;
443
+
444
+ if (sizeof(nbuf) <= nlen) {
445
+ if (NULL == (n2 = strdup(name))) {
446
+ rb_raise(rb_eNoMemError, "for attribute name.");
447
+ }
448
+ } else {
449
+ strcpy(n2, name);
450
+ }
451
+ n = n2;
452
+ v = obj;
453
+ while (0 != (end = strchr(n, '.'))) {
454
+ *end = '\0';
455
+ i = rb_intern(n);
456
+ v = rb_funcall(v, i, 0);
457
+ n = end + 1;
458
+ }
459
+ i = rb_intern(n);
460
+ v = rb_funcall(v, i, 0);
461
+ if (nbuf != n2) {
462
+ free(n2);
463
+ }
464
+ }
465
+ fill_indent(out, d2);
466
+ oj_dump_cstr(name, nlen, 0, 0, out);
467
+ *out->cur++ = ':';
468
+ oj_dump_custom_val(v, d2, out, true);
469
+ assure_size(out, 2);
470
+ *out->cur++ = ',';
471
+ }
472
+ out->cur--;
473
+ }
474
+ *out->cur++ = '}';
475
+ *out->cur = '\0';
476
+ }
477
+
478
+ // Return class if still needs dumping.
479
+ static VALUE dump_common(VALUE obj, int depth, Out out) {
480
+ if (Yes == out->opts->raw_json && rb_respond_to(obj, oj_raw_json_id)) {
481
+ oj_dump_raw_json(obj, depth, out);
482
+ } else if (Yes == out->opts->to_json && rb_respond_to(obj, oj_to_json_id)) {
483
+ volatile VALUE rs;
484
+ const char * s;
485
+ int len;
486
+
487
+ if (Yes == out->opts->trace) {
488
+ oj_trace("to_json", obj, __FILE__, __LINE__, depth + 1, TraceRubyIn);
489
+ }
490
+ if (0 == rb_obj_method_arity(obj, oj_to_json_id)) {
491
+ rs = rb_funcall(obj, oj_to_json_id, 0);
492
+ } else {
493
+ rs = rb_funcall2(obj, oj_to_json_id, out->argc, out->argv);
494
+ }
495
+ if (Yes == out->opts->trace) {
496
+ oj_trace("to_json", obj, __FILE__, __LINE__, depth + 1, TraceRubyOut);
497
+ }
498
+ s = RSTRING_PTR(rs);
499
+ len = (int)RSTRING_LEN(rs);
500
+
501
+ assure_size(out, len + 1);
502
+ APPEND_CHARS(out->cur, s, len);
503
+ *out->cur = '\0';
504
+ } else if (Yes == out->opts->as_json && rb_respond_to(obj, oj_as_json_id)) {
505
+ volatile VALUE aj;
506
+
507
+ if (Yes == out->opts->trace) {
508
+ oj_trace("as_json", obj, __FILE__, __LINE__, depth + 1, TraceRubyIn);
509
+ }
510
+ // Some classes elect to not take an options argument so check the arity
511
+ // of as_json.
512
+ if (0 == rb_obj_method_arity(obj, oj_as_json_id)) {
513
+ aj = rb_funcall(obj, oj_as_json_id, 0);
514
+ } else {
515
+ aj = rb_funcall2(obj, oj_as_json_id, out->argc, out->argv);
516
+ }
517
+ if (Yes == out->opts->trace) {
518
+ oj_trace("as_json", obj, __FILE__, __LINE__, depth + 1, TraceRubyOut);
519
+ }
520
+ // Catch the obvious brain damaged recursive dumping.
521
+ if (aj == obj) {
522
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
523
+
524
+ oj_dump_cstr(RSTRING_PTR(rstr),
525
+ (int)RSTRING_LEN(rstr),
526
+ false,
527
+ false,
528
+ out);
529
+ } else {
530
+ oj_dump_custom_val(aj, depth, out, true);
531
+ }
532
+ } else if (Yes == out->opts->to_hash && rb_respond_to(obj, oj_to_hash_id)) {
533
+ volatile VALUE h = rb_funcall(obj, oj_to_hash_id, 0);
534
+
535
+ if (T_HASH != rb_type(h)) {
536
+ // It seems that ActiveRecord implemented to_hash so that it returns
537
+ // an Array and not a Hash. To get around that any value returned
538
+ // will be dumped.
539
+
540
+ // rb_raise(rb_eTypeError, "%s.to_hash() did not return a Hash.\n",
541
+ // rb_class2name(rb_obj_class(obj)));
542
+ oj_dump_custom_val(h, depth, out, false);
543
+ } else {
544
+ dump_hash(h, depth, out, true);
545
+ }
546
+ } else if (!oj_code_dump(codes, obj, depth, out)) {
547
+ VALUE clas = rb_obj_class(obj);
548
+ Odd odd = oj_get_odd(clas);
549
+
550
+ if (NULL == odd) {
551
+ return clas;
552
+ }
553
+ dump_odd(obj, odd, clas, depth + 1, out);
554
+ }
555
+ return Qnil;
556
+ }
557
+
558
+ static int dump_attr_cb(ID key, VALUE value, VALUE ov) {
559
+ Out out = (Out)ov;
560
+ int depth = out->depth;
561
+ size_t size;
562
+ const char *attr;
563
+
564
+ if (dump_ignore(out->opts, value)) {
565
+ return ST_CONTINUE;
566
+ }
567
+ if (out->omit_nil && Qnil == value) {
568
+ return ST_CONTINUE;
569
+ }
570
+ size = depth * out->indent + 1;
571
+ attr = rb_id2name(key);
572
+ // Some exceptions such as NoMethodError have an invisible attribute where
573
+ // the key name is NULL. Not an empty string but NULL.
574
+ if (NULL == attr) {
575
+ attr = "";
576
+ } else if (Yes == out->opts->ignore_under && '@' == *attr && '_' == attr[1]) {
577
+ return ST_CONTINUE;
578
+ }
579
+ if (0 == strcmp("bt", attr) || 0 == strcmp("mesg", attr)) {
580
+ return ST_CONTINUE;
581
+ }
582
+ assure_size(out, size);
583
+ fill_indent(out, depth);
584
+ if ('@' == *attr) {
585
+ attr++;
586
+ oj_dump_cstr(attr, strlen(attr), 0, 0, out);
587
+ } else {
588
+ char buf[32];
589
+
590
+ *buf = '~';
591
+ strncpy(buf + 1, attr, sizeof(buf) - 2);
592
+ buf[sizeof(buf) - 1] = '\0';
593
+ oj_dump_cstr(buf, strlen(buf), 0, 0, out);
594
+ }
595
+ *out->cur++ = ':';
596
+ oj_dump_custom_val(value, depth, out, true);
597
+ out->depth = depth;
598
+ *out->cur++ = ',';
599
+
600
+ return ST_CONTINUE;
601
+ }
602
+
603
+ static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out) {
604
+ size_t size = 0;
605
+ int d2 = depth + 1;
606
+ int cnt;
607
+ bool class_written = false;
608
+
609
+ assure_size(out, 2);
610
+ *out->cur++ = '{';
611
+ if (Qundef != clas && NULL != out->opts->create_id && Yes == out->opts->create_ok) {
612
+ size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
613
+ const char *classname = rb_obj_classname(obj);
614
+ size_t len = strlen(classname);
615
+
616
+ size = d2 * out->indent + 10 + len + out->opts->create_id_len + sep_len;
617
+ assure_size(out, size);
618
+ fill_indent(out, d2);
619
+ *out->cur++ = '"';
620
+ APPEND_CHARS(out->cur, out->opts->create_id, out->opts->create_id_len);
621
+ *out->cur++ = '"';
622
+ if (0 < out->opts->dump_opts.before_size) {
623
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
624
+ }
625
+ *out->cur++ = ':';
626
+ if (0 < out->opts->dump_opts.after_size) {
627
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
628
+ }
629
+ *out->cur++ = '"';
630
+ APPEND_CHARS(out->cur, classname, len);
631
+ *out->cur++ = '"';
632
+ class_written = true;
633
+ }
634
+ cnt = (int)rb_ivar_count(obj);
635
+ if (class_written) {
636
+ *out->cur++ = ',';
637
+ }
638
+ if (0 == cnt && Qundef == clas) {
639
+ // Might be something special like an Enumerable.
640
+ if (Qtrue == rb_obj_is_kind_of(obj, oj_enumerable_class)) {
641
+ out->cur--;
642
+ oj_dump_custom_val(rb_funcall(obj, rb_intern("entries"), 0), depth, out, false);
643
+ return;
644
+ }
645
+ }
646
+ out->depth = depth + 1;
647
+ rb_ivar_foreach(obj, dump_attr_cb, (VALUE)out);
648
+ if (',' == *(out->cur - 1)) {
649
+ out->cur--; // backup to overwrite last comma
650
+ }
651
+ if (rb_obj_is_kind_of(obj, rb_eException)) {
652
+ volatile VALUE rv;
653
+
654
+ if (',' != *(out->cur - 1)) {
655
+ *out->cur++ = ',';
656
+ }
657
+ // message
658
+ assure_size(out, 2);
659
+ fill_indent(out, d2);
660
+ oj_dump_cstr("~mesg", 5, 0, 0, out);
661
+ *out->cur++ = ':';
662
+ rv = rb_funcall2(obj, rb_intern("message"), 0, 0);
663
+ oj_dump_custom_val(rv, d2, out, true);
664
+ assure_size(out, size + 2);
665
+ *out->cur++ = ',';
666
+ // backtrace
667
+ fill_indent(out, d2);
668
+ oj_dump_cstr("~bt", 3, 0, 0, out);
669
+ *out->cur++ = ':';
670
+ rv = rb_funcall2(obj, rb_intern("backtrace"), 0, 0);
671
+ oj_dump_custom_val(rv, d2, out, true);
672
+ assure_size(out, 2);
673
+ }
674
+ out->depth = depth;
675
+
676
+ fill_indent(out, depth);
677
+ *out->cur++ = '}';
678
+ *out->cur = '\0';
679
+ }
680
+
681
+ static void dump_obj(VALUE obj, int depth, Out out, bool as_ok) {
682
+ long id = oj_check_circular(obj, out);
683
+ VALUE clas;
684
+
685
+ if (0 > id) {
686
+ oj_dump_nil(Qnil, depth, out, false);
687
+ } else if (Qnil != (clas = dump_common(obj, depth, out))) {
688
+ dump_obj_attrs(obj, clas, 0, depth, out);
689
+ }
690
+ *out->cur = '\0';
691
+ }
692
+
693
+ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
694
+ size_t size;
695
+ int i, cnt;
696
+ int d2 = depth + 1;
697
+ long id = oj_check_circular(a, out);
698
+
699
+ if (0 > id) {
700
+ oj_dump_nil(Qnil, depth, out, false);
701
+ return;
702
+ }
703
+ cnt = (int)RARRAY_LEN(a);
704
+ *out->cur++ = '[';
705
+ assure_size(out, 2);
706
+ if (0 == cnt) {
707
+ *out->cur++ = ']';
708
+ } else {
709
+ if (out->opts->dump_opts.use) {
710
+ size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
711
+ } else {
712
+ size = d2 * out->indent + 2;
713
+ }
714
+ assure_size(out, size * cnt);
715
+ cnt--;
716
+ for (i = 0; i <= cnt; i++) {
717
+ if (out->opts->dump_opts.use) {
718
+ if (0 < out->opts->dump_opts.array_size) {
719
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
720
+ }
721
+ if (0 < out->opts->dump_opts.indent_size) {
722
+ int i;
723
+ for (i = d2; 0 < i; i--) {
724
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
725
+ }
726
+ }
727
+ } else {
728
+ fill_indent(out, d2);
729
+ }
730
+ oj_dump_custom_val(RARRAY_AREF(a, i), d2, out, true);
731
+ if (i < cnt) {
732
+ *out->cur++ = ',';
733
+ }
734
+ }
735
+ size = depth * out->indent + 1;
736
+ assure_size(out, size);
737
+ if (out->opts->dump_opts.use) {
738
+ if (0 < out->opts->dump_opts.array_size) {
739
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
740
+ }
741
+ if (0 < out->opts->dump_opts.indent_size) {
742
+ int i;
743
+
744
+ for (i = depth; 0 < i; i--) {
745
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
746
+ }
747
+ }
748
+ } else {
749
+ fill_indent(out, depth);
750
+ }
751
+ *out->cur++ = ']';
752
+ }
753
+ *out->cur = '\0';
754
+ }
755
+
756
+ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
757
+ long id = oj_check_circular(obj, out);
758
+ VALUE clas;
759
+
760
+ if (0 > id) {
761
+ oj_dump_nil(Qnil, depth, out, false);
762
+ } else if (Qnil != (clas = dump_common(obj, depth, out))) {
763
+ VALUE ma = Qnil;
764
+ VALUE v;
765
+ char num_id[32];
766
+ int i;
767
+ int d2 = depth + 1;
768
+ int d3 = d2 + 1;
769
+ size_t size = d2 * out->indent + d3 * out->indent + 3;
770
+ const char *name;
771
+ int cnt;
772
+ size_t len;
773
+
774
+ assure_size(out, size);
775
+ if (clas == rb_cRange) {
776
+ *out->cur++ = '"';
777
+ oj_dump_custom_val(rb_funcall(obj, oj_begin_id, 0), d3, out, false);
778
+ assure_size(out, 3);
779
+ APPEND_CHARS(out->cur, "..", 2);
780
+ if (Qtrue == rb_funcall(obj, oj_exclude_end_id, 0)) {
781
+ *out->cur++ = '.';
782
+ }
783
+ oj_dump_custom_val(rb_funcall(obj, oj_end_id, 0), d3, out, false);
784
+ *out->cur++ = '"';
785
+
786
+ return;
787
+ }
788
+ *out->cur++ = '{';
789
+ fill_indent(out, d2);
790
+ size = d3 * out->indent + 2;
791
+ ma = rb_struct_s_members(clas);
792
+
793
+ #ifdef RSTRUCT_LEN
794
+ #if RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
795
+ cnt = (int)NUM2LONG(RSTRUCT_LEN(obj));
796
+ #else // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
797
+ cnt = (int)RSTRUCT_LEN(obj);
798
+ #endif // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
799
+ #else
800
+ // This is a bit risky as a struct in C ruby is not the same as a Struct
801
+ // class in interpreted Ruby so length() may not be defined.
802
+ cnt = FIX2INT(rb_funcall2(obj, oj_length_id, 0, 0));
803
+ #endif
804
+ for (i = 0; i < cnt; i++) {
805
+ #ifdef RSTRUCT_LEN
806
+ v = RSTRUCT_GET(obj, i);
807
+ #else
808
+ v = rb_struct_aref(obj, INT2FIX(i));
809
+ #endif
810
+ if (ma != Qnil) {
811
+ volatile VALUE s = rb_sym2str(RARRAY_AREF(ma, i));
812
+
813
+ name = RSTRING_PTR(s);
814
+ len = (int)RSTRING_LEN(s);
815
+ } else {
816
+ len = snprintf(num_id, sizeof(num_id), "%d", i);
817
+ name = num_id;
818
+ }
819
+ assure_size(out, size + len + 3);
820
+ fill_indent(out, d3);
821
+ *out->cur++ = '"';
822
+ APPEND_CHARS(out->cur, name, len);
823
+ APPEND_CHARS(out->cur, "\":", 2);
824
+ oj_dump_custom_val(v, d3, out, true);
825
+ *out->cur++ = ',';
826
+ }
827
+ out->cur--;
828
+ *out->cur++ = '}';
829
+ *out->cur = '\0';
830
+ }
831
+ }
832
+
833
+ static void dump_data(VALUE obj, int depth, Out out, bool as_ok) {
834
+ long id = oj_check_circular(obj, out);
835
+ VALUE clas;
836
+
837
+ if (0 > id) {
838
+ oj_dump_nil(Qnil, depth, out, false);
839
+ } else if (Qnil != (clas = dump_common(obj, depth, out))) {
840
+ dump_obj_attrs(obj, clas, id, depth, out);
841
+ }
842
+ }
843
+
844
+ static void dump_regexp(VALUE obj, int depth, Out out, bool as_ok) {
845
+ if (NULL != out->opts->create_id) {
846
+ dump_obj_str(obj, depth, out);
847
+ } else {
848
+ dump_obj_as_str(obj, depth, out);
849
+ }
850
+ }
851
+
852
+ static void dump_complex(VALUE obj, int depth, Out out, bool as_ok) {
853
+ complex_dump(obj, depth, out);
854
+ }
855
+
856
+ static void dump_rational(VALUE obj, int depth, Out out, bool as_ok) {
857
+ rational_dump(obj, depth, out);
858
+ }
859
+
860
+ static DumpFunc custom_funcs[] = {
861
+ NULL, // RUBY_T_NONE = 0x00,
862
+ dump_obj, // RUBY_T_OBJECT = 0x01,
863
+ oj_dump_class, // RUBY_T_CLASS = 0x02,
864
+ oj_dump_class, // RUBY_T_MODULE = 0x03,
865
+ oj_dump_float, // RUBY_T_FLOAT = 0x04,
866
+ oj_dump_str, // RUBY_T_STRING = 0x05,
867
+ dump_regexp, // RUBY_T_REGEXP = 0x06,
868
+ dump_array, // RUBY_T_ARRAY = 0x07,
869
+ dump_hash, // RUBY_T_HASH = 0x08,
870
+ dump_struct, // RUBY_T_STRUCT = 0x09,
871
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
872
+ NULL, // RUBY_T_FILE = 0x0b,
873
+ dump_data, // RUBY_T_DATA = 0x0c,
874
+ NULL, // RUBY_T_MATCH = 0x0d,
875
+ dump_complex, // RUBY_T_COMPLEX = 0x0e,
876
+ dump_rational, // RUBY_T_RATIONAL = 0x0f,
877
+ NULL, // 0x10
878
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
879
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
880
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
881
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
882
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
883
+ };
884
+
885
+ void oj_dump_custom_val(VALUE obj, int depth, Out out, bool as_ok) {
886
+ int type = rb_type(obj);
887
+
888
+ if (Yes == out->opts->trace) {
889
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceIn);
890
+ }
891
+ if (MAX_DEPTH < depth) {
892
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
893
+ }
894
+ if (0 < type && type <= RUBY_T_FIXNUM) {
895
+ DumpFunc f = custom_funcs[type];
896
+
897
+ if (NULL != f) {
898
+ f(obj, depth, out, true);
899
+ if (Yes == out->opts->trace) {
900
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
901
+ }
902
+ return;
903
+ }
904
+ }
905
+ oj_dump_nil(Qnil, depth, out, false);
906
+ if (Yes == out->opts->trace) {
907
+ oj_trace("dump", Qnil, __FILE__, __LINE__, depth, TraceOut);
908
+ }
909
+ }
910
+
911
+ ///// load functions /////
912
+
913
+ static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
914
+ const char * key = kval->key;
915
+ int klen = kval->klen;
916
+ Val parent = stack_peek(&pi->stack);
917
+ volatile VALUE rkey = kval->key_val;
918
+
919
+ if (Qundef == rkey && Yes == pi->options.create_ok && NULL != pi->options.create_id &&
920
+ *pi->options.create_id == *key && (int)pi->options.create_id_len == klen &&
921
+ 0 == strncmp(pi->options.create_id, key, klen)) {
922
+ parent->clas = oj_name2class(pi, str, len, false, rb_eArgError);
923
+ if (2 == klen && '^' == *key && 'o' == key[1]) {
924
+ if (Qundef != parent->clas) {
925
+ if (!oj_code_has(codes, parent->clas, false)) {
926
+ parent->val = rb_obj_alloc(parent->clas);
927
+ }
928
+ }
929
+ }
930
+ } else {
931
+ volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
932
+ //volatile VALUE rstr = rb_utf8_str_new(str, len);
933
+
934
+ if (Qundef == rkey) {
935
+ if (Yes == pi->options.sym_key) {
936
+ rkey = ID2SYM(rb_intern3(key, klen, oj_utf8_encoding));
937
+ } else {
938
+ rkey = rb_utf8_str_new(key, klen);
939
+ }
940
+ }
941
+ if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
942
+ VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
943
+
944
+ if (Qnil != clas) {
945
+ rstr = rb_funcall(clas, oj_json_create_id, 1, rstr);
946
+ }
947
+ }
948
+ switch (rb_type(parent->val)) {
949
+ case T_OBJECT: oj_set_obj_ivar(parent, kval, rstr); break;
950
+ case T_HASH:
951
+ if (4 == parent->klen && NULL != parent->key && rb_cTime == parent->clas &&
952
+ 0 == strncmp("time", parent->key, 4)) {
953
+ if (Qnil == (parent->val = oj_parse_xml_time(str, (int)len))) {
954
+ parent->val = rb_funcall(rb_cTime, rb_intern("parse"), 1, rb_str_new(str, len));
955
+ }
956
+ } else {
957
+ rb_hash_aset(parent->val, rkey, rstr);
958
+ }
959
+ break;
960
+ default: break;
961
+ }
962
+ if (Yes == pi->options.trace) {
963
+ oj_trace_parse_call("set_string", pi, __FILE__, __LINE__, rstr);
964
+ }
965
+ }
966
+ }
967
+
968
+ static void end_hash(struct _parseInfo *pi) {
969
+ Val parent = stack_peek(&pi->stack);
970
+
971
+ if (Qundef != parent->clas && parent->clas != rb_obj_class(parent->val)) {
972
+ volatile VALUE obj = oj_code_load(codes, parent->clas, parent->val);
973
+
974
+ if (Qnil != obj) {
975
+ parent->val = obj;
976
+ } else {
977
+ parent->val = rb_funcall(parent->clas, oj_json_create_id, 1, parent->val);
978
+ }
979
+ parent->clas = Qundef;
980
+ }
981
+ if (Yes == pi->options.trace) {
982
+ oj_trace_parse_hash_end(pi, __FILE__, __LINE__);
983
+ }
984
+ }
985
+
986
+ static void hash_set_num(struct _parseInfo *pi, Val kval, NumInfo ni) {
987
+ Val parent = stack_peek(&pi->stack);
988
+ volatile VALUE rval = oj_num_as_value(ni);
989
+
990
+ switch (rb_type(parent->val)) {
991
+ case T_OBJECT: oj_set_obj_ivar(parent, kval, rval); break;
992
+ case T_HASH:
993
+ if (4 == parent->klen && NULL != parent->key && rb_cTime == parent->clas && 0 != ni->div &&
994
+ 0 == strncmp("time", parent->key, 4)) {
995
+ int64_t nsec = ni->num * 1000000000LL / ni->div;
996
+
997
+ if (ni->neg) {
998
+ ni->i = -ni->i;
999
+ if (0 < nsec) {
1000
+ ni->i--;
1001
+ nsec = 1000000000LL - nsec;
1002
+ }
1003
+ }
1004
+ if (86400 == ni->exp) { // UTC time
1005
+ parent->val = rb_time_nano_new(ni->i, (long)nsec);
1006
+ // Since the ruby C routines always create local time, the
1007
+ // offset and then a conversion to UTC keeps makes the time
1008
+ // match the expected value.
1009
+ parent->val = rb_funcall2(parent->val, oj_utc_id, 0, 0);
1010
+ } else if (ni->has_exp) {
1011
+ int64_t t = (int64_t)(ni->i + ni->exp);
1012
+ struct _timeInfo ti;
1013
+ VALUE args[8];
1014
+
1015
+ sec_as_time(t, &ti);
1016
+
1017
+ args[0] = LONG2NUM(ti.year);
1018
+ args[1] = LONG2NUM(ti.mon);
1019
+ args[2] = LONG2NUM(ti.day);
1020
+ args[3] = LONG2NUM(ti.hour);
1021
+ args[4] = LONG2NUM(ti.min);
1022
+ args[5] = rb_float_new((double)ti.sec + ((double)nsec + 0.5) / 1000000000.0);
1023
+ args[6] = LONG2NUM(ni->exp);
1024
+ parent->val = rb_funcall2(rb_cTime, oj_new_id, 7, args);
1025
+ } else {
1026
+ parent->val = rb_time_nano_new(ni->i, (long)nsec);
1027
+ }
1028
+ rval = parent->val;
1029
+ } else {
1030
+ rb_hash_aset(parent->val, oj_calc_hash_key(pi, kval), rval);
1031
+ }
1032
+ break;
1033
+ default: break;
1034
+ }
1035
+ if (Yes == pi->options.trace) {
1036
+ oj_trace_parse_call("set_string", pi, __FILE__, __LINE__, rval);
1037
+ }
1038
+ }
1039
+
1040
+ static void hash_set_value(ParseInfo pi, Val kval, VALUE value) {
1041
+ Val parent = stack_peek(&pi->stack);
1042
+
1043
+ switch (rb_type(parent->val)) {
1044
+ case T_OBJECT: oj_set_obj_ivar(parent, kval, value); break;
1045
+ case T_HASH: rb_hash_aset(parent->val, oj_calc_hash_key(pi, kval), value); break;
1046
+ default: break;
1047
+ }
1048
+ if (Yes == pi->options.trace) {
1049
+ oj_trace_parse_call("set_value", pi, __FILE__, __LINE__, value);
1050
+ }
1051
+ }
1052
+
1053
+ static void array_append_num(ParseInfo pi, NumInfo ni) {
1054
+ Val parent = stack_peek(&pi->stack);
1055
+ volatile VALUE rval = oj_num_as_value(ni);
1056
+
1057
+ rb_ary_push(parent->val, rval);
1058
+ if (Yes == pi->options.trace) {
1059
+ oj_trace_parse_call("append_number", pi, __FILE__, __LINE__, rval);
1060
+ }
1061
+ }
1062
+
1063
+ static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
1064
+ volatile VALUE rstr = rb_utf8_str_new(str, len);
1065
+
1066
+ if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
1067
+ VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
1068
+
1069
+ if (Qnil != clas) {
1070
+ rb_ary_push(stack_peek(&pi->stack)->val, rb_funcall(clas, oj_json_create_id, 1, rstr));
1071
+ return;
1072
+ }
1073
+ }
1074
+ rb_ary_push(stack_peek(&pi->stack)->val, rstr);
1075
+ if (Yes == pi->options.trace) {
1076
+ oj_trace_parse_call("append_string", pi, __FILE__, __LINE__, rstr);
1077
+ }
1078
+ }
1079
+
1080
+ void oj_set_custom_callbacks(ParseInfo pi) {
1081
+ oj_set_compat_callbacks(pi);
1082
+ pi->hash_set_cstr = hash_set_cstr;
1083
+ pi->end_hash = end_hash;
1084
+ pi->hash_set_num = hash_set_num;
1085
+ pi->hash_set_value = hash_set_value;
1086
+ pi->array_append_cstr = array_append_cstr;
1087
+ pi->array_append_num = array_append_num;
1088
+ }
1089
+
1090
+ VALUE
1091
+ oj_custom_parse(int argc, VALUE *argv, VALUE self) {
1092
+ struct _parseInfo pi;
1093
+
1094
+ parse_info_init(&pi);
1095
+ pi.options = oj_default_options;
1096
+ pi.handler = Qnil;
1097
+ pi.err_class = Qnil;
1098
+ pi.max_depth = 0;
1099
+ pi.options.allow_nan = Yes;
1100
+ pi.options.nilnil = Yes;
1101
+ oj_set_custom_callbacks(&pi);
1102
+
1103
+ if (T_STRING == rb_type(*argv)) {
1104
+ return oj_pi_parse(argc, argv, &pi, 0, 0, false);
1105
+ } else {
1106
+ return oj_pi_sparse(argc, argv, &pi, 0);
1107
+ }
1108
+ }
1109
+
1110
+ VALUE
1111
+ oj_custom_parse_cstr(int argc, VALUE *argv, char *json, size_t len) {
1112
+ struct _parseInfo pi;
1113
+
1114
+ parse_info_init(&pi);
1115
+ pi.options = oj_default_options;
1116
+ pi.handler = Qnil;
1117
+ pi.err_class = Qnil;
1118
+ pi.max_depth = 0;
1119
+ pi.options.allow_nan = Yes;
1120
+ pi.options.nilnil = Yes;
1121
+ oj_set_custom_callbacks(&pi);
1122
+ pi.end_hash = end_hash;
1123
+
1124
+ return oj_pi_parse(argc, argv, &pi, json, len, false);
1125
+ }