nano-safe-gem 0.0.1

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 (97) hide show
  1. checksums.yaml +7 -0
  2. data/nano-safe-gem.gemspec +12 -0
  3. data/oj-3.17.3/CHANGELOG.md +1506 -0
  4. data/oj-3.17.3/LICENSE +21 -0
  5. data/oj-3.17.3/README.md +103 -0
  6. data/oj-3.17.3/RELEASE_NOTES.md +61 -0
  7. data/oj-3.17.3/ext/oj/buf.h +85 -0
  8. data/oj-3.17.3/ext/oj/cache.c +329 -0
  9. data/oj-3.17.3/ext/oj/cache.h +22 -0
  10. data/oj-3.17.3/ext/oj/cache8.c +105 -0
  11. data/oj-3.17.3/ext/oj/cache8.h +21 -0
  12. data/oj-3.17.3/ext/oj/circarray.c +64 -0
  13. data/oj-3.17.3/ext/oj/circarray.h +22 -0
  14. data/oj-3.17.3/ext/oj/code.c +214 -0
  15. data/oj-3.17.3/ext/oj/code.h +40 -0
  16. data/oj-3.17.3/ext/oj/compat.c +239 -0
  17. data/oj-3.17.3/ext/oj/custom.c +1079 -0
  18. data/oj-3.17.3/ext/oj/debug.c +126 -0
  19. data/oj-3.17.3/ext/oj/dump.c +1765 -0
  20. data/oj-3.17.3/ext/oj/dump.h +112 -0
  21. data/oj-3.17.3/ext/oj/dump_compat.c +902 -0
  22. data/oj-3.17.3/ext/oj/dump_leaf.c +162 -0
  23. data/oj-3.17.3/ext/oj/dump_object.c +715 -0
  24. data/oj-3.17.3/ext/oj/dump_strict.c +404 -0
  25. data/oj-3.17.3/ext/oj/encode.h +16 -0
  26. data/oj-3.17.3/ext/oj/encoder.c +43 -0
  27. data/oj-3.17.3/ext/oj/err.c +57 -0
  28. data/oj-3.17.3/ext/oj/err.h +67 -0
  29. data/oj-3.17.3/ext/oj/extconf.rb +72 -0
  30. data/oj-3.17.3/ext/oj/fast.c +1730 -0
  31. data/oj-3.17.3/ext/oj/intern.c +313 -0
  32. data/oj-3.17.3/ext/oj/intern.h +22 -0
  33. data/oj-3.17.3/ext/oj/mem.c +318 -0
  34. data/oj-3.17.3/ext/oj/mem.h +53 -0
  35. data/oj-3.17.3/ext/oj/mimic_json.c +922 -0
  36. data/oj-3.17.3/ext/oj/object.c +726 -0
  37. data/oj-3.17.3/ext/oj/odd.c +245 -0
  38. data/oj-3.17.3/ext/oj/odd.h +43 -0
  39. data/oj-3.17.3/ext/oj/oj.c +2332 -0
  40. data/oj-3.17.3/ext/oj/oj.h +405 -0
  41. data/oj-3.17.3/ext/oj/parse.c +1367 -0
  42. data/oj-3.17.3/ext/oj/parse.h +113 -0
  43. data/oj-3.17.3/ext/oj/parser.c +1676 -0
  44. data/oj-3.17.3/ext/oj/parser.h +103 -0
  45. data/oj-3.17.3/ext/oj/rails.c +1480 -0
  46. data/oj-3.17.3/ext/oj/rails.h +18 -0
  47. data/oj-3.17.3/ext/oj/reader.c +220 -0
  48. data/oj-3.17.3/ext/oj/reader.h +137 -0
  49. data/oj-3.17.3/ext/oj/resolve.c +80 -0
  50. data/oj-3.17.3/ext/oj/resolve.h +12 -0
  51. data/oj-3.17.3/ext/oj/rxclass.c +144 -0
  52. data/oj-3.17.3/ext/oj/rxclass.h +26 -0
  53. data/oj-3.17.3/ext/oj/safe.c +230 -0
  54. data/oj-3.17.3/ext/oj/safe.h +79 -0
  55. data/oj-3.17.3/ext/oj/saj.c +677 -0
  56. data/oj-3.17.3/ext/oj/saj2.c +584 -0
  57. data/oj-3.17.3/ext/oj/saj2.h +23 -0
  58. data/oj-3.17.3/ext/oj/scp.c +185 -0
  59. data/oj-3.17.3/ext/oj/simd.h +219 -0
  60. data/oj-3.17.3/ext/oj/sparse.c +943 -0
  61. data/oj-3.17.3/ext/oj/stream_writer.c +329 -0
  62. data/oj-3.17.3/ext/oj/strict.c +187 -0
  63. data/oj-3.17.3/ext/oj/string_writer.c +517 -0
  64. data/oj-3.17.3/ext/oj/trace.c +72 -0
  65. data/oj-3.17.3/ext/oj/trace.h +55 -0
  66. data/oj-3.17.3/ext/oj/usual.c +1231 -0
  67. data/oj-3.17.3/ext/oj/usual.h +69 -0
  68. data/oj-3.17.3/ext/oj/util.c +136 -0
  69. data/oj-3.17.3/ext/oj/util.h +20 -0
  70. data/oj-3.17.3/ext/oj/val_stack.c +101 -0
  71. data/oj-3.17.3/ext/oj/val_stack.h +151 -0
  72. data/oj-3.17.3/ext/oj/validate.c +46 -0
  73. data/oj-3.17.3/ext/oj/wab.c +586 -0
  74. data/oj-3.17.3/lib/oj/active_support_helper.rb +39 -0
  75. data/oj-3.17.3/lib/oj/bag.rb +95 -0
  76. data/oj-3.17.3/lib/oj/easy_hash.rb +52 -0
  77. data/oj-3.17.3/lib/oj/error.rb +21 -0
  78. data/oj-3.17.3/lib/oj/json.rb +188 -0
  79. data/oj-3.17.3/lib/oj/mimic.rb +301 -0
  80. data/oj-3.17.3/lib/oj/saj.rb +80 -0
  81. data/oj-3.17.3/lib/oj/schandler.rb +143 -0
  82. data/oj-3.17.3/lib/oj/state.rb +135 -0
  83. data/oj-3.17.3/lib/oj/version.rb +4 -0
  84. data/oj-3.17.3/lib/oj.rb +15 -0
  85. data/oj-3.17.3/pages/Advanced.md +22 -0
  86. data/oj-3.17.3/pages/Compatibility.md +25 -0
  87. data/oj-3.17.3/pages/Custom.md +23 -0
  88. data/oj-3.17.3/pages/Encoding.md +65 -0
  89. data/oj-3.17.3/pages/InstallOptions.md +20 -0
  90. data/oj-3.17.3/pages/JsonGem.md +94 -0
  91. data/oj-3.17.3/pages/Modes.md +161 -0
  92. data/oj-3.17.3/pages/Options.md +337 -0
  93. data/oj-3.17.3/pages/Parser.md +309 -0
  94. data/oj-3.17.3/pages/Rails.md +167 -0
  95. data/oj-3.17.3/pages/Security.md +20 -0
  96. data/oj-3.17.3/pages/WAB.md +13 -0
  97. metadata +136 -0
@@ -0,0 +1,404 @@
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 (NULL != out->opts->dump_opts.only || NULL != out->opts->dump_opts.except) {
197
+ if (oj_key_skip(key, out->opts->dump_opts.only, out->opts->dump_opts.except)) {
198
+ return ST_CONTINUE;
199
+ }
200
+ }
201
+ if (!out->opts->dump_opts.use) {
202
+ size = depth * out->indent + 1;
203
+ assure_size(out, size);
204
+ fill_indent(out, depth);
205
+ switch (rtype) {
206
+ case T_STRING: oj_dump_str(key, 0, out, false); break;
207
+ case T_SYMBOL: oj_dump_sym(key, 0, out, false); break;
208
+ default: oj_dump_str(oj_safe_string_convert(key), 0, out, false); break;
209
+ }
210
+ *out->cur++ = ':';
211
+ } else {
212
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
213
+ assure_size(out, size);
214
+ if (0 < out->opts->dump_opts.hash_size) {
215
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
216
+ }
217
+ if (0 < out->opts->dump_opts.indent_size) {
218
+ int i;
219
+ for (i = depth; 0 < i; i--) {
220
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
221
+ }
222
+ }
223
+ switch (rtype) {
224
+ case T_STRING: oj_dump_str(key, 0, out, false); break;
225
+ case T_SYMBOL: oj_dump_sym(key, 0, out, false); break;
226
+ default: oj_dump_str(oj_safe_string_convert(key), 0, out, false); break;
227
+ }
228
+ size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
229
+ assure_size(out, size);
230
+ if (0 < out->opts->dump_opts.before_size) {
231
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
232
+ }
233
+ *out->cur++ = ':';
234
+ if (0 < out->opts->dump_opts.after_size) {
235
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
236
+ }
237
+ }
238
+ if (NullMode == out->opts->mode) {
239
+ oj_dump_null_val(value, depth, out);
240
+ } else {
241
+ oj_dump_strict_val(value, depth, out);
242
+ }
243
+ out->depth = depth;
244
+ *out->cur++ = ',';
245
+
246
+ return ST_CONTINUE;
247
+ }
248
+
249
+ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
250
+ int cnt;
251
+ size_t size;
252
+
253
+ if (Yes == out->opts->circular) {
254
+ if (0 > oj_check_circular(obj, out)) {
255
+ oj_dump_nil(Qnil, 0, out, false);
256
+ return;
257
+ }
258
+ }
259
+ cnt = (int)RHASH_SIZE(obj);
260
+ size = depth * out->indent + 2;
261
+ assure_size(out, 2);
262
+ *out->cur++ = '{';
263
+ if (0 == cnt) {
264
+ *out->cur++ = '}';
265
+ } else {
266
+ out->depth = depth + 1;
267
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
268
+ if (',' == *(out->cur - 1)) {
269
+ out->cur--; // backup to overwrite last comma
270
+ }
271
+ if (!out->opts->dump_opts.use) {
272
+ assure_size(out, size);
273
+ fill_indent(out, depth);
274
+ } else {
275
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
276
+ assure_size(out, size);
277
+ if (0 < out->opts->dump_opts.hash_size) {
278
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
279
+ }
280
+ if (0 < out->opts->dump_opts.indent_size) {
281
+ int i;
282
+
283
+ for (i = depth; 0 < i; i--) {
284
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
285
+ }
286
+ }
287
+ }
288
+ *out->cur++ = '}';
289
+ }
290
+ *out->cur = '\0';
291
+ }
292
+
293
+ static void dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
294
+ VALUE clas = rb_obj_class(obj);
295
+
296
+ if (oj_bigdecimal_class == clas) {
297
+ volatile VALUE rstr = oj_safe_string_convert(obj);
298
+
299
+ oj_dump_raw(RSTRING_PTR(rstr), RSTRING_LEN(rstr), out);
300
+ } else {
301
+ raise_strict(obj);
302
+ }
303
+ }
304
+
305
+ static void dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
306
+ VALUE clas = rb_obj_class(obj);
307
+
308
+ if (oj_bigdecimal_class == clas) {
309
+ volatile VALUE rstr = oj_safe_string_convert(obj);
310
+
311
+ oj_dump_raw(RSTRING_PTR(rstr), RSTRING_LEN(rstr), out);
312
+ } else {
313
+ oj_dump_nil(Qnil, depth, out, false);
314
+ }
315
+ }
316
+
317
+ static DumpFunc strict_funcs[] = {
318
+ NULL, // RUBY_T_NONE = 0x00,
319
+ dump_data_strict, // RUBY_T_OBJECT = 0x01,
320
+ NULL, // RUBY_T_CLASS = 0x02,
321
+ NULL, // RUBY_T_MODULE = 0x03,
322
+ dump_float, // RUBY_T_FLOAT = 0x04,
323
+ oj_dump_str, // RUBY_T_STRING = 0x05,
324
+ NULL, // RUBY_T_REGEXP = 0x06,
325
+ dump_array, // RUBY_T_ARRAY = 0x07,
326
+ dump_hash, // RUBY_T_HASH = 0x08,
327
+ NULL, // RUBY_T_STRUCT = 0x09,
328
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
329
+ NULL, // RUBY_T_FILE = 0x0b,
330
+ dump_data_strict, // RUBY_T_DATA = 0x0c,
331
+ NULL, // RUBY_T_MATCH = 0x0d,
332
+ NULL, // RUBY_T_COMPLEX = 0x0e,
333
+ NULL, // RUBY_T_RATIONAL = 0x0f,
334
+ NULL, // 0x10
335
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
336
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
337
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
338
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
339
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
340
+ };
341
+
342
+ void oj_dump_strict_val(VALUE obj, int depth, Out out) {
343
+ int type = rb_type(obj);
344
+
345
+ TRACE(out->opts->trace, "dump", obj, depth, TraceIn);
346
+ if (MAX_DEPTH < depth) {
347
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
348
+ }
349
+ if (0 < type && type <= RUBY_T_FIXNUM) {
350
+ DumpFunc f = strict_funcs[type];
351
+
352
+ if (NULL != f) {
353
+ f(obj, depth, out, false);
354
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
355
+ return;
356
+ }
357
+ }
358
+ raise_strict(obj);
359
+ }
360
+
361
+ static DumpFunc null_funcs[] = {
362
+ NULL, // RUBY_T_NONE = 0x00,
363
+ dump_data_null, // RUBY_T_OBJECT = 0x01,
364
+ NULL, // RUBY_T_CLASS = 0x02,
365
+ NULL, // RUBY_T_MODULE = 0x03,
366
+ dump_float, // RUBY_T_FLOAT = 0x04,
367
+ oj_dump_str, // RUBY_T_STRING = 0x05,
368
+ NULL, // RUBY_T_REGEXP = 0x06,
369
+ dump_array, // RUBY_T_ARRAY = 0x07,
370
+ dump_hash, // RUBY_T_HASH = 0x08,
371
+ NULL, // RUBY_T_STRUCT = 0x09,
372
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
373
+ NULL, // RUBY_T_FILE = 0x0b,
374
+ dump_data_null, // RUBY_T_DATA = 0x0c,
375
+ NULL, // RUBY_T_MATCH = 0x0d,
376
+ NULL, // RUBY_T_COMPLEX = 0x0e,
377
+ NULL, // RUBY_T_RATIONAL = 0x0f,
378
+ NULL, // 0x10
379
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
380
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
381
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
382
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
383
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
384
+ };
385
+
386
+ void oj_dump_null_val(VALUE obj, int depth, Out out) {
387
+ int type = rb_type(obj);
388
+
389
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
390
+ if (MAX_DEPTH < depth) {
391
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
392
+ }
393
+ if (0 < type && type <= RUBY_T_FIXNUM) {
394
+ DumpFunc f = null_funcs[type];
395
+
396
+ if (NULL != f) {
397
+ f(obj, depth, out, false);
398
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
399
+ return;
400
+ }
401
+ }
402
+ oj_dump_nil(Qnil, depth, out, false);
403
+ TRACE(out->opts->trace, "dump", Qnil, depth, TraceOut);
404
+ }
@@ -0,0 +1,16 @@
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.
3
+
4
+ #ifndef OJ_ENCODE_H
5
+ #define OJ_ENCODE_H
6
+
7
+ #include "oj.h"
8
+ #include "ruby.h"
9
+ #include "ruby/encoding.h"
10
+
11
+ static inline VALUE oj_encode(VALUE rstr) {
12
+ rb_enc_associate(rstr, oj_utf8_encoding);
13
+ return rstr;
14
+ }
15
+
16
+ #endif /* OJ_ENCODE_H */
@@ -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?
@@ -0,0 +1,57 @@
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.
3
+
4
+ #include "err.h"
5
+
6
+ #include <stdarg.h>
7
+
8
+ void oj_err_set(Err e, VALUE clas, const char *format, ...) {
9
+ va_list ap;
10
+
11
+ va_start(ap, format);
12
+ e->clas = clas;
13
+ vsnprintf(e->msg, sizeof(e->msg) - 1, format, ap);
14
+ va_end(ap);
15
+ }
16
+
17
+ void oj_err_raise(Err e) {
18
+ rb_raise(e->clas, "%s", e->msg);
19
+ }
20
+
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;
30
+
31
+ for (; json < current && '\n' != *current; current--) {
32
+ col++;
33
+ }
34
+ for (; json < current; current--) {
35
+ if ('\n' == *current) {
36
+ n++;
37
+ }
38
+ }
39
+ oj_err_set(err, eclas, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
40
+ }
41
+
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;
46
+
47
+ for (; json < current && '\n' != *current; current--) {
48
+ col++;
49
+ }
50
+ for (; json < current; current--) {
51
+ if ('\n' == *current) {
52
+ n++;
53
+ }
54
+ }
55
+ oj_err_set(&err, oj_parse_error_class, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
56
+ rb_raise(err.clas, "%s", err.msg);
57
+ }
@@ -0,0 +1,67 @@
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.
3
+
4
+ #ifndef OJ_ERR_H
5
+ #define OJ_ERR_H
6
+
7
+ #include <errno.h>
8
+
9
+ #include "ruby.h"
10
+
11
+ // Needed to silence 2.4.0 warnings.
12
+ #ifndef NORETURN
13
+ #define NORETURN(x) x
14
+ #endif
15
+
16
+ #define OJ_ERR_START 300
17
+
18
+ typedef enum {
19
+ OJ_OK = 0,
20
+ OJ_ERR_MEMORY = ENOMEM,
21
+ OJ_ERR_PARSE = OJ_ERR_START,
22
+ OJ_ERR_READ,
23
+ OJ_ERR_WRITE,
24
+ OJ_ERR_OVERFLOW,
25
+ OJ_ERR_ARG,
26
+ OJ_ERR_TOO_MANY,
27
+ OJ_ERR_TYPE,
28
+ OJ_ERR_KEY,
29
+ OJ_ABORT,
30
+ OJ_ERR_LAST,
31
+ } ojStatus;
32
+
33
+ #define set_error(err, eclas, msg, json, current) _oj_err_set_with_location(err, eclas, msg, json, current, FILE, LINE)
34
+
35
+ typedef struct _err {
36
+ VALUE clas;
37
+ char msg[128];
38
+ } *Err;
39
+
40
+ extern VALUE oj_parse_error_class;
41
+
42
+ extern void oj_err_set(Err e, VALUE clas, const char *format, ...);
43
+ extern void _oj_err_set_with_location(Err err,
44
+ VALUE eclas,
45
+ const char *msg,
46
+ const char *json,
47
+ const char *current,
48
+ const char *file,
49
+ int line);
50
+
51
+ NORETURN(extern void oj_err_raise(Err e));
52
+
53
+ #define raise_error(msg, json, current) _oj_raise_error(msg, json, current, __FILE__, __LINE__)
54
+
55
+ NORETURN(
56
+ extern void _oj_raise_error(const char *msg, const char *json, const char *current, const char *file, int line));
57
+
58
+ inline static void err_init(Err e) {
59
+ e->clas = Qnil;
60
+ *e->msg = '\0';
61
+ }
62
+
63
+ inline static int err_has(Err e) {
64
+ return (Qnil != e->clas);
65
+ }
66
+
67
+ #endif /* OJ_ERR_H */
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkmf'
4
+ require 'rbconfig'
5
+
6
+ extension_name = 'oj'
7
+ dir_config(extension_name)
8
+
9
+ parts = RUBY_DESCRIPTION.split(' ')
10
+ type = parts[0]
11
+ type = type[4..] if type.start_with?('tcs-')
12
+ is_windows = RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
13
+ platform = RUBY_PLATFORM
14
+ version = RUBY_VERSION.split('.')
15
+ puts ">>>>> Creating Makefile for #{type} version #{RUBY_VERSION} on #{platform} <<<<<"
16
+
17
+ dflags = {
18
+ 'RUBY_TYPE' => type,
19
+ (type.upcase + '_RUBY') => nil,
20
+ 'RUBY_VERSION' => RUBY_VERSION,
21
+ 'RUBY_VERSION_MAJOR' => version[0],
22
+ 'RUBY_VERSION_MINOR' => version[1],
23
+ 'RUBY_VERSION_MICRO' => version[2],
24
+ 'IS_WINDOWS' => is_windows ? 1 : 0,
25
+ 'RSTRUCT_LEN_RETURNS_INTEGER_OBJECT' => ('ruby' == type && '2' == version[0] && '4' == version[1] && '1' >= version[2]) ? 1 : 0,
26
+ }
27
+
28
+ # Support for compaction.
29
+ have_func('rb_gc_mark_movable')
30
+ have_func('stpcpy')
31
+ have_func('pthread_mutex_init')
32
+ have_func('getrlimit', 'sys/resource.h')
33
+ have_func('rb_enc_interned_str')
34
+ have_func('rb_ext_ractor_safe', 'ruby.h')
35
+
36
+ dflags['OJ_DEBUG'] = true unless ENV['OJ_DEBUG'].nil?
37
+
38
+ # SIMD optimizations use runtime CPU detection and function-level target attributes
39
+ # We do NOT add global -msse4.2/-msse2 flags here because:
40
+ # 1. It would cause illegal instruction errors on CPUs without SSE4.2
41
+ # 2. The code uses __attribute__((target("sse4.2"))) for SSE4.2 functions
42
+ # 3. Runtime detection in oj_get_simd_implementation() selects the right path
43
+ #
44
+ # We only add -msse2 if available, since SSE2 is baseline for all x86_64 CPUs
45
+ # and needed for compiling the SSE2 fallback code on 32-bit x86
46
+ if try_cflags('-msse2')
47
+ $CPPFLAGS += ' -msse2'
48
+ end
49
+
50
+ if enable_config('trace-log', false)
51
+ dflags['OJ_ENABLE_TRACE_LOG'] = 1
52
+ end
53
+
54
+ dflags.each do |k, v|
55
+ if v.nil?
56
+ $CPPFLAGS += " -D#{k}"
57
+ else
58
+ $CPPFLAGS += " -D#{k}=#{v}"
59
+ end
60
+ end
61
+
62
+ $CPPFLAGS += ' -Wall'
63
+ # puts "*** $CPPFLAGS: #{$CPPFLAGS}"
64
+ # Adding the __attribute__ flag only works with gcc compilers and even then it
65
+ # does not work to check args with varargs so just remove the check.
66
+ CONFIG['warnflags'].slice!(/ -Wsuggest-attribute=format/)
67
+ CONFIG['warnflags'].slice!(/ -Wdeclaration-after-statement/)
68
+ CONFIG['warnflags'].slice!(/ -Wmissing-noreturn/)
69
+
70
+ create_makefile(File.join(extension_name, extension_name))
71
+
72
+ %x{make clean}