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
data/ext/oj/validate.c ADDED
@@ -0,0 +1,46 @@
1
+ // Copyright (c) 2021, Peter Ohler, All rights reserved.
2
+
3
+ #include "parser.h"
4
+
5
+ static void noop(ojParser p) {
6
+ }
7
+
8
+ static VALUE option(ojParser p, const char *key, VALUE value) {
9
+ rb_raise(rb_eArgError, "%s is not an option for the validate delegate", key);
10
+ return Qnil;
11
+ }
12
+
13
+ static VALUE result(ojParser p) {
14
+ return Qnil;
15
+ }
16
+
17
+ static void dfree(ojParser p) {
18
+ }
19
+
20
+ static void mark(ojParser p) {
21
+ }
22
+
23
+ void oj_set_parser_validator(ojParser p) {
24
+ Funcs end = p->funcs + 3;
25
+ Funcs f;
26
+ p->ctx = NULL;
27
+
28
+ for (f = p->funcs; f < end; f++) {
29
+ f->add_null = noop;
30
+ f->add_true = noop;
31
+ f->add_false = noop;
32
+ f->add_int = noop;
33
+ f->add_float = noop;
34
+ f->add_big = noop;
35
+ f->add_str = noop;
36
+ f->open_array = noop;
37
+ f->close_array = noop;
38
+ f->open_object = noop;
39
+ f->close_object = noop;
40
+ }
41
+ p->option = option;
42
+ p->result = result;
43
+ p->free = dfree;
44
+ p->mark = mark;
45
+ p->start = noop;
46
+ }
data/ext/oj/wab.c ADDED
@@ -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[256] = "\
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
+ }
@@ -1,16 +1,14 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  require 'active_support/time'
3
4
 
4
5
  module Oj
5
-
6
6
  # Exists only to handle the ActiveSupport::TimeWithZone.
7
7
  class ActiveSupportHelper
8
-
9
8
  def self.createTimeWithZone(utc, zone)
10
9
  ActiveSupport::TimeWithZone.new(utc - utc.gmt_offset, ActiveSupport::TimeZone[zone])
11
10
  end
12
11
  end
13
-
14
12
  end
15
13
 
16
14
  Oj.register_odd(ActiveSupport::TimeWithZone, Oj::ActiveSupportHelper, :createTimeWithZone, :utc, 'time_zone.name')
data/lib/oj/bag.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module Oj
3
4
 
@@ -14,7 +15,7 @@ module Oj
14
15
  # @example Oj::Bag.new(:@x => 42, :@y => 57)
15
16
  # @param [Hash] args instance variable symbols and their values
16
17
  def initialize(args = {})
17
- args.each do |k,v|
18
+ args.each do |k, v|
18
19
  self.instance_variable_set(k, v)
19
20
  end
20
21
  end
@@ -25,6 +26,7 @@ module Oj
25
26
  # variable reader, otherwise false.
26
27
  def respond_to?(m)
27
28
  return true if super
29
+
28
30
  instance_variables.include?(:"@#{m}")
29
31
  end
30
32
 
@@ -36,8 +38,10 @@ module Oj
36
38
  # @raise [NoMethodError] if the instance variable is not defined.
37
39
  def method_missing(m, *args, &block)
38
40
  raise ArgumentError.new("wrong number of arguments (#{args.size} for 0) to method #{m}") unless args.nil? or args.empty?
41
+
39
42
  at_m = :"@#{m}"
40
43
  raise NoMethodError.new("undefined method #{m}", m) unless instance_variable_defined?(at_m)
44
+
41
45
  instance_variable_get(at_m)
42
46
  end
43
47
 
@@ -46,9 +50,11 @@ module Oj
46
50
  # @return [Boolean] true if each variable and value are the same, otherwise false.
47
51
  def eql?(other)
48
52
  return false if (other.nil? or self.class != other.class)
53
+
49
54
  ova = other.instance_variables
50
55
  iv = instance_variables
51
56
  return false if ova.size != iv.size
57
+
52
58
  iv.all? { |vid| instance_variable_get(vid) != other.instance_variable_get(vid) }
53
59
  end
54
60
  alias == eql?
@@ -64,6 +70,7 @@ module Oj
64
70
  classname = classname.to_s unless classname.is_a?(String)
65
71
  tokens = classname.split('::').map(&:to_sym)
66
72
  raise NameError.new("Invalid classname '#{classname}") if tokens.empty?
73
+
67
74
  m = Object
68
75
  tokens[0..-2].each do |sym|
69
76
  if m.const_defined?(sym)