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,586 @@
1
+ // Copyright (c) 2012 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+ #include <time.h>
8
+ #include <unistd.h>
9
+
10
+ #include "dump.h"
11
+ #include "encode.h"
12
+ #include "err.h"
13
+ #include "intern.h"
14
+ #include "oj.h"
15
+ #include "parse.h"
16
+ #include "trace.h"
17
+ #include "util.h"
18
+
19
+ // Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
20
+ #define OJ_INFINITY (1.0 / 0.0)
21
+
22
+ static char hex_chars[257] = "\
23
+ ................................\
24
+ ................xxxxxxxxxx......\
25
+ .xxxxxx.........................\
26
+ .xxxxxx.........................\
27
+ ................................\
28
+ ................................\
29
+ ................................\
30
+ ................................";
31
+
32
+ static VALUE wab_uuid_clas = Qundef;
33
+ static VALUE uri_clas = Qundef;
34
+ static VALUE uri_http_clas = Qundef;
35
+
36
+ ///// dump functions /////
37
+
38
+ static VALUE resolve_wab_uuid_class(void) {
39
+ if (Qundef == wab_uuid_clas) {
40
+ volatile VALUE wab_module;
41
+
42
+ wab_uuid_clas = Qnil;
43
+ if (rb_const_defined_at(rb_cObject, rb_intern("WAB"))) {
44
+ wab_module = rb_const_get_at(rb_cObject, rb_intern("WAB"));
45
+ if (rb_const_defined_at(wab_module, rb_intern("UUID"))) {
46
+ wab_uuid_clas = rb_const_get(wab_module, rb_intern("UUID"));
47
+ }
48
+ }
49
+ }
50
+ return wab_uuid_clas;
51
+ }
52
+
53
+ static VALUE resolve_uri_class(void) {
54
+ if (Qundef == uri_clas) {
55
+ uri_clas = Qnil;
56
+ if (rb_const_defined_at(rb_cObject, rb_intern("URI"))) {
57
+ uri_clas = rb_const_get_at(rb_cObject, rb_intern("URI"));
58
+ }
59
+ }
60
+ return uri_clas;
61
+ }
62
+
63
+ static VALUE resolve_uri_http_class(void) {
64
+ if (Qundef == uri_http_clas) {
65
+ volatile VALUE uri_module;
66
+
67
+ uri_http_clas = Qnil;
68
+ if (rb_const_defined_at(rb_cObject, rb_intern("URI"))) {
69
+ uri_module = rb_const_get_at(rb_cObject, rb_intern("URI"));
70
+ if (rb_const_defined_at(uri_module, rb_intern("HTTP"))) {
71
+ uri_http_clas = rb_const_get(uri_module, rb_intern("HTTP"));
72
+ }
73
+ }
74
+ }
75
+ return uri_http_clas;
76
+ }
77
+
78
+ static void raise_wab(VALUE obj) {
79
+ rb_raise(rb_eTypeError, "Failed to dump %s Object to JSON in wab mode.\n", rb_class2name(rb_obj_class(obj)));
80
+ }
81
+
82
+ // Removed dependencies on math due to problems with CentOS 5.4.
83
+ static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
84
+ char buf[64];
85
+ char *b;
86
+ double d = rb_num2dbl(obj);
87
+ int cnt = 0;
88
+
89
+ if (0.0 == d) {
90
+ b = buf;
91
+ *b++ = '0';
92
+ *b++ = '.';
93
+ *b++ = '0';
94
+ *b++ = '\0';
95
+ cnt = 3;
96
+ } else {
97
+ if (OJ_INFINITY == d || -OJ_INFINITY == d || isnan(d)) {
98
+ raise_wab(obj);
99
+ } else if (d == (double)(long long int)d) {
100
+ cnt = snprintf(buf, sizeof(buf), "%.1f", d);
101
+ } else {
102
+ cnt = snprintf(buf, sizeof(buf), "%0.16g", d);
103
+ }
104
+ }
105
+ assure_size(out, cnt);
106
+ for (b = buf; '\0' != *b; b++) {
107
+ *out->cur++ = *b;
108
+ }
109
+ *out->cur = '\0';
110
+ }
111
+
112
+ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
113
+ size_t size;
114
+ size_t i;
115
+ size_t cnt;
116
+ int d2 = depth + 1;
117
+
118
+ cnt = RARRAY_LEN(a);
119
+ *out->cur++ = '[';
120
+ size = 2;
121
+ assure_size(out, size);
122
+ if (0 == cnt) {
123
+ *out->cur++ = ']';
124
+ } else {
125
+ size = d2 * out->indent + 2;
126
+ assure_size(out, size * cnt);
127
+ cnt--;
128
+ for (i = 0; i <= cnt; i++) {
129
+ fill_indent(out, d2);
130
+ oj_dump_wab_val(RARRAY_AREF(a, i), d2, out);
131
+ if (i < cnt) {
132
+ *out->cur++ = ',';
133
+ }
134
+ }
135
+ size = depth * out->indent + 1;
136
+ assure_size(out, size);
137
+ fill_indent(out, depth);
138
+ *out->cur++ = ']';
139
+ }
140
+ *out->cur = '\0';
141
+ }
142
+
143
+ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
144
+ Out out = (Out)ov;
145
+ int depth = out->depth;
146
+ long size;
147
+ int rtype = rb_type(key);
148
+
149
+ if (rtype != T_SYMBOL) {
150
+ rb_raise(rb_eTypeError,
151
+ "In :wab mode all Hash keys must be Symbols, not %s.\n",
152
+ rb_class2name(rb_obj_class(key)));
153
+ }
154
+ size = depth * out->indent + 1;
155
+ assure_size(out, size);
156
+ fill_indent(out, depth);
157
+ oj_dump_sym(key, 0, out, false);
158
+ *out->cur++ = ':';
159
+ oj_dump_wab_val(value, depth, out);
160
+ out->depth = depth;
161
+ *out->cur++ = ',';
162
+
163
+ return ST_CONTINUE;
164
+ }
165
+
166
+ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
167
+ int cnt;
168
+ size_t size;
169
+
170
+ cnt = (int)RHASH_SIZE(obj);
171
+ size = depth * out->indent + 2;
172
+ assure_size(out, 2);
173
+ *out->cur++ = '{';
174
+ if (0 == cnt) {
175
+ *out->cur++ = '}';
176
+ } else {
177
+ out->depth = depth + 1;
178
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
179
+ if (',' == *(out->cur - 1)) {
180
+ out->cur--; // backup to overwrite last comma
181
+ }
182
+ assure_size(out, size);
183
+ fill_indent(out, depth);
184
+ *out->cur++ = '}';
185
+ }
186
+ *out->cur = '\0';
187
+ }
188
+
189
+ static void dump_time(VALUE obj, Out out) {
190
+ char buf[64];
191
+ struct _timeInfo ti;
192
+ int len;
193
+ time_t sec;
194
+ long long nsec;
195
+
196
+ if (16 <= sizeof(struct timespec)) {
197
+ struct timespec ts = rb_time_timespec(obj);
198
+
199
+ sec = ts.tv_sec;
200
+ nsec = ts.tv_nsec;
201
+ } else {
202
+ sec = NUM2LL(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
203
+ nsec = NUM2LL(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
204
+ }
205
+
206
+ assure_size(out, 36);
207
+ // 2012-01-05T23:58:07.123456000Z
208
+ sec_as_time(sec, &ti);
209
+
210
+ len = sprintf(buf,
211
+ "%04d-%02d-%02dT%02d:%02d:%02d.%09ldZ",
212
+ ti.year,
213
+ ti.mon,
214
+ ti.day,
215
+ ti.hour,
216
+ ti.min,
217
+ ti.sec,
218
+ (long)nsec);
219
+ oj_dump_cstr(buf, len, 0, 0, out);
220
+ }
221
+
222
+ static void dump_obj(VALUE obj, int depth, Out out, bool as_ok) {
223
+ volatile VALUE clas = rb_obj_class(obj);
224
+
225
+ if (rb_cTime == clas) {
226
+ dump_time(obj, out);
227
+ } else if (oj_bigdecimal_class == clas) {
228
+ volatile VALUE rstr = oj_safe_string_convert(obj);
229
+
230
+ oj_dump_raw(RSTRING_PTR(rstr), RSTRING_LEN(rstr), out);
231
+ } else if (resolve_wab_uuid_class() == clas) {
232
+ oj_dump_str(oj_safe_string_convert(obj), depth, out, false);
233
+ } else if (resolve_uri_http_class() == clas) {
234
+ oj_dump_str(oj_safe_string_convert(obj), depth, out, false);
235
+ } else {
236
+ raise_wab(obj);
237
+ }
238
+ }
239
+
240
+ static DumpFunc wab_funcs[] = {
241
+ NULL, // RUBY_T_NONE = 0x00,
242
+ dump_obj, // RUBY_T_OBJECT = 0x01,
243
+ NULL, // RUBY_T_CLASS = 0x02,
244
+ NULL, // RUBY_T_MODULE = 0x03,
245
+ dump_float, // RUBY_T_FLOAT = 0x04,
246
+ oj_dump_str, // RUBY_T_STRING = 0x05,
247
+ NULL, // RUBY_T_REGEXP = 0x06,
248
+ dump_array, // RUBY_T_ARRAY = 0x07,
249
+ dump_hash, // RUBY_T_HASH = 0x08,
250
+ NULL, // RUBY_T_STRUCT = 0x09,
251
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
252
+ NULL, // RUBY_T_FILE = 0x0b,
253
+ dump_obj, // RUBY_T_DATA = 0x0c,
254
+ NULL, // RUBY_T_MATCH = 0x0d,
255
+ NULL, // RUBY_T_COMPLEX = 0x0e,
256
+ NULL, // RUBY_T_RATIONAL = 0x0f,
257
+ NULL, // 0x10
258
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
259
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
260
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
261
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
262
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
263
+ };
264
+
265
+ void oj_dump_wab_val(VALUE obj, int depth, Out out) {
266
+ int type = rb_type(obj);
267
+
268
+ TRACE(out->opts->trace, "dump", obj, depth, TraceIn);
269
+ if (MAX_DEPTH < depth) {
270
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
271
+ }
272
+ if (0 < type && type <= RUBY_T_FIXNUM) {
273
+ DumpFunc f = wab_funcs[type];
274
+
275
+ if (NULL != f) {
276
+ f(obj, depth, out, false);
277
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
278
+ return;
279
+ }
280
+ }
281
+ raise_wab(obj);
282
+ }
283
+
284
+ ///// load functions /////
285
+
286
+ static VALUE calc_hash_key(ParseInfo pi, Val parent) {
287
+ volatile VALUE rkey = parent->key_val;
288
+
289
+ if (Qundef != rkey) {
290
+ rkey = oj_encode(rkey);
291
+ rkey = rb_str_intern(rkey);
292
+
293
+ return rkey;
294
+ }
295
+ if (Yes == pi->options.cache_keys) {
296
+ rkey = oj_sym_intern(parent->key, parent->klen);
297
+ } else {
298
+ #if HAVE_RB_ENC_INTERNED_STR
299
+ rkey = rb_enc_interned_str(parent->key, parent->klen, oj_utf8_encoding);
300
+ #else
301
+ rkey = rb_utf8_str_new(parent->key, parent->klen);
302
+ rkey = rb_str_intern(rkey);
303
+ OBJ_FREEZE(rkey);
304
+ #endif
305
+ }
306
+ return rkey;
307
+ }
308
+
309
+ static void hash_end(ParseInfo pi) {
310
+ TRACE_PARSE_HASH_END(pi->options.trace, pi);
311
+ }
312
+
313
+ static void array_end(ParseInfo pi) {
314
+ TRACE_PARSE_ARRAY_END(pi->options.trace, pi);
315
+ }
316
+
317
+ static VALUE noop_hash_key(ParseInfo pi, const char *key, size_t klen) {
318
+ return Qundef;
319
+ }
320
+
321
+ static void add_value(ParseInfo pi, VALUE val) {
322
+ TRACE_PARSE_CALL(pi->options.trace, "add_value", pi, val);
323
+ pi->stack.head->val = val;
324
+ }
325
+
326
+ // 123e4567-e89b-12d3-a456-426655440000
327
+ static bool uuid_check(const char *str, int len) {
328
+ int i;
329
+
330
+ for (i = 0; i < 8; i++, str++) {
331
+ if ('x' != hex_chars[*(uint8_t *)str]) {
332
+ return false;
333
+ }
334
+ }
335
+ str++;
336
+ for (i = 0; i < 4; i++, str++) {
337
+ if ('x' != hex_chars[*(uint8_t *)str]) {
338
+ return false;
339
+ }
340
+ }
341
+ str++;
342
+ for (i = 0; i < 4; i++, str++) {
343
+ if ('x' != hex_chars[*(uint8_t *)str]) {
344
+ return false;
345
+ }
346
+ }
347
+ str++;
348
+ for (i = 0; i < 4; i++, str++) {
349
+ if ('x' != hex_chars[*(uint8_t *)str]) {
350
+ return false;
351
+ }
352
+ }
353
+ str++;
354
+ for (i = 0; i < 12; i++, str++) {
355
+ if ('x' != hex_chars[*(uint8_t *)str]) {
356
+ return false;
357
+ }
358
+ }
359
+ return true;
360
+ }
361
+
362
+ static const char *read_num(const char *s, int len, int *vp) {
363
+ uint32_t v = 0;
364
+
365
+ for (; 0 < len; len--, s++) {
366
+ if ('0' <= *s && *s <= '9') {
367
+ v = v * 10 + *s - '0';
368
+ } else {
369
+ return NULL;
370
+ }
371
+ }
372
+ *vp = (int)v;
373
+
374
+ return s;
375
+ }
376
+
377
+ static VALUE time_parse(const char *s, int len) {
378
+ struct tm tm;
379
+ bool neg = false;
380
+ long nsecs = 0;
381
+ int i;
382
+ time_t secs;
383
+
384
+ memset(&tm, 0, sizeof(tm));
385
+ if ('-' == *s) {
386
+ s++;
387
+ neg = true;
388
+ }
389
+ if (NULL == (s = read_num(s, 4, &tm.tm_year))) {
390
+ return Qnil;
391
+ }
392
+ if (neg) {
393
+ tm.tm_year = -tm.tm_year;
394
+ neg = false;
395
+ }
396
+ tm.tm_year -= 1900;
397
+ s++;
398
+ if (NULL == (s = read_num(s, 2, &tm.tm_mon))) {
399
+ return Qnil;
400
+ }
401
+ tm.tm_mon--;
402
+ s++;
403
+ if (NULL == (s = read_num(s, 2, &tm.tm_mday))) {
404
+ return Qnil;
405
+ }
406
+ s++;
407
+ if (NULL == (s = read_num(s, 2, &tm.tm_hour))) {
408
+ return Qnil;
409
+ }
410
+ s++;
411
+ if (NULL == (s = read_num(s, 2, &tm.tm_min))) {
412
+ return Qnil;
413
+ }
414
+ s++;
415
+ if (NULL == (s = read_num(s, 2, &tm.tm_sec))) {
416
+ return Qnil;
417
+ }
418
+ s++;
419
+
420
+ for (i = 9; 0 < i; i--, s++) {
421
+ if ('0' <= *s && *s <= '9') {
422
+ nsecs = nsecs * 10 + *s - '0';
423
+ } else {
424
+ return Qnil;
425
+ }
426
+ }
427
+ #if IS_WINDOWS
428
+ secs = (time_t)mktime(&tm);
429
+ memset(&tm, 0, sizeof(tm));
430
+ tm.tm_year = 70;
431
+ tm.tm_mday = 1;
432
+ secs -= (time_t)mktime(&tm);
433
+ #else
434
+ secs = (time_t)timegm(&tm);
435
+ #endif
436
+ return rb_funcall(rb_time_nano_new(secs, nsecs), oj_utc_id, 0);
437
+ }
438
+
439
+ static VALUE protect_uri(VALUE rstr) {
440
+ return rb_funcall(resolve_uri_class(), oj_parse_id, 1, rstr);
441
+ }
442
+
443
+ static VALUE cstr_to_rstr(ParseInfo pi, const char *str, size_t len) {
444
+ volatile VALUE v = Qnil;
445
+
446
+ if (30 == len && '-' == str[4] && '-' == str[7] && 'T' == str[10] && ':' == str[13] && ':' == str[16] &&
447
+ '.' == str[19] && 'Z' == str[29]) {
448
+ if (Qnil != (v = time_parse(str, (int)len))) {
449
+ return v;
450
+ }
451
+ }
452
+ if (36 == len && '-' == str[8] && '-' == str[13] && '-' == str[18] && '-' == str[23] && uuid_check(str, (int)len) &&
453
+ Qnil != resolve_wab_uuid_class()) {
454
+ return rb_funcall(wab_uuid_clas, oj_new_id, 1, rb_str_new(str, len));
455
+ }
456
+ if (7 < len && 0 == strncasecmp("http://", str, 7)) {
457
+ int err = 0;
458
+ v = rb_str_new(str, len);
459
+ volatile VALUE uri = rb_protect(protect_uri, v, &err);
460
+
461
+ if (0 == err) {
462
+ return uri;
463
+ }
464
+ }
465
+ return oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
466
+ }
467
+
468
+ static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
469
+ pi->stack.head->val = cstr_to_rstr(pi, str, len);
470
+ TRACE_PARSE_CALL(pi->options.trace, "add_string", pi, pi->stack.head->val);
471
+ }
472
+
473
+ static void add_num(ParseInfo pi, NumInfo ni) {
474
+ if (ni->infinity || ni->nan) {
475
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
476
+ }
477
+ pi->stack.head->val = oj_num_as_value(ni);
478
+ TRACE_PARSE_CALL(pi->options.trace, "add_number", pi, pi->stack.head->val);
479
+ }
480
+
481
+ static VALUE start_hash(ParseInfo pi) {
482
+ TRACE_PARSE_IN(pi->options.trace, "start_hash", pi);
483
+ if (Qnil != pi->options.hash_class) {
484
+ return rb_class_new_instance(0, NULL, pi->options.hash_class);
485
+ }
486
+ return rb_hash_new();
487
+ }
488
+
489
+ static void hash_set_cstr(ParseInfo pi, Val parent, const char *str, size_t len, const char *orig) {
490
+ volatile VALUE rval = cstr_to_rstr(pi, str, len);
491
+
492
+ rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), rval);
493
+ TRACE_PARSE_CALL(pi->options.trace, "set_string", pi, rval);
494
+ }
495
+
496
+ static void hash_set_num(ParseInfo pi, Val parent, NumInfo ni) {
497
+ volatile VALUE rval = Qnil;
498
+
499
+ if (ni->infinity || ni->nan) {
500
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
501
+ }
502
+ rval = oj_num_as_value(ni);
503
+ rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), rval);
504
+ TRACE_PARSE_CALL(pi->options.trace, "set_number", pi, rval);
505
+ }
506
+
507
+ static void hash_set_value(ParseInfo pi, Val parent, VALUE value) {
508
+ rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), value);
509
+ TRACE_PARSE_CALL(pi->options.trace, "set_value", pi, value);
510
+ }
511
+
512
+ static VALUE start_array(ParseInfo pi) {
513
+ TRACE_PARSE_IN(pi->options.trace, "start_array", pi);
514
+ return rb_ary_new();
515
+ }
516
+
517
+ static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
518
+ volatile VALUE rval = cstr_to_rstr(pi, str, len);
519
+
520
+ rb_ary_push(stack_peek(&pi->stack)->val, rval);
521
+ TRACE_PARSE_CALL(pi->options.trace, "set_value", pi, rval);
522
+ }
523
+
524
+ static void array_append_num(ParseInfo pi, NumInfo ni) {
525
+ volatile VALUE rval = Qnil;
526
+
527
+ if (ni->infinity || ni->nan) {
528
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
529
+ }
530
+ rval = oj_num_as_value(ni);
531
+ rb_ary_push(stack_peek(&pi->stack)->val, rval);
532
+ TRACE_PARSE_CALL(pi->options.trace, "append_number", pi, rval);
533
+ }
534
+
535
+ static void array_append_value(ParseInfo pi, VALUE value) {
536
+ rb_ary_push(stack_peek(&pi->stack)->val, value);
537
+ TRACE_PARSE_CALL(pi->options.trace, "append_value", pi, value);
538
+ }
539
+
540
+ void oj_set_wab_callbacks(ParseInfo pi) {
541
+ pi->start_hash = start_hash;
542
+ pi->end_hash = hash_end;
543
+ pi->hash_key = noop_hash_key;
544
+ pi->hash_set_cstr = hash_set_cstr;
545
+ pi->hash_set_num = hash_set_num;
546
+ pi->hash_set_value = hash_set_value;
547
+ pi->start_array = start_array;
548
+ pi->end_array = array_end;
549
+ pi->array_append_cstr = array_append_cstr;
550
+ pi->array_append_num = array_append_num;
551
+ pi->array_append_value = array_append_value;
552
+ pi->add_cstr = add_cstr;
553
+ pi->add_num = add_num;
554
+ pi->add_value = add_value;
555
+ pi->expect_value = 1;
556
+ }
557
+
558
+ VALUE
559
+ oj_wab_parse(int argc, VALUE *argv, VALUE self) {
560
+ struct _parseInfo pi;
561
+
562
+ parse_info_init(&pi);
563
+ pi.options = oj_default_options;
564
+ pi.handler = Qnil;
565
+ pi.err_class = Qnil;
566
+ oj_set_wab_callbacks(&pi);
567
+
568
+ if (T_STRING == rb_type(*argv)) {
569
+ return oj_pi_parse(argc, argv, &pi, 0, 0, true);
570
+ } else {
571
+ return oj_pi_sparse(argc, argv, &pi, 0);
572
+ }
573
+ }
574
+
575
+ VALUE
576
+ oj_wab_parse_cstr(int argc, VALUE *argv, char *json, size_t len) {
577
+ struct _parseInfo pi;
578
+
579
+ parse_info_init(&pi);
580
+ pi.options = oj_default_options;
581
+ pi.handler = Qnil;
582
+ pi.err_class = Qnil;
583
+ oj_set_wab_callbacks(&pi);
584
+
585
+ return oj_pi_parse(argc, argv, &pi, json, len, true);
586
+ }
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/time'
4
+
5
+ module Oj
6
+ # Exists only to handle the ActiveSupport::TimeWithZone.
7
+ class ActiveSupportHelper
8
+ def self.createTimeWithZone(utc, zone)
9
+ ActiveSupport::TimeWithZone.new(utc - utc.gmt_offset, ActiveSupport::TimeZone[zone])
10
+ end
11
+ end
12
+ end
13
+
14
+ Oj.register_odd(ActiveSupport::TimeWithZone, Oj::ActiveSupportHelper, :createTimeWithZone, :utc, 'time_zone.name')
15
+
16
+ # This is a hack to work around an oddness with DateTime and the ActiveSupport
17
+ # that causes a hang when some methods are called from C. Hour, min(ute),
18
+ # sec(ond) and other methods are special but they can be called from C until
19
+ # activesupport/time is required. After that they can not be even though
20
+ # resond_to? returns true. By defining methods to call super the problem goes
21
+ # away. There is obviously some magic going on under the covers that I don't
22
+ # understand.
23
+ class DateTime
24
+ def hour()
25
+ super
26
+ end
27
+ def min()
28
+ super
29
+ end
30
+ def sec()
31
+ super
32
+ end
33
+ def sec_fraction()
34
+ super
35
+ end
36
+ def offset()
37
+ super
38
+ end
39
+ end