oj 2.18.5 → 3.16.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +53 -221
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +54 -72
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +61 -63
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +38 -67
  11. data/ext/oj/circarray.h +16 -42
  12. data/ext/oj/code.c +214 -0
  13. data/ext/oj/code.h +40 -0
  14. data/ext/oj/compat.c +194 -110
  15. data/ext/oj/custom.c +1074 -0
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1276 -2494
  18. data/ext/oj/dump.h +110 -0
  19. data/ext/oj/dump_compat.c +897 -0
  20. data/ext/oj/dump_leaf.c +162 -0
  21. data/ext/oj/dump_object.c +710 -0
  22. data/ext/oj/dump_strict.c +399 -0
  23. data/ext/oj/encode.h +7 -42
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -53
  26. data/ext/oj/err.h +49 -46
  27. data/ext/oj/extconf.rb +33 -32
  28. data/ext/oj/fast.c +1082 -1098
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +919 -0
  34. data/ext/oj/object.c +545 -625
  35. data/ext/oj/odd.c +158 -168
  36. data/ext/oj/odd.h +32 -58
  37. data/ext/oj/oj.c +1727 -2080
  38. data/ext/oj/oj.h +334 -259
  39. data/ext/oj/parse.c +974 -753
  40. data/ext/oj/parse.h +97 -90
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +1478 -0
  44. data/ext/oj/rails.h +18 -0
  45. data/ext/oj/reader.c +136 -163
  46. data/ext/oj/reader.h +76 -112
  47. data/ext/oj/resolve.c +45 -94
  48. data/ext/oj/resolve.h +7 -34
  49. data/ext/oj/rxclass.c +144 -0
  50. data/ext/oj/rxclass.h +26 -0
  51. data/ext/oj/saj.c +445 -511
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +82 -143
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +749 -644
  57. data/ext/oj/stream_writer.c +329 -0
  58. data/ext/oj/strict.c +114 -112
  59. data/ext/oj/string_writer.c +517 -0
  60. data/ext/oj/trace.c +72 -0
  61. data/ext/oj/trace.h +55 -0
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +136 -0
  65. data/ext/oj/util.h +20 -0
  66. data/ext/oj/val_stack.c +75 -72
  67. data/ext/oj/val_stack.h +94 -127
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +586 -0
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +21 -13
  73. data/lib/oj/error.rb +10 -12
  74. data/lib/oj/json.rb +188 -0
  75. data/lib/oj/mimic.rb +165 -26
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +135 -0
  79. data/lib/oj/version.rb +2 -3
  80. data/lib/oj.rb +3 -31
  81. data/pages/Advanced.md +22 -0
  82. data/pages/Compatibility.md +25 -0
  83. data/pages/Custom.md +23 -0
  84. data/pages/Encoding.md +65 -0
  85. data/pages/InstallOptions.md +20 -0
  86. data/pages/JsonGem.md +94 -0
  87. data/pages/Modes.md +161 -0
  88. data/pages/Options.md +337 -0
  89. data/pages/Parser.md +309 -0
  90. data/pages/Rails.md +167 -0
  91. data/pages/Security.md +20 -0
  92. data/pages/WAB.md +13 -0
  93. metadata +126 -163
  94. data/ext/oj/hash.c +0 -163
  95. data/ext/oj/hash.h +0 -46
  96. data/ext/oj/hash_test.c +0 -512
  97. data/test/_test_active.rb +0 -76
  98. data/test/_test_active_mimic.rb +0 -96
  99. data/test/_test_mimic_rails.rb +0 -126
  100. data/test/activesupport_datetime_test.rb +0 -23
  101. data/test/bug.rb +0 -51
  102. data/test/bug2.rb +0 -10
  103. data/test/bug3.rb +0 -46
  104. data/test/bug_fast.rb +0 -32
  105. data/test/bug_load.rb +0 -24
  106. data/test/crash.rb +0 -111
  107. data/test/curl/curl_oj.rb +0 -46
  108. data/test/curl/get_oj.rb +0 -24
  109. data/test/curl/just_curl.rb +0 -31
  110. data/test/curl/just_oj.rb +0 -51
  111. data/test/example.rb +0 -11
  112. data/test/files.rb +0 -29
  113. data/test/foo.rb +0 -24
  114. data/test/helper.rb +0 -27
  115. data/test/io.rb +0 -48
  116. data/test/isolated/shared.rb +0 -310
  117. data/test/isolated/test_mimic_after.rb +0 -13
  118. data/test/isolated/test_mimic_alone.rb +0 -12
  119. data/test/isolated/test_mimic_as_json.rb +0 -45
  120. data/test/isolated/test_mimic_before.rb +0 -13
  121. data/test/isolated/test_mimic_define.rb +0 -28
  122. data/test/isolated/test_mimic_rails_after.rb +0 -22
  123. data/test/isolated/test_mimic_rails_before.rb +0 -21
  124. data/test/isolated/test_mimic_rails_datetime.rb +0 -27
  125. data/test/isolated/test_mimic_redefine.rb +0 -15
  126. data/test/mod.rb +0 -16
  127. data/test/perf.rb +0 -107
  128. data/test/perf_compat.rb +0 -128
  129. data/test/perf_fast.rb +0 -164
  130. data/test/perf_file.rb +0 -64
  131. data/test/perf_object.rb +0 -138
  132. data/test/perf_saj.rb +0 -109
  133. data/test/perf_scp.rb +0 -151
  134. data/test/perf_simple.rb +0 -287
  135. data/test/perf_strict.rb +0 -128
  136. data/test/rails.rb +0 -50
  137. data/test/russian.rb +0 -18
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -55
  151. data/test/sample_json.rb +0 -37
  152. data/test/struct.rb +0 -29
  153. data/test/test_compat.rb +0 -398
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -458
  156. data/test/test_file.rb +0 -245
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_object.rb +0 -745
  160. data/test/test_saj.rb +0 -186
  161. data/test/test_scp.rb +0 -396
  162. data/test/test_serializer.rb +0 -59
  163. data/test/test_strict.rb +0 -254
  164. data/test/test_various.rb +0 -1383
  165. data/test/test_writer.rb +0 -308
  166. data/test/write_timebars.rb +0 -31
@@ -0,0 +1,399 @@
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 <errno.h>
5
+ #include <math.h>
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+ #include <time.h>
10
+ #include <unistd.h>
11
+
12
+ #include "dump.h"
13
+ #include "trace.h"
14
+
15
+ // Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
16
+ #define OJ_INFINITY (1.0 / 0.0)
17
+
18
+ typedef unsigned long ulong;
19
+
20
+ static const char inf_val[] = INF_VAL;
21
+ static const char ninf_val[] = NINF_VAL;
22
+ static const char nan_val[] = NAN_VAL;
23
+
24
+ static void raise_strict(VALUE obj) {
25
+ rb_raise(rb_eTypeError, "Failed to dump %s Object to JSON in strict mode.\n", rb_class2name(rb_obj_class(obj)));
26
+ }
27
+
28
+ // Removed dependencies on math due to problems with CentOS 5.4.
29
+ static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
30
+ char buf[64];
31
+ char* b;
32
+ double d = rb_num2dbl(obj);
33
+ size_t cnt = 0;
34
+
35
+ if (0.0 == d) {
36
+ b = buf;
37
+ *b++ = '0';
38
+ *b++ = '.';
39
+ *b++ = '0';
40
+ *b++ = '\0';
41
+ cnt = 3;
42
+ } else {
43
+ NanDump nd = out->opts->dump_opts.nan_dump;
44
+
45
+ if (AutoNan == nd) {
46
+ nd = RaiseNan;
47
+ }
48
+ if (OJ_INFINITY == d) {
49
+ switch (nd) {
50
+ case RaiseNan:
51
+ case WordNan: raise_strict(obj); break;
52
+ case NullNan:
53
+ strcpy(buf, "null");
54
+ cnt = 4;
55
+ break;
56
+ case HugeNan:
57
+ default:
58
+ strcpy(buf, inf_val);
59
+ cnt = sizeof(inf_val) - 1;
60
+ break;
61
+ }
62
+ } else if (-OJ_INFINITY == d) {
63
+ switch (nd) {
64
+ case RaiseNan:
65
+ case WordNan: raise_strict(obj); break;
66
+ case NullNan:
67
+ strcpy(buf, "null");
68
+ cnt = 4;
69
+ break;
70
+ case HugeNan:
71
+ default:
72
+ strcpy(buf, ninf_val);
73
+ cnt = sizeof(ninf_val) - 1;
74
+ break;
75
+ }
76
+ } else if (isnan(d)) {
77
+ switch (nd) {
78
+ case RaiseNan:
79
+ case WordNan: raise_strict(obj); break;
80
+ case NullNan:
81
+ strcpy(buf, "null");
82
+ cnt = 4;
83
+ break;
84
+ case HugeNan:
85
+ default:
86
+ strcpy(buf, nan_val);
87
+ cnt = sizeof(nan_val) - 1;
88
+ break;
89
+ }
90
+ } else if (d == (double)(long long int)d) {
91
+ cnt = snprintf(buf, sizeof(buf), "%.1f", d);
92
+ } else if (0 == out->opts->float_prec) {
93
+ volatile VALUE rstr = oj_safe_string_convert(obj);
94
+
95
+ cnt = RSTRING_LEN(rstr);
96
+ if ((int)sizeof(buf) <= cnt) {
97
+ cnt = sizeof(buf) - 1;
98
+ }
99
+ memcpy(buf, RSTRING_PTR(rstr), cnt);
100
+ buf[cnt] = '\0';
101
+ } else {
102
+ cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, out->opts->float_fmt);
103
+ }
104
+ }
105
+ assure_size(out, cnt);
106
+ APPEND_CHARS(out->cur, buf, cnt);
107
+ *out->cur = '\0';
108
+ }
109
+
110
+ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
111
+ size_t size;
112
+ size_t i;
113
+ size_t cnt;
114
+ int d2 = depth + 1;
115
+
116
+ if (Yes == out->opts->circular) {
117
+ if (0 > oj_check_circular(a, out)) {
118
+ oj_dump_nil(Qnil, 0, out, false);
119
+ return;
120
+ }
121
+ }
122
+ cnt = RARRAY_LEN(a);
123
+ *out->cur++ = '[';
124
+ size = 2;
125
+ assure_size(out, size);
126
+ if (0 == cnt) {
127
+ *out->cur++ = ']';
128
+ } else {
129
+ if (out->opts->dump_opts.use) {
130
+ size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
131
+ } else {
132
+ size = d2 * out->indent + 2;
133
+ }
134
+ assure_size(out, size * cnt);
135
+ cnt--;
136
+ for (i = 0; i <= cnt; i++) {
137
+ if (out->opts->dump_opts.use) {
138
+ if (0 < out->opts->dump_opts.array_size) {
139
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
140
+ }
141
+ if (0 < out->opts->dump_opts.indent_size) {
142
+ int i;
143
+ for (i = d2; 0 < i; i--) {
144
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
145
+ }
146
+ }
147
+ } else {
148
+ fill_indent(out, d2);
149
+ }
150
+ if (NullMode == out->opts->mode) {
151
+ oj_dump_null_val(RARRAY_AREF(a, i), d2, out);
152
+ } else {
153
+ oj_dump_strict_val(RARRAY_AREF(a, i), d2, out);
154
+ }
155
+ if (i < cnt) {
156
+ *out->cur++ = ',';
157
+ }
158
+ }
159
+ size = depth * out->indent + 1;
160
+ assure_size(out, size);
161
+ if (out->opts->dump_opts.use) {
162
+ // printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
163
+ // out->opts->dump_opts->indent);
164
+ if (0 < out->opts->dump_opts.array_size) {
165
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
166
+ }
167
+ if (0 < out->opts->dump_opts.indent_size) {
168
+ int i;
169
+
170
+ for (i = depth; 0 < i; i--) {
171
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
172
+ }
173
+ }
174
+ } else {
175
+ fill_indent(out, depth);
176
+ }
177
+ *out->cur++ = ']';
178
+ }
179
+ *out->cur = '\0';
180
+ }
181
+
182
+ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
183
+ Out out = (Out)ov;
184
+ int depth = out->depth;
185
+ long size;
186
+ int rtype = rb_type(key);
187
+
188
+ if (rtype != T_STRING && rtype != T_SYMBOL) {
189
+ rb_raise(rb_eTypeError,
190
+ "In :strict and :null mode all Hash keys must be Strings or Symbols, not %s.\n",
191
+ rb_class2name(rb_obj_class(key)));
192
+ }
193
+ if (out->omit_nil && Qnil == value) {
194
+ return ST_CONTINUE;
195
+ }
196
+ if (!out->opts->dump_opts.use) {
197
+ size = depth * out->indent + 1;
198
+ assure_size(out, size);
199
+ fill_indent(out, depth);
200
+ if (rtype == T_STRING) {
201
+ oj_dump_str(key, 0, out, false);
202
+ } else {
203
+ oj_dump_sym(key, 0, out, false);
204
+ }
205
+ *out->cur++ = ':';
206
+ } else {
207
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
208
+ assure_size(out, size);
209
+ if (0 < out->opts->dump_opts.hash_size) {
210
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
211
+ }
212
+ if (0 < out->opts->dump_opts.indent_size) {
213
+ int i;
214
+ for (i = depth; 0 < i; i--) {
215
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
216
+ }
217
+ }
218
+ if (rtype == T_STRING) {
219
+ oj_dump_str(key, 0, out, false);
220
+ } else {
221
+ oj_dump_sym(key, 0, out, false);
222
+ }
223
+ size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
224
+ assure_size(out, size);
225
+ if (0 < out->opts->dump_opts.before_size) {
226
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
227
+ }
228
+ *out->cur++ = ':';
229
+ if (0 < out->opts->dump_opts.after_size) {
230
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
231
+ }
232
+ }
233
+ if (NullMode == out->opts->mode) {
234
+ oj_dump_null_val(value, depth, out);
235
+ } else {
236
+ oj_dump_strict_val(value, depth, out);
237
+ }
238
+ out->depth = depth;
239
+ *out->cur++ = ',';
240
+
241
+ return ST_CONTINUE;
242
+ }
243
+
244
+ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
245
+ int cnt;
246
+ size_t size;
247
+
248
+ if (Yes == out->opts->circular) {
249
+ if (0 > oj_check_circular(obj, out)) {
250
+ oj_dump_nil(Qnil, 0, out, false);
251
+ return;
252
+ }
253
+ }
254
+ cnt = (int)RHASH_SIZE(obj);
255
+ size = depth * out->indent + 2;
256
+ assure_size(out, 2);
257
+ *out->cur++ = '{';
258
+ if (0 == cnt) {
259
+ *out->cur++ = '}';
260
+ } else {
261
+ out->depth = depth + 1;
262
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
263
+ if (',' == *(out->cur - 1)) {
264
+ out->cur--; // backup to overwrite last comma
265
+ }
266
+ if (!out->opts->dump_opts.use) {
267
+ assure_size(out, size);
268
+ fill_indent(out, depth);
269
+ } else {
270
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
271
+ assure_size(out, size);
272
+ if (0 < out->opts->dump_opts.hash_size) {
273
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
274
+ }
275
+ if (0 < out->opts->dump_opts.indent_size) {
276
+ int i;
277
+
278
+ for (i = depth; 0 < i; i--) {
279
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
280
+ }
281
+ }
282
+ }
283
+ *out->cur++ = '}';
284
+ }
285
+ *out->cur = '\0';
286
+ }
287
+
288
+ static void dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
289
+ VALUE clas = rb_obj_class(obj);
290
+
291
+ if (oj_bigdecimal_class == clas) {
292
+ volatile VALUE rstr = oj_safe_string_convert(obj);
293
+
294
+ oj_dump_raw(RSTRING_PTR(rstr), RSTRING_LEN(rstr), out);
295
+ } else {
296
+ raise_strict(obj);
297
+ }
298
+ }
299
+
300
+ static void dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
301
+ VALUE clas = rb_obj_class(obj);
302
+
303
+ if (oj_bigdecimal_class == clas) {
304
+ volatile VALUE rstr = oj_safe_string_convert(obj);
305
+
306
+ oj_dump_raw(RSTRING_PTR(rstr), RSTRING_LEN(rstr), out);
307
+ } else {
308
+ oj_dump_nil(Qnil, depth, out, false);
309
+ }
310
+ }
311
+
312
+ static DumpFunc strict_funcs[] = {
313
+ NULL, // RUBY_T_NONE = 0x00,
314
+ dump_data_strict, // RUBY_T_OBJECT = 0x01,
315
+ NULL, // RUBY_T_CLASS = 0x02,
316
+ NULL, // RUBY_T_MODULE = 0x03,
317
+ dump_float, // RUBY_T_FLOAT = 0x04,
318
+ oj_dump_str, // RUBY_T_STRING = 0x05,
319
+ NULL, // RUBY_T_REGEXP = 0x06,
320
+ dump_array, // RUBY_T_ARRAY = 0x07,
321
+ dump_hash, // RUBY_T_HASH = 0x08,
322
+ NULL, // RUBY_T_STRUCT = 0x09,
323
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
324
+ NULL, // RUBY_T_FILE = 0x0b,
325
+ dump_data_strict, // RUBY_T_DATA = 0x0c,
326
+ NULL, // RUBY_T_MATCH = 0x0d,
327
+ NULL, // RUBY_T_COMPLEX = 0x0e,
328
+ NULL, // RUBY_T_RATIONAL = 0x0f,
329
+ NULL, // 0x10
330
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
331
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
332
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
333
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
334
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
335
+ };
336
+
337
+ void oj_dump_strict_val(VALUE obj, int depth, Out out) {
338
+ int type = rb_type(obj);
339
+
340
+ TRACE(out->opts->trace, "dump", obj, depth, TraceIn);
341
+ if (MAX_DEPTH < depth) {
342
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
343
+ }
344
+ if (0 < type && type <= RUBY_T_FIXNUM) {
345
+ DumpFunc f = strict_funcs[type];
346
+
347
+ if (NULL != f) {
348
+ f(obj, depth, out, false);
349
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
350
+ return;
351
+ }
352
+ }
353
+ raise_strict(obj);
354
+ }
355
+
356
+ static DumpFunc null_funcs[] = {
357
+ NULL, // RUBY_T_NONE = 0x00,
358
+ dump_data_null, // RUBY_T_OBJECT = 0x01,
359
+ NULL, // RUBY_T_CLASS = 0x02,
360
+ NULL, // RUBY_T_MODULE = 0x03,
361
+ dump_float, // RUBY_T_FLOAT = 0x04,
362
+ oj_dump_str, // RUBY_T_STRING = 0x05,
363
+ NULL, // RUBY_T_REGEXP = 0x06,
364
+ dump_array, // RUBY_T_ARRAY = 0x07,
365
+ dump_hash, // RUBY_T_HASH = 0x08,
366
+ NULL, // RUBY_T_STRUCT = 0x09,
367
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
368
+ NULL, // RUBY_T_FILE = 0x0b,
369
+ dump_data_null, // RUBY_T_DATA = 0x0c,
370
+ NULL, // RUBY_T_MATCH = 0x0d,
371
+ NULL, // RUBY_T_COMPLEX = 0x0e,
372
+ NULL, // RUBY_T_RATIONAL = 0x0f,
373
+ NULL, // 0x10
374
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
375
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
376
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
377
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
378
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
379
+ };
380
+
381
+ void oj_dump_null_val(VALUE obj, int depth, Out out) {
382
+ int type = rb_type(obj);
383
+
384
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
385
+ if (MAX_DEPTH < depth) {
386
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
387
+ }
388
+ if (0 < type && type <= RUBY_T_FIXNUM) {
389
+ DumpFunc f = null_funcs[type];
390
+
391
+ if (NULL != f) {
392
+ f(obj, depth, out, false);
393
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
394
+ return;
395
+ }
396
+ }
397
+ oj_dump_nil(Qnil, depth, out, false);
398
+ TRACE(out->opts->trace, "dump", Qnil, depth, TraceOut);
399
+ }
data/ext/oj/encode.h CHANGED
@@ -1,51 +1,16 @@
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
- */
1
+ // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
30
3
 
31
- #ifndef __OJ_ENCODE_H__
32
- #define __OJ_ENCODE_H__
4
+ #ifndef OJ_ENCODE_H
5
+ #define OJ_ENCODE_H
33
6
 
7
+ #include "oj.h"
34
8
  #include "ruby.h"
35
- #if HAS_ENCODING_SUPPORT
36
9
  #include "ruby/encoding.h"
37
- #endif
38
10
 
39
- static inline VALUE
40
- oj_encode(VALUE rstr) {
41
- #if HAS_ENCODING_SUPPORT
11
+ static inline VALUE oj_encode(VALUE rstr) {
42
12
  rb_enc_associate(rstr, oj_utf8_encoding);
43
- #else
44
- if (Qnil != oj_utf8_encoding) {
45
- rstr = rb_funcall(oj_utf8_encoding, oj_iconv_id, 1, rstr);
46
- }
47
- #endif
48
13
  return rstr;
49
14
  }
50
15
 
51
- #endif /* __OJ_ENCODE_H__ */
16
+ #endif /* OJ_ENCODE_H */
data/ext/oj/encoder.c ADDED
@@ -0,0 +1,43 @@
1
+ // Copyright (c) 2011, 2022 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include "oj.h"
5
+
6
+ typedef struct _encoder {
7
+ int indent; // indention for dump, default 2
8
+ char circular; // YesNo
9
+ char escape_mode; // Escape_Mode
10
+ char mode; // Mode
11
+ char time_format; // TimeFormat
12
+ char bigdec_as_num; // YesNo
13
+ char to_hash; // YesNo
14
+ char to_json; // YesNo
15
+ char as_json; // YesNo
16
+ char raw_json; // YesNo
17
+ char trace; // YesNo
18
+ char sec_prec_set; // boolean (0 or 1)
19
+ char ignore_under; // YesNo - ignore attrs starting with _ if true in object and custom modes
20
+ int64_t int_range_min; // dump numbers below as string
21
+ int64_t int_range_max; // dump numbers above as string
22
+ const char* create_id; // 0 or string
23
+ size_t create_id_len; // length of create_id
24
+ int sec_prec; // second precision when dumping time
25
+ char float_prec; // float precision, linked to float_fmt
26
+ char float_fmt[7]; // float format for dumping, if empty use Ruby
27
+ struct _dumpOpts dump_opts;
28
+ struct _rxClass str_rx;
29
+ VALUE* ignore; // Qnil terminated array of classes or NULL
30
+ }* Encoder;
31
+
32
+ /*
33
+ rb_define_module_function(Oj, "encode", encode, -1);
34
+ rb_define_module_function(Oj, "to_file", to_file, -1); // or maybe just write
35
+ rb_define_module_function(Oj, "to_stream", to_stream, -1);
36
+ */
37
+
38
+ // write(to, obj)
39
+ // if to is a string then open file
40
+ // else if stream then write to stream
41
+ // handle non-blocking
42
+
43
+ // should each mode have a different encoder or use delegates like the parser?
data/ext/oj/err.c CHANGED
@@ -1,40 +1,12 @@
1
- /* err.c
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
- #include <stdarg.h>
1
+ // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
32
3
 
33
4
  #include "err.h"
34
5
 
35
- void
36
- oj_err_set(Err e, VALUE clas, const char *format, ...) {
37
- va_list ap;
6
+ #include <stdarg.h>
7
+
8
+ void oj_err_set(Err e, VALUE clas, const char *format, ...) {
9
+ va_list ap;
38
10
 
39
11
  va_start(ap, format);
40
12
  e->clas = clas;
@@ -42,40 +14,43 @@ oj_err_set(Err e, VALUE clas, const char *format, ...) {
42
14
  va_end(ap);
43
15
  }
44
16
 
45
- void
46
- oj_err_raise(Err e) {
17
+ void oj_err_raise(Err e) {
47
18
  rb_raise(e->clas, "%s", e->msg);
48
19
  }
49
20
 
50
- void
51
- _oj_err_set_with_location(Err err, VALUE eclas, const char *msg, const char *json, const char *current, const char* file, int line) {
52
- int n = 1;
53
- int col = 1;
21
+ void _oj_err_set_with_location(Err err,
22
+ VALUE eclas,
23
+ const char *msg,
24
+ const char *json,
25
+ const char *current,
26
+ const char *file,
27
+ int line) {
28
+ int n = 1;
29
+ int col = 1;
54
30
 
55
31
  for (; json < current && '\n' != *current; current--) {
56
- col++;
32
+ col++;
57
33
  }
58
34
  for (; json < current; current--) {
59
- if ('\n' == *current) {
60
- n++;
61
- }
35
+ if ('\n' == *current) {
36
+ n++;
37
+ }
62
38
  }
63
39
  oj_err_set(err, eclas, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
64
40
  }
65
41
 
66
- void
67
- _oj_raise_error(const char *msg, const char *json, const char *current, const char* file, int line) {
68
- struct _Err err;
69
- int n = 1;
70
- int col = 1;
42
+ void _oj_raise_error(const char *msg, const char *json, const char *current, const char *file, int line) {
43
+ struct _err err;
44
+ int n = 1;
45
+ int col = 1;
71
46
 
72
47
  for (; json < current && '\n' != *current; current--) {
73
- col++;
48
+ col++;
74
49
  }
75
50
  for (; json < current; current--) {
76
- if ('\n' == *current) {
77
- n++;
78
- }
51
+ if ('\n' == *current) {
52
+ n++;
53
+ }
79
54
  }
80
55
  oj_err_set(&err, oj_parse_error_class, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
81
56
  rb_raise(err.clas, "%s", err.msg);