oj 3.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +104 -0
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +107 -0
  6. data/ext/oj/cache8.h +48 -0
  7. data/ext/oj/circarray.c +68 -0
  8. data/ext/oj/circarray.h +23 -0
  9. data/ext/oj/code.c +235 -0
  10. data/ext/oj/code.h +42 -0
  11. data/ext/oj/compat.c +299 -0
  12. data/ext/oj/custom.c +1218 -0
  13. data/ext/oj/dump.c +1249 -0
  14. data/ext/oj/dump.h +96 -0
  15. data/ext/oj/dump_compat.c +975 -0
  16. data/ext/oj/dump_leaf.c +252 -0
  17. data/ext/oj/dump_object.c +844 -0
  18. data/ext/oj/dump_strict.c +434 -0
  19. data/ext/oj/encode.h +45 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +53 -0
  23. data/ext/oj/fast.c +1771 -0
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +890 -0
  28. data/ext/oj/object.c +775 -0
  29. data/ext/oj/odd.c +231 -0
  30. data/ext/oj/odd.h +44 -0
  31. data/ext/oj/oj.c +1723 -0
  32. data/ext/oj/oj.h +387 -0
  33. data/ext/oj/parse.c +1134 -0
  34. data/ext/oj/parse.h +112 -0
  35. data/ext/oj/rails.c +1528 -0
  36. data/ext/oj/rails.h +21 -0
  37. data/ext/oj/reader.c +231 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +102 -0
  40. data/ext/oj/resolve.h +14 -0
  41. data/ext/oj/rxclass.c +147 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +714 -0
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +924 -0
  46. data/ext/oj/stream_writer.c +363 -0
  47. data/ext/oj/strict.c +212 -0
  48. data/ext/oj/string_writer.c +534 -0
  49. data/ext/oj/trace.c +79 -0
  50. data/ext/oj/trace.h +28 -0
  51. data/ext/oj/util.c +136 -0
  52. data/ext/oj/util.h +19 -0
  53. data/ext/oj/val_stack.c +118 -0
  54. data/ext/oj/val_stack.h +185 -0
  55. data/ext/oj/wab.c +631 -0
  56. data/lib/oj.rb +21 -0
  57. data/lib/oj/active_support_helper.rb +41 -0
  58. data/lib/oj/bag.rb +88 -0
  59. data/lib/oj/easy_hash.rb +52 -0
  60. data/lib/oj/error.rb +22 -0
  61. data/lib/oj/json.rb +176 -0
  62. data/lib/oj/mimic.rb +267 -0
  63. data/lib/oj/saj.rb +66 -0
  64. data/lib/oj/schandler.rb +142 -0
  65. data/lib/oj/state.rb +131 -0
  66. data/lib/oj/version.rb +5 -0
  67. data/pages/Advanced.md +22 -0
  68. data/pages/Compatibility.md +25 -0
  69. data/pages/Custom.md +23 -0
  70. data/pages/Encoding.md +65 -0
  71. data/pages/JsonGem.md +79 -0
  72. data/pages/Modes.md +155 -0
  73. data/pages/Options.md +287 -0
  74. data/pages/Rails.md +155 -0
  75. data/pages/Security.md +20 -0
  76. data/pages/WAB.md +13 -0
  77. data/test/_test_active.rb +76 -0
  78. data/test/_test_active_mimic.rb +96 -0
  79. data/test/_test_mimic_rails.rb +126 -0
  80. data/test/activerecord/result_test.rb +27 -0
  81. data/test/activesupport4/decoding_test.rb +108 -0
  82. data/test/activesupport4/encoding_test.rb +531 -0
  83. data/test/activesupport4/test_helper.rb +41 -0
  84. data/test/activesupport5/abstract_unit.rb +45 -0
  85. data/test/activesupport5/decoding_test.rb +133 -0
  86. data/test/activesupport5/encoding_test.rb +500 -0
  87. data/test/activesupport5/encoding_test_cases.rb +98 -0
  88. data/test/activesupport5/test_helper.rb +72 -0
  89. data/test/activesupport5/time_zone_test_helpers.rb +39 -0
  90. data/test/activesupport6/abstract_unit.rb +44 -0
  91. data/test/activesupport6/decoding_test.rb +133 -0
  92. data/test/activesupport6/encoding_test.rb +507 -0
  93. data/test/activesupport6/encoding_test_cases.rb +98 -0
  94. data/test/activesupport6/test_common.rb +17 -0
  95. data/test/activesupport6/test_helper.rb +163 -0
  96. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  97. data/test/bar.rb +35 -0
  98. data/test/baz.rb +16 -0
  99. data/test/files.rb +29 -0
  100. data/test/foo.rb +52 -0
  101. data/test/helper.rb +26 -0
  102. data/test/isolated/shared.rb +308 -0
  103. data/test/isolated/test_mimic_after.rb +13 -0
  104. data/test/isolated/test_mimic_alone.rb +12 -0
  105. data/test/isolated/test_mimic_as_json.rb +45 -0
  106. data/test/isolated/test_mimic_before.rb +13 -0
  107. data/test/isolated/test_mimic_define.rb +28 -0
  108. data/test/isolated/test_mimic_rails_after.rb +22 -0
  109. data/test/isolated/test_mimic_rails_before.rb +21 -0
  110. data/test/isolated/test_mimic_redefine.rb +15 -0
  111. data/test/json_gem/json_addition_test.rb +216 -0
  112. data/test/json_gem/json_common_interface_test.rb +148 -0
  113. data/test/json_gem/json_encoding_test.rb +107 -0
  114. data/test/json_gem/json_ext_parser_test.rb +20 -0
  115. data/test/json_gem/json_fixtures_test.rb +35 -0
  116. data/test/json_gem/json_generator_test.rb +383 -0
  117. data/test/json_gem/json_generic_object_test.rb +90 -0
  118. data/test/json_gem/json_parser_test.rb +470 -0
  119. data/test/json_gem/json_string_matching_test.rb +42 -0
  120. data/test/json_gem/test_helper.rb +18 -0
  121. data/test/perf.rb +107 -0
  122. data/test/perf_compat.rb +130 -0
  123. data/test/perf_fast.rb +164 -0
  124. data/test/perf_file.rb +64 -0
  125. data/test/perf_object.rb +138 -0
  126. data/test/perf_saj.rb +109 -0
  127. data/test/perf_scp.rb +151 -0
  128. data/test/perf_simple.rb +287 -0
  129. data/test/perf_strict.rb +145 -0
  130. data/test/perf_wab.rb +131 -0
  131. data/test/prec.rb +23 -0
  132. data/test/sample.rb +54 -0
  133. data/test/sample/change.rb +14 -0
  134. data/test/sample/dir.rb +19 -0
  135. data/test/sample/doc.rb +36 -0
  136. data/test/sample/file.rb +48 -0
  137. data/test/sample/group.rb +16 -0
  138. data/test/sample/hasprops.rb +16 -0
  139. data/test/sample/layer.rb +12 -0
  140. data/test/sample/line.rb +20 -0
  141. data/test/sample/oval.rb +10 -0
  142. data/test/sample/rect.rb +10 -0
  143. data/test/sample/shape.rb +35 -0
  144. data/test/sample/text.rb +20 -0
  145. data/test/sample_json.rb +37 -0
  146. data/test/test_compat.rb +502 -0
  147. data/test/test_custom.rb +527 -0
  148. data/test/test_debian.rb +53 -0
  149. data/test/test_fast.rb +470 -0
  150. data/test/test_file.rb +239 -0
  151. data/test/test_gc.rb +49 -0
  152. data/test/test_hash.rb +29 -0
  153. data/test/test_integer_range.rb +72 -0
  154. data/test/test_null.rb +376 -0
  155. data/test/test_object.rb +1027 -0
  156. data/test/test_rails.rb +26 -0
  157. data/test/test_saj.rb +186 -0
  158. data/test/test_scp.rb +433 -0
  159. data/test/test_strict.rb +433 -0
  160. data/test/test_various.rb +719 -0
  161. data/test/test_wab.rb +307 -0
  162. data/test/test_writer.rb +380 -0
  163. data/test/tests.rb +25 -0
  164. data/test/tests_mimic.rb +14 -0
  165. data/test/tests_mimic_addition.rb +7 -0
  166. data/test/zoo.rb +13 -0
  167. metadata +381 -0
@@ -0,0 +1,434 @@
1
+ /* dump_strict.c
2
+ * Copyright (c) 2012, 2017, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #include <stdlib.h>
7
+ #include <time.h>
8
+ #include <stdio.h>
9
+ #include <string.h>
10
+ #include <math.h>
11
+ #include <unistd.h>
12
+ #include <errno.h>
13
+
14
+ #include "dump.h"
15
+ #include "trace.h"
16
+
17
+ // Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
18
+ #define OJ_INFINITY (1.0/0.0)
19
+
20
+ typedef unsigned long ulong;
21
+
22
+ static const char inf_val[] = INF_VAL;
23
+ static const char ninf_val[] = NINF_VAL;
24
+ static const char nan_val[] = NAN_VAL;
25
+
26
+ static void
27
+ raise_strict(VALUE obj) {
28
+ rb_raise(rb_eTypeError, "Failed to dump %s Object to JSON in strict mode.\n", rb_class2name(rb_obj_class(obj)));
29
+ }
30
+
31
+ // Removed dependencies on math due to problems with CentOS 5.4.
32
+ static void
33
+ dump_float(VALUE obj, int depth, Out out, bool as_ok) {
34
+ char buf[64];
35
+ char *b;
36
+ double d = rb_num2dbl(obj);
37
+ int cnt = 0;
38
+
39
+ if (0.0 == d) {
40
+ b = buf;
41
+ *b++ = '0';
42
+ *b++ = '.';
43
+ *b++ = '0';
44
+ *b++ = '\0';
45
+ cnt = 3;
46
+ } else {
47
+ NanDump nd = out->opts->dump_opts.nan_dump;
48
+
49
+ if (AutoNan == nd) {
50
+ nd = RaiseNan;
51
+ }
52
+ if (OJ_INFINITY == d) {
53
+ switch (nd) {
54
+ case RaiseNan:
55
+ case WordNan:
56
+ raise_strict(obj);
57
+ break;
58
+ case NullNan:
59
+ strcpy(buf, "null");
60
+ cnt = 4;
61
+ break;
62
+ case HugeNan:
63
+ default:
64
+ strcpy(buf, inf_val);
65
+ cnt = sizeof(inf_val) - 1;
66
+ break;
67
+ }
68
+ } else if (-OJ_INFINITY == d) {
69
+ switch (nd) {
70
+ case RaiseNan:
71
+ case WordNan:
72
+ raise_strict(obj);
73
+ break;
74
+ case NullNan:
75
+ strcpy(buf, "null");
76
+ cnt = 4;
77
+ break;
78
+ case HugeNan:
79
+ default:
80
+ strcpy(buf, ninf_val);
81
+ cnt = sizeof(ninf_val) - 1;
82
+ break;
83
+ }
84
+ } else if (isnan(d)) {
85
+ switch (nd) {
86
+ case RaiseNan:
87
+ case WordNan:
88
+ raise_strict(obj);
89
+ break;
90
+ case NullNan:
91
+ strcpy(buf, "null");
92
+ cnt = 4;
93
+ break;
94
+ case HugeNan:
95
+ default:
96
+ strcpy(buf, nan_val);
97
+ cnt = sizeof(nan_val) - 1;
98
+ break;
99
+ }
100
+ } else if (d == (double)(long long int)d) {
101
+ cnt = snprintf(buf, sizeof(buf), "%.1f", d);
102
+ } else if (0 == out->opts->float_prec) {
103
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
104
+
105
+ cnt = (int)RSTRING_LEN(rstr);
106
+ if ((int)sizeof(buf) <= cnt) {
107
+ cnt = sizeof(buf) - 1;
108
+ }
109
+ strncpy(buf, rb_string_value_ptr((VALUE*)&rstr), cnt);
110
+ buf[cnt] = '\0';
111
+ } else {
112
+ cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, out->opts->float_fmt);
113
+ }
114
+ }
115
+ assure_size(out, cnt);
116
+ for (b = buf; '\0' != *b; b++) {
117
+ *out->cur++ = *b;
118
+ }
119
+ *out->cur = '\0';
120
+ }
121
+
122
+ static void
123
+ dump_array(VALUE a, int depth, Out out, bool as_ok) {
124
+ size_t size;
125
+ int i, cnt;
126
+ int d2 = depth + 1;
127
+
128
+ if (Yes == out->opts->circular) {
129
+ if (0 > oj_check_circular(a, out)) {
130
+ oj_dump_nil(Qnil, 0, out, false);
131
+ return;
132
+ }
133
+ }
134
+ cnt = (int)RARRAY_LEN(a);
135
+ *out->cur++ = '[';
136
+ size = 2;
137
+ assure_size(out, size);
138
+ if (0 == cnt) {
139
+ *out->cur++ = ']';
140
+ } else {
141
+ if (out->opts->dump_opts.use) {
142
+ size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
143
+ } else {
144
+ size = d2 * out->indent + 2;
145
+ }
146
+ cnt--;
147
+ for (i = 0; i <= cnt; i++) {
148
+ assure_size(out, size);
149
+ if (out->opts->dump_opts.use) {
150
+ if (0 < out->opts->dump_opts.array_size) {
151
+ strcpy(out->cur, out->opts->dump_opts.array_nl);
152
+ out->cur += out->opts->dump_opts.array_size;
153
+ }
154
+ if (0 < out->opts->dump_opts.indent_size) {
155
+ int i;
156
+ for (i = d2; 0 < i; i--) {
157
+ strcpy(out->cur, out->opts->dump_opts.indent_str);
158
+ out->cur += out->opts->dump_opts.indent_size;
159
+ }
160
+ }
161
+ } else {
162
+ fill_indent(out, d2);
163
+ }
164
+ if (NullMode == out->opts->mode) {
165
+ oj_dump_null_val(rb_ary_entry(a, i), d2, out);
166
+ } else {
167
+ oj_dump_strict_val(rb_ary_entry(a, i), d2, out);
168
+ }
169
+ if (i < cnt) {
170
+ *out->cur++ = ',';
171
+ }
172
+ }
173
+ size = depth * out->indent + 1;
174
+ assure_size(out, size);
175
+ if (out->opts->dump_opts.use) {
176
+ //printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size, out->opts->dump_opts->indent);
177
+ if (0 < out->opts->dump_opts.array_size) {
178
+ strcpy(out->cur, out->opts->dump_opts.array_nl);
179
+ out->cur += out->opts->dump_opts.array_size;
180
+ }
181
+ if (0 < out->opts->dump_opts.indent_size) {
182
+ int i;
183
+
184
+ for (i = depth; 0 < i; i--) {
185
+ strcpy(out->cur, out->opts->dump_opts.indent_str);
186
+ out->cur += out->opts->dump_opts.indent_size;
187
+ }
188
+ }
189
+ } else {
190
+ fill_indent(out, depth);
191
+ }
192
+ *out->cur++ = ']';
193
+ }
194
+ *out->cur = '\0';
195
+ }
196
+
197
+ static int
198
+ hash_cb(VALUE key, VALUE value, VALUE ov) {
199
+ Out out = (Out)ov;
200
+ int depth = out->depth;
201
+ long size;
202
+ int rtype = rb_type(key);
203
+
204
+ if (rtype != T_STRING && rtype != T_SYMBOL) {
205
+ rb_raise(rb_eTypeError, "In :strict and :null mode all Hash keys must be Strings or Symbols, not %s.\n", rb_class2name(rb_obj_class(key)));
206
+ }
207
+ if (out->omit_nil && Qnil == value) {
208
+ return ST_CONTINUE;
209
+ }
210
+ if (!out->opts->dump_opts.use) {
211
+ size = depth * out->indent + 1;
212
+ assure_size(out, size);
213
+ fill_indent(out, depth);
214
+ if (rtype == T_STRING) {
215
+ oj_dump_str(key, 0, out, false);
216
+ } else {
217
+ oj_dump_sym(key, 0, out, false);
218
+ }
219
+ *out->cur++ = ':';
220
+ } else {
221
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
222
+ assure_size(out, size);
223
+ if (0 < out->opts->dump_opts.hash_size) {
224
+ strcpy(out->cur, out->opts->dump_opts.hash_nl);
225
+ out->cur += out->opts->dump_opts.hash_size;
226
+ }
227
+ if (0 < out->opts->dump_opts.indent_size) {
228
+ int i;
229
+ for (i = depth; 0 < i; i--) {
230
+ strcpy(out->cur, out->opts->dump_opts.indent_str);
231
+ out->cur += out->opts->dump_opts.indent_size;
232
+ }
233
+ }
234
+ if (rtype == T_STRING) {
235
+ oj_dump_str(key, 0, out, false);
236
+ } else {
237
+ oj_dump_sym(key, 0, out, false);
238
+ }
239
+ size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
240
+ assure_size(out, size);
241
+ if (0 < out->opts->dump_opts.before_size) {
242
+ strcpy(out->cur, out->opts->dump_opts.before_sep);
243
+ out->cur += out->opts->dump_opts.before_size;
244
+ }
245
+ *out->cur++ = ':';
246
+ if (0 < out->opts->dump_opts.after_size) {
247
+ strcpy(out->cur, out->opts->dump_opts.after_sep);
248
+ out->cur += out->opts->dump_opts.after_size;
249
+ }
250
+ }
251
+ if (NullMode == out->opts->mode) {
252
+ oj_dump_null_val(value, depth, out);
253
+ } else {
254
+ oj_dump_strict_val(value, depth, out);
255
+ }
256
+ out->depth = depth;
257
+ *out->cur++ = ',';
258
+
259
+ return ST_CONTINUE;
260
+ }
261
+
262
+ static void
263
+ dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
264
+ int cnt;
265
+ size_t size;
266
+
267
+ if (Yes == out->opts->circular) {
268
+ if (0 > oj_check_circular(obj, out)) {
269
+ oj_dump_nil(Qnil, 0, out, false);
270
+ return;
271
+ }
272
+ }
273
+ cnt = (int)RHASH_SIZE(obj);
274
+ size = depth * out->indent + 2;
275
+ assure_size(out, 2);
276
+ *out->cur++ = '{';
277
+ if (0 == cnt) {
278
+ *out->cur++ = '}';
279
+ } else {
280
+ out->depth = depth + 1;
281
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
282
+ if (',' == *(out->cur - 1)) {
283
+ out->cur--; // backup to overwrite last comma
284
+ }
285
+ if (!out->opts->dump_opts.use) {
286
+ assure_size(out, size);
287
+ fill_indent(out, depth);
288
+ } else {
289
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
290
+ assure_size(out, size);
291
+ if (0 < out->opts->dump_opts.hash_size) {
292
+ strcpy(out->cur, out->opts->dump_opts.hash_nl);
293
+ out->cur += out->opts->dump_opts.hash_size;
294
+ }
295
+ if (0 < out->opts->dump_opts.indent_size) {
296
+ int i;
297
+
298
+ for (i = depth; 0 < i; i--) {
299
+ strcpy(out->cur, out->opts->dump_opts.indent_str);
300
+ out->cur += out->opts->dump_opts.indent_size;
301
+ }
302
+ }
303
+ }
304
+ *out->cur++ = '}';
305
+ }
306
+ *out->cur = '\0';
307
+ }
308
+
309
+ static void
310
+ dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
311
+ VALUE clas = rb_obj_class(obj);
312
+
313
+ if (oj_bigdecimal_class == clas) {
314
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
315
+
316
+ oj_dump_raw(rb_string_value_ptr((VALUE*)&rstr), (int)RSTRING_LEN(rstr), out);
317
+ } else {
318
+ raise_strict(obj);
319
+ }
320
+ }
321
+
322
+ static void
323
+ dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
324
+ VALUE clas = rb_obj_class(obj);
325
+
326
+ if (oj_bigdecimal_class == clas) {
327
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
328
+
329
+ oj_dump_raw(rb_string_value_ptr((VALUE*)&rstr), (int)RSTRING_LEN(rstr), out);
330
+ } else {
331
+ oj_dump_nil(Qnil, depth, out, false);
332
+ }
333
+ }
334
+
335
+ static DumpFunc strict_funcs[] = {
336
+ NULL, // RUBY_T_NONE = 0x00,
337
+ dump_data_strict, // RUBY_T_OBJECT = 0x01,
338
+ NULL, // RUBY_T_CLASS = 0x02,
339
+ NULL, // RUBY_T_MODULE = 0x03,
340
+ dump_float, // RUBY_T_FLOAT = 0x04,
341
+ oj_dump_str, // RUBY_T_STRING = 0x05,
342
+ NULL, // RUBY_T_REGEXP = 0x06,
343
+ dump_array, // RUBY_T_ARRAY = 0x07,
344
+ dump_hash, // RUBY_T_HASH = 0x08,
345
+ NULL, // RUBY_T_STRUCT = 0x09,
346
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
347
+ NULL, // RUBY_T_FILE = 0x0b,
348
+ dump_data_strict, // RUBY_T_DATA = 0x0c,
349
+ NULL, // RUBY_T_MATCH = 0x0d,
350
+ NULL, // RUBY_T_COMPLEX = 0x0e,
351
+ NULL, // RUBY_T_RATIONAL = 0x0f,
352
+ NULL, // 0x10
353
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
354
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
355
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
356
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
357
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
358
+ };
359
+
360
+ void
361
+ oj_dump_strict_val(VALUE obj, int depth, Out out) {
362
+ int type = rb_type(obj);
363
+
364
+ if (Yes == out->opts->trace) {
365
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceIn);
366
+ }
367
+ if (MAX_DEPTH < depth) {
368
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
369
+ }
370
+ if (0 < type && type <= RUBY_T_FIXNUM) {
371
+ DumpFunc f = strict_funcs[type];
372
+
373
+ if (NULL != f) {
374
+ f(obj, depth, out, false);
375
+ if (Yes == out->opts->trace) {
376
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
377
+ }
378
+ return;
379
+ }
380
+ }
381
+ raise_strict(obj);
382
+ }
383
+
384
+ static DumpFunc null_funcs[] = {
385
+ NULL, // RUBY_T_NONE = 0x00,
386
+ dump_data_null, // RUBY_T_OBJECT = 0x01,
387
+ NULL, // RUBY_T_CLASS = 0x02,
388
+ NULL, // RUBY_T_MODULE = 0x03,
389
+ dump_float, // RUBY_T_FLOAT = 0x04,
390
+ oj_dump_str, // RUBY_T_STRING = 0x05,
391
+ NULL, // RUBY_T_REGEXP = 0x06,
392
+ dump_array, // RUBY_T_ARRAY = 0x07,
393
+ dump_hash, // RUBY_T_HASH = 0x08,
394
+ NULL, // RUBY_T_STRUCT = 0x09,
395
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
396
+ NULL, // RUBY_T_FILE = 0x0b,
397
+ dump_data_null, // RUBY_T_DATA = 0x0c,
398
+ NULL, // RUBY_T_MATCH = 0x0d,
399
+ NULL, // RUBY_T_COMPLEX = 0x0e,
400
+ NULL, // RUBY_T_RATIONAL = 0x0f,
401
+ NULL, // 0x10
402
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
403
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
404
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
405
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
406
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
407
+ };
408
+
409
+ void
410
+ oj_dump_null_val(VALUE obj, int depth, Out out) {
411
+ int type = rb_type(obj);
412
+
413
+ if (Yes == out->opts->trace) {
414
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
415
+ }
416
+ if (MAX_DEPTH < depth) {
417
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
418
+ }
419
+ if (0 < type && type <= RUBY_T_FIXNUM) {
420
+ DumpFunc f = null_funcs[type];
421
+
422
+ if (NULL != f) {
423
+ f(obj, depth, out, false);
424
+ if (Yes == out->opts->trace) {
425
+ oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
426
+ }
427
+ return;
428
+ }
429
+ }
430
+ oj_dump_nil(Qnil, depth, out, false);
431
+ if (Yes == out->opts->trace) {
432
+ oj_trace("dump", Qnil, __FILE__, __LINE__, depth, TraceOut);
433
+ }
434
+ }
@@ -0,0 +1,45 @@
1
+ /* encode.h
2
+ * Copyright (c) 2011, 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
+ */
30
+
31
+ #ifndef OJ_ENCODE_H
32
+ #define OJ_ENCODE_H
33
+
34
+ #include "ruby.h"
35
+ #include "ruby/encoding.h"
36
+
37
+ #include "oj.h"
38
+
39
+ static inline VALUE
40
+ oj_encode(VALUE rstr) {
41
+ rb_enc_associate(rstr, oj_utf8_encoding);
42
+ return rstr;
43
+ }
44
+
45
+ #endif /* OJ_ENCODE_H */