oj 3.7.4 → 3.13.23

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 (147) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1360 -0
  3. data/README.md +31 -8
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +53 -72
  6. data/ext/oj/cache.c +326 -0
  7. data/ext/oj/cache.h +21 -0
  8. data/ext/oj/cache8.c +61 -64
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +37 -43
  11. data/ext/oj/circarray.h +16 -17
  12. data/ext/oj/code.c +165 -179
  13. data/ext/oj/code.h +27 -29
  14. data/ext/oj/compat.c +174 -194
  15. data/ext/oj/custom.c +790 -866
  16. data/ext/oj/debug.c +132 -0
  17. data/ext/oj/dump.c +848 -863
  18. data/ext/oj/dump.h +81 -67
  19. data/ext/oj/dump_compat.c +85 -123
  20. data/ext/oj/dump_leaf.c +100 -188
  21. data/ext/oj/dump_object.c +527 -656
  22. data/ext/oj/dump_strict.c +315 -338
  23. data/ext/oj/encode.h +7 -34
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +40 -29
  26. data/ext/oj/err.h +48 -48
  27. data/ext/oj/extconf.rb +17 -4
  28. data/ext/oj/fast.c +1073 -1088
  29. data/ext/oj/intern.c +298 -0
  30. data/ext/oj/intern.h +26 -0
  31. data/ext/oj/mimic_json.c +469 -436
  32. data/ext/oj/object.c +532 -599
  33. data/ext/oj/odd.c +154 -138
  34. data/ext/oj/odd.h +37 -38
  35. data/ext/oj/oj.c +1333 -986
  36. data/ext/oj/oj.h +336 -316
  37. data/ext/oj/parse.c +1002 -846
  38. data/ext/oj/parse.h +92 -87
  39. data/ext/oj/parser.c +1587 -0
  40. data/ext/oj/parser.h +102 -0
  41. data/ext/oj/rails.c +888 -878
  42. data/ext/oj/rails.h +11 -14
  43. data/ext/oj/reader.c +141 -147
  44. data/ext/oj/reader.h +73 -89
  45. data/ext/oj/resolve.c +41 -62
  46. data/ext/oj/resolve.h +7 -9
  47. data/ext/oj/rxclass.c +71 -75
  48. data/ext/oj/rxclass.h +18 -19
  49. data/ext/oj/saj.c +443 -486
  50. data/ext/oj/saj2.c +596 -0
  51. data/ext/oj/saj2.h +23 -0
  52. data/ext/oj/scp.c +88 -113
  53. data/ext/oj/sparse.c +787 -709
  54. data/ext/oj/stream_writer.c +133 -159
  55. data/ext/oj/strict.c +127 -118
  56. data/ext/oj/string_writer.c +230 -249
  57. data/ext/oj/trace.c +34 -41
  58. data/ext/oj/trace.h +19 -19
  59. data/ext/oj/usual.c +1207 -0
  60. data/ext/oj/usual.h +68 -0
  61. data/ext/oj/util.c +136 -0
  62. data/ext/oj/util.h +20 -0
  63. data/ext/oj/val_stack.c +60 -68
  64. data/ext/oj/val_stack.h +91 -129
  65. data/ext/oj/validate.c +46 -0
  66. data/ext/oj/wab.c +342 -353
  67. data/lib/oj/bag.rb +1 -0
  68. data/lib/oj/easy_hash.rb +5 -4
  69. data/lib/oj/error.rb +1 -1
  70. data/lib/oj/json.rb +1 -1
  71. data/lib/oj/mimic.rb +48 -14
  72. data/lib/oj/saj.rb +20 -6
  73. data/lib/oj/state.rb +9 -8
  74. data/lib/oj/version.rb +2 -2
  75. data/lib/oj.rb +0 -8
  76. data/pages/Compatibility.md +1 -1
  77. data/pages/JsonGem.md +15 -0
  78. data/pages/Modes.md +53 -46
  79. data/pages/Options.md +78 -11
  80. data/pages/Parser.md +309 -0
  81. data/pages/Rails.md +73 -22
  82. data/pages/Security.md +1 -1
  83. data/test/activerecord/result_test.rb +7 -2
  84. data/test/activesupport5/abstract_unit.rb +45 -0
  85. data/test/activesupport5/decoding_test.rb +68 -60
  86. data/test/activesupport5/encoding_test.rb +111 -96
  87. data/test/activesupport5/encoding_test_cases.rb +33 -25
  88. data/test/activesupport5/test_helper.rb +43 -21
  89. data/test/activesupport5/time_zone_test_helpers.rb +18 -3
  90. data/test/activesupport6/abstract_unit.rb +44 -0
  91. data/test/activesupport6/decoding_test.rb +133 -0
  92. data/test/activesupport6/encoding_test.rb +507 -0
  93. data/test/activesupport6/encoding_test_cases.rb +98 -0
  94. data/test/activesupport6/test_common.rb +17 -0
  95. data/test/activesupport6/test_helper.rb +163 -0
  96. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  97. data/test/activesupport7/abstract_unit.rb +49 -0
  98. data/test/activesupport7/decoding_test.rb +125 -0
  99. data/test/activesupport7/encoding_test.rb +486 -0
  100. data/test/activesupport7/encoding_test_cases.rb +104 -0
  101. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  102. data/test/bar.rb +6 -12
  103. data/test/baz.rb +16 -0
  104. data/test/bug.rb +16 -0
  105. data/test/foo.rb +69 -75
  106. data/test/helper.rb +16 -0
  107. data/test/json_gem/json_common_interface_test.rb +8 -3
  108. data/test/json_gem/json_generator_test.rb +21 -8
  109. data/test/json_gem/json_parser_test.rb +8 -1
  110. data/test/json_gem/test_helper.rb +12 -0
  111. data/test/mem.rb +33 -0
  112. data/test/perf.rb +1 -1
  113. data/test/perf_dump.rb +50 -0
  114. data/test/perf_once.rb +58 -0
  115. data/test/perf_parser.rb +189 -0
  116. data/test/perf_scp.rb +11 -10
  117. data/test/perf_strict.rb +17 -23
  118. data/test/prec.rb +23 -0
  119. data/test/sample_json.rb +1 -1
  120. data/test/test_compat.rb +46 -10
  121. data/test/test_custom.rb +145 -7
  122. data/test/test_fast.rb +62 -2
  123. data/test/test_file.rb +23 -7
  124. data/test/test_gc.rb +11 -0
  125. data/test/test_generate.rb +21 -0
  126. data/test/test_hash.rb +11 -1
  127. data/test/test_integer_range.rb +1 -2
  128. data/test/test_object.rb +43 -12
  129. data/test/test_parser.rb +11 -0
  130. data/test/test_parser_debug.rb +27 -0
  131. data/test/test_parser_saj.rb +335 -0
  132. data/test/test_parser_usual.rb +217 -0
  133. data/test/test_rails.rb +35 -0
  134. data/test/test_saj.rb +1 -1
  135. data/test/test_scp.rb +3 -5
  136. data/test/test_strict.rb +26 -1
  137. data/test/test_various.rb +86 -65
  138. data/test/test_wab.rb +2 -0
  139. data/test/test_writer.rb +19 -2
  140. data/test/tests.rb +10 -1
  141. data/test/tests_mimic.rb +9 -0
  142. data/test/tests_mimic_addition.rb +9 -0
  143. data/test/zoo.rb +13 -0
  144. metadata +63 -110
  145. data/ext/oj/hash.c +0 -163
  146. data/ext/oj/hash.h +0 -46
  147. data/ext/oj/hash_test.c +0 -512
data/ext/oj/parser.c ADDED
@@ -0,0 +1,1587 @@
1
+ // Copyright (c) 2020, 2021, Peter Ohler, All rights reserved.
2
+
3
+ #include "parser.h"
4
+
5
+ #include <fcntl.h>
6
+
7
+ #include "oj.h"
8
+
9
+ #define DEBUG 0
10
+
11
+ #define USE_THREAD_LIMIT 0
12
+ // #define USE_THREAD_LIMIT 100000
13
+ #define MAX_EXP 4932
14
+ // max in the pow_map which is the limit for double
15
+ #define MAX_POW 308
16
+
17
+ #define MIN_SLEEP (1000000000LL / (double)CLOCKS_PER_SEC)
18
+ // 9,223,372,036,854,775,807
19
+ #define BIG_LIMIT LLONG_MAX / 10
20
+ #define FRAC_LIMIT 10000000000000000ULL
21
+
22
+ // Give better performance with indented JSON but worse with unindented.
23
+ //#define SPACE_JUMP
24
+
25
+ enum {
26
+ SKIP_CHAR = 'a',
27
+ SKIP_NEWLINE = 'b',
28
+ VAL_NULL = 'c',
29
+ VAL_TRUE = 'd',
30
+ VAL_FALSE = 'e',
31
+ VAL_NEG = 'f',
32
+ VAL0 = 'g',
33
+ VAL_DIGIT = 'h',
34
+ VAL_QUOTE = 'i',
35
+ OPEN_ARRAY = 'k',
36
+ OPEN_OBJECT = 'l',
37
+ CLOSE_ARRAY = 'm',
38
+ CLOSE_OBJECT = 'n',
39
+ AFTER_COMMA = 'o',
40
+ KEY_QUOTE = 'p',
41
+ COLON_COLON = 'q',
42
+ NUM_SPC = 'r',
43
+ NUM_NEWLINE = 's',
44
+ NUM_DOT = 't',
45
+ NUM_COMMA = 'u',
46
+ NUM_FRAC = 'v',
47
+ FRAC_E = 'w',
48
+ EXP_SIGN = 'x',
49
+ EXP_DIGIT = 'y',
50
+ STR_QUOTE = 'z',
51
+ NEG_DIGIT = '-',
52
+ STR_SLASH = 'A',
53
+ ESC_OK = 'B',
54
+ BIG_DIGIT = 'C',
55
+ BIG_DOT = 'D',
56
+ U_OK = 'E',
57
+ TOKEN_OK = 'F',
58
+ NUM_CLOSE_OBJECT = 'G',
59
+ NUM_CLOSE_ARRAY = 'H',
60
+ BIG_FRAC = 'I',
61
+ BIG_E = 'J',
62
+ BIG_EXP_SIGN = 'K',
63
+ BIG_EXP = 'L',
64
+ UTF1 = 'M', // expect 1 more follow byte
65
+ NUM_DIGIT = 'N',
66
+ NUM_ZERO = 'O',
67
+ UTF2 = 'P', // expect 2 more follow byte
68
+ UTF3 = 'Q', // expect 3 more follow byte
69
+ STR_OK = 'R',
70
+ UTFX = 'S', // following bytes
71
+ ESC_U = 'U',
72
+ CHAR_ERR = '.',
73
+ DONE = 'X',
74
+ };
75
+
76
+ /*
77
+ 0123456789abcdef0123456789abcdef */
78
+ static const char value_map[257] = "\
79
+ X........ab..a..................\
80
+ a.i..........f..ghhhhhhhhh......\
81
+ ...........................k.m..\
82
+ ......e.......c.....d......l.n..\
83
+ ................................\
84
+ ................................\
85
+ ................................\
86
+ ................................v";
87
+
88
+ static const char null_map[257] = "\
89
+ ................................\
90
+ ............o...................\
91
+ ................................\
92
+ ............F........F..........\
93
+ ................................\
94
+ ................................\
95
+ ................................\
96
+ ................................N";
97
+
98
+ static const char true_map[257] = "\
99
+ ................................\
100
+ ............o...................\
101
+ ................................\
102
+ .....F............F..F..........\
103
+ ................................\
104
+ ................................\
105
+ ................................\
106
+ ................................T";
107
+
108
+ static const char false_map[257] = "\
109
+ ................................\
110
+ ............o...................\
111
+ ................................\
112
+ .F...F......F......F............\
113
+ ................................\
114
+ ................................\
115
+ ................................\
116
+ ................................F";
117
+
118
+ static const char comma_map[257] = "\
119
+ .........ab..a..................\
120
+ a.i..........f..ghhhhhhhhh......\
121
+ ...........................k....\
122
+ ......e.......c.....d......l....\
123
+ ................................\
124
+ ................................\
125
+ ................................\
126
+ ................................,";
127
+
128
+ static const char after_map[257] = "\
129
+ X........ab..a..................\
130
+ a...........o...................\
131
+ .............................m..\
132
+ .............................n..\
133
+ ................................\
134
+ ................................\
135
+ ................................\
136
+ ................................a";
137
+
138
+ static const char key1_map[257] = "\
139
+ .........ab..a..................\
140
+ a.p.............................\
141
+ ................................\
142
+ .............................n..\
143
+ ................................\
144
+ ................................\
145
+ ................................\
146
+ ................................K";
147
+
148
+ static const char key_map[257] = "\
149
+ .........ab..a..................\
150
+ a.p.............................\
151
+ ................................\
152
+ ................................\
153
+ ................................\
154
+ ................................\
155
+ ................................\
156
+ ................................k";
157
+
158
+ static const char colon_map[257] = "\
159
+ .........ab..a..................\
160
+ a.........................q.....\
161
+ ................................\
162
+ ................................\
163
+ ................................\
164
+ ................................\
165
+ ................................\
166
+ ................................:";
167
+
168
+ static const char neg_map[257] = "\
169
+ ................................\
170
+ ................O---------......\
171
+ ................................\
172
+ ................................\
173
+ ................................\
174
+ ................................\
175
+ ................................\
176
+ ................................-";
177
+
178
+ static const char zero_map[257] = "\
179
+ .........rs..r..................\
180
+ r...........u.t.................\
181
+ .............................H..\
182
+ .............................G..\
183
+ ................................\
184
+ ................................\
185
+ ................................\
186
+ ................................0";
187
+
188
+ static const char digit_map[257] = "\
189
+ .........rs..r..................\
190
+ r...........u.t.NNNNNNNNNN......\
191
+ .....w.......................H..\
192
+ .....w.......................G..\
193
+ ................................\
194
+ ................................\
195
+ ................................\
196
+ ................................d";
197
+
198
+ static const char dot_map[257] = "\
199
+ ................................\
200
+ ................vvvvvvvvvv......\
201
+ ................................\
202
+ ................................\
203
+ ................................\
204
+ ................................\
205
+ ................................\
206
+ .................................";
207
+
208
+ static const char frac_map[257] = "\
209
+ .........rs..r..................\
210
+ r...........u...vvvvvvvvvv......\
211
+ .....w.......................H..\
212
+ .....w.......................G..\
213
+ ................................\
214
+ ................................\
215
+ ................................\
216
+ ................................f";
217
+
218
+ static const char exp_sign_map[257] = "\
219
+ ................................\
220
+ ...........x.x..yyyyyyyyyy......\
221
+ ................................\
222
+ ................................\
223
+ ................................\
224
+ ................................\
225
+ ................................\
226
+ ................................x";
227
+
228
+ static const char exp_zero_map[257] = "\
229
+ ................................\
230
+ ................yyyyyyyyyy......\
231
+ ................................\
232
+ ................................\
233
+ ................................\
234
+ ................................\
235
+ ................................\
236
+ ................................z";
237
+
238
+ static const char exp_map[257] = "\
239
+ .........rs..r..................\
240
+ r...........u...yyyyyyyyyy......\
241
+ .............................H..\
242
+ .............................G..\
243
+ ................................\
244
+ ................................\
245
+ ................................\
246
+ ................................X";
247
+
248
+ static const char big_digit_map[257] = "\
249
+ .........rs..r..................\
250
+ r...........u.D.CCCCCCCCCC......\
251
+ .....J.......................H..\
252
+ .....J.......................G..\
253
+ ................................\
254
+ ................................\
255
+ ................................\
256
+ ................................D";
257
+
258
+ static const char big_dot_map[257] = "\
259
+ ................................\
260
+ ................IIIIIIIIII......\
261
+ ................................\
262
+ ................................\
263
+ ................................\
264
+ ................................\
265
+ ................................\
266
+ ................................o";
267
+
268
+ static const char big_frac_map[257] = "\
269
+ .........rs..r..................\
270
+ r...........u...IIIIIIIIII......\
271
+ .....J.......................H..\
272
+ .....J.......................G..\
273
+ ................................\
274
+ ................................\
275
+ ................................\
276
+ ................................g";
277
+
278
+ static const char big_exp_sign_map[257] = "\
279
+ ................................\
280
+ ...........K.K..LLLLLLLLLL......\
281
+ ................................\
282
+ ................................\
283
+ ................................\
284
+ ................................\
285
+ ................................\
286
+ ................................B";
287
+
288
+ static const char big_exp_zero_map[257] = "\
289
+ ................................\
290
+ ................LLLLLLLLLL......\
291
+ ................................\
292
+ ................................\
293
+ ................................\
294
+ ................................\
295
+ ................................\
296
+ ................................Z";
297
+
298
+ static const char big_exp_map[257] = "\
299
+ .........rs..r..................\
300
+ r...........u...LLLLLLLLLL......\
301
+ .............................H..\
302
+ .............................G..\
303
+ ................................\
304
+ ................................\
305
+ ................................\
306
+ ................................Y";
307
+
308
+ static const char string_map[257] = "\
309
+ ................................\
310
+ RRzRRRRRRRRRRRRRRRRRRRRRRRRRRRRR\
311
+ RRRRRRRRRRRRRRRRRRRRRRRRRRRRARRR\
312
+ RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR\
313
+ ................................\
314
+ ................................\
315
+ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\
316
+ PPPPPPPPPPPPPPPPQQQQQQQQ........s";
317
+
318
+ static const char esc_map[257] = "\
319
+ ................................\
320
+ ..B............B................\
321
+ ............................B...\
322
+ ..B...B.......B...B.BU..........\
323
+ ................................\
324
+ ................................\
325
+ ................................\
326
+ ................................~";
327
+
328
+ static const char esc_byte_map[257] = "\
329
+ ................................\
330
+ ..\"............/................\
331
+ ............................\\...\
332
+ ..\b...\f.......\n...\r.\t..........\
333
+ ................................\
334
+ ................................\
335
+ ................................\
336
+ ................................b";
337
+
338
+ static const char u_map[257] = "\
339
+ ................................\
340
+ ................EEEEEEEEEE......\
341
+ .EEEEEE.........................\
342
+ .EEEEEE.........................\
343
+ ................................\
344
+ ................................\
345
+ ................................\
346
+ ................................u";
347
+
348
+ static const char utf_map[257] = "\
349
+ ................................\
350
+ ................................\
351
+ ................................\
352
+ ................................\
353
+ SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\
354
+ SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\
355
+ ................................\
356
+ ................................8";
357
+
358
+ static const char space_map[257] = "\
359
+ .........ab..a..................\
360
+ a...............................\
361
+ ................................\
362
+ ................................\
363
+ ................................\
364
+ ................................\
365
+ ................................\
366
+ ................................S";
367
+
368
+ static const char trail_map[257] = "\
369
+ .........ab..a..................\
370
+ a...............................\
371
+ ................................\
372
+ ................................\
373
+ ................................\
374
+ ................................\
375
+ ................................\
376
+ ................................R";
377
+
378
+ static const byte hex_map[256] = "\
379
+ ................................\
380
+ ................\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09......\
381
+ .\x0a\x0b\x0c\x0d\x0e\x0f.........................\
382
+ .\x0a\x0b\x0c\x0d\x0e\x0f.........................\
383
+ ................................\
384
+ ................................\
385
+ ................................\
386
+ ................................";
387
+
388
+ static long double pow_map[309] = {
389
+ 1.0L, 1.0e1L, 1.0e2L, 1.0e3L, 1.0e4L, 1.0e5L, 1.0e6L, 1.0e7L, 1.0e8L, 1.0e9L, 1.0e10L,
390
+ 1.0e11L, 1.0e12L, 1.0e13L, 1.0e14L, 1.0e15L, 1.0e16L, 1.0e17L, 1.0e18L, 1.0e19L, 1.0e20L, 1.0e21L,
391
+ 1.0e22L, 1.0e23L, 1.0e24L, 1.0e25L, 1.0e26L, 1.0e27L, 1.0e28L, 1.0e29L, 1.0e30L, 1.0e31L, 1.0e32L,
392
+ 1.0e33L, 1.0e34L, 1.0e35L, 1.0e36L, 1.0e37L, 1.0e38L, 1.0e39L, 1.0e40L, 1.0e41L, 1.0e42L, 1.0e43L,
393
+ 1.0e44L, 1.0e45L, 1.0e46L, 1.0e47L, 1.0e48L, 1.0e49L, 1.0e50L, 1.0e51L, 1.0e52L, 1.0e53L, 1.0e54L,
394
+ 1.0e55L, 1.0e56L, 1.0e57L, 1.0e58L, 1.0e59L, 1.0e60L, 1.0e61L, 1.0e62L, 1.0e63L, 1.0e64L, 1.0e65L,
395
+ 1.0e66L, 1.0e67L, 1.0e68L, 1.0e69L, 1.0e70L, 1.0e71L, 1.0e72L, 1.0e73L, 1.0e74L, 1.0e75L, 1.0e76L,
396
+ 1.0e77L, 1.0e78L, 1.0e79L, 1.0e80L, 1.0e81L, 1.0e82L, 1.0e83L, 1.0e84L, 1.0e85L, 1.0e86L, 1.0e87L,
397
+ 1.0e88L, 1.0e89L, 1.0e90L, 1.0e91L, 1.0e92L, 1.0e93L, 1.0e94L, 1.0e95L, 1.0e96L, 1.0e97L, 1.0e98L,
398
+ 1.0e99L, 1.0e100L, 1.0e101L, 1.0e102L, 1.0e103L, 1.0e104L, 1.0e105L, 1.0e106L, 1.0e107L, 1.0e108L, 1.0e109L,
399
+ 1.0e110L, 1.0e111L, 1.0e112L, 1.0e113L, 1.0e114L, 1.0e115L, 1.0e116L, 1.0e117L, 1.0e118L, 1.0e119L, 1.0e120L,
400
+ 1.0e121L, 1.0e122L, 1.0e123L, 1.0e124L, 1.0e125L, 1.0e126L, 1.0e127L, 1.0e128L, 1.0e129L, 1.0e130L, 1.0e131L,
401
+ 1.0e132L, 1.0e133L, 1.0e134L, 1.0e135L, 1.0e136L, 1.0e137L, 1.0e138L, 1.0e139L, 1.0e140L, 1.0e141L, 1.0e142L,
402
+ 1.0e143L, 1.0e144L, 1.0e145L, 1.0e146L, 1.0e147L, 1.0e148L, 1.0e149L, 1.0e150L, 1.0e151L, 1.0e152L, 1.0e153L,
403
+ 1.0e154L, 1.0e155L, 1.0e156L, 1.0e157L, 1.0e158L, 1.0e159L, 1.0e160L, 1.0e161L, 1.0e162L, 1.0e163L, 1.0e164L,
404
+ 1.0e165L, 1.0e166L, 1.0e167L, 1.0e168L, 1.0e169L, 1.0e170L, 1.0e171L, 1.0e172L, 1.0e173L, 1.0e174L, 1.0e175L,
405
+ 1.0e176L, 1.0e177L, 1.0e178L, 1.0e179L, 1.0e180L, 1.0e181L, 1.0e182L, 1.0e183L, 1.0e184L, 1.0e185L, 1.0e186L,
406
+ 1.0e187L, 1.0e188L, 1.0e189L, 1.0e190L, 1.0e191L, 1.0e192L, 1.0e193L, 1.0e194L, 1.0e195L, 1.0e196L, 1.0e197L,
407
+ 1.0e198L, 1.0e199L, 1.0e200L, 1.0e201L, 1.0e202L, 1.0e203L, 1.0e204L, 1.0e205L, 1.0e206L, 1.0e207L, 1.0e208L,
408
+ 1.0e209L, 1.0e210L, 1.0e211L, 1.0e212L, 1.0e213L, 1.0e214L, 1.0e215L, 1.0e216L, 1.0e217L, 1.0e218L, 1.0e219L,
409
+ 1.0e220L, 1.0e221L, 1.0e222L, 1.0e223L, 1.0e224L, 1.0e225L, 1.0e226L, 1.0e227L, 1.0e228L, 1.0e229L, 1.0e230L,
410
+ 1.0e231L, 1.0e232L, 1.0e233L, 1.0e234L, 1.0e235L, 1.0e236L, 1.0e237L, 1.0e238L, 1.0e239L, 1.0e240L, 1.0e241L,
411
+ 1.0e242L, 1.0e243L, 1.0e244L, 1.0e245L, 1.0e246L, 1.0e247L, 1.0e248L, 1.0e249L, 1.0e250L, 1.0e251L, 1.0e252L,
412
+ 1.0e253L, 1.0e254L, 1.0e255L, 1.0e256L, 1.0e257L, 1.0e258L, 1.0e259L, 1.0e260L, 1.0e261L, 1.0e262L, 1.0e263L,
413
+ 1.0e264L, 1.0e265L, 1.0e266L, 1.0e267L, 1.0e268L, 1.0e269L, 1.0e270L, 1.0e271L, 1.0e272L, 1.0e273L, 1.0e274L,
414
+ 1.0e275L, 1.0e276L, 1.0e277L, 1.0e278L, 1.0e279L, 1.0e280L, 1.0e281L, 1.0e282L, 1.0e283L, 1.0e284L, 1.0e285L,
415
+ 1.0e286L, 1.0e287L, 1.0e288L, 1.0e289L, 1.0e290L, 1.0e291L, 1.0e292L, 1.0e293L, 1.0e294L, 1.0e295L, 1.0e296L,
416
+ 1.0e297L, 1.0e298L, 1.0e299L, 1.0e300L, 1.0e301L, 1.0e302L, 1.0e303L, 1.0e304L, 1.0e305L, 1.0e306L, 1.0e307L,
417
+ 1.0e308L};
418
+
419
+ static VALUE parser_class;
420
+
421
+ // Works with extended unicode as well. \Uffffffff if support is desired in
422
+ // the future.
423
+ static size_t unicodeToUtf8(uint32_t code, byte *buf) {
424
+ byte *start = buf;
425
+
426
+ if (0x0000007F >= code) {
427
+ *buf++ = (byte)code;
428
+ } else if (0x000007FF >= code) {
429
+ *buf++ = 0xC0 | (code >> 6);
430
+ *buf++ = 0x80 | (0x3F & code);
431
+ } else if (0x0000FFFF >= code) {
432
+ *buf++ = 0xE0 | (code >> 12);
433
+ *buf++ = 0x80 | ((code >> 6) & 0x3F);
434
+ *buf++ = 0x80 | (0x3F & code);
435
+ } else if (0x001FFFFF >= code) {
436
+ *buf++ = 0xF0 | (code >> 18);
437
+ *buf++ = 0x80 | ((code >> 12) & 0x3F);
438
+ *buf++ = 0x80 | ((code >> 6) & 0x3F);
439
+ *buf++ = 0x80 | (0x3F & code);
440
+ } else if (0x03FFFFFF >= code) {
441
+ *buf++ = 0xF8 | (code >> 24);
442
+ *buf++ = 0x80 | ((code >> 18) & 0x3F);
443
+ *buf++ = 0x80 | ((code >> 12) & 0x3F);
444
+ *buf++ = 0x80 | ((code >> 6) & 0x3F);
445
+ *buf++ = 0x80 | (0x3F & code);
446
+ } else if (0x7FFFFFFF >= code) {
447
+ *buf++ = 0xFC | (code >> 30);
448
+ *buf++ = 0x80 | ((code >> 24) & 0x3F);
449
+ *buf++ = 0x80 | ((code >> 18) & 0x3F);
450
+ *buf++ = 0x80 | ((code >> 12) & 0x3F);
451
+ *buf++ = 0x80 | ((code >> 6) & 0x3F);
452
+ *buf++ = 0x80 | (0x3F & code);
453
+ }
454
+ return buf - start;
455
+ }
456
+
457
+ static void parser_reset(ojParser p) {
458
+ p->reader = 0;
459
+ memset(&p->num, 0, sizeof(p->num));
460
+ buf_reset(&p->key);
461
+ buf_reset(&p->buf);
462
+ p->map = value_map;
463
+ p->next_map = NULL;
464
+ p->depth = 0;
465
+ }
466
+
467
+ static void parse_error(ojParser p, const char *fmt, ...) {
468
+ va_list ap;
469
+ char buf[256];
470
+
471
+ va_start(ap, fmt);
472
+ vsnprintf(buf, sizeof(buf), fmt, ap);
473
+ va_end(ap);
474
+ rb_raise(oj_json_parser_error_class, "%s at %ld:%ld", buf, p->line, p->col);
475
+ }
476
+
477
+ static void byte_error(ojParser p, byte b) {
478
+ switch (p->map[256]) {
479
+ case 'N': // null_map
480
+ parse_error(p, "expected null");
481
+ break;
482
+ case 'T': // true_map
483
+ parse_error(p, "expected true");
484
+ break;
485
+ case 'F': // false_map
486
+ parse_error(p, "expected false");
487
+ break;
488
+ case 's': // string_map
489
+ parse_error(p, "invalid JSON character 0x%02x", b);
490
+ break;
491
+ default: parse_error(p, "unexpected character '%c' in '%c' mode", b, p->map[256]); break;
492
+ }
493
+ }
494
+
495
+ static void calc_num(ojParser p) {
496
+ switch (p->type) {
497
+ case OJ_INT:
498
+ if (p->num.neg) {
499
+ p->num.fixnum = -p->num.fixnum;
500
+ p->num.neg = false;
501
+ }
502
+ p->funcs[p->stack[p->depth]].add_int(p);
503
+ break;
504
+ case OJ_DECIMAL: {
505
+ long double d = (long double)p->num.fixnum;
506
+
507
+ if (p->num.neg) {
508
+ d = -d;
509
+ }
510
+ if (0 < p->num.shift) {
511
+ d /= pow_map[p->num.shift];
512
+ }
513
+ if (0 < p->num.exp) {
514
+ long double x;
515
+
516
+ if (MAX_POW < p->num.exp) {
517
+ x = powl(10.0L, (long double)p->num.exp);
518
+ } else {
519
+ x = pow_map[p->num.exp];
520
+ }
521
+ if (p->num.exp_neg) {
522
+ d /= x;
523
+ } else {
524
+ d *= x;
525
+ }
526
+ }
527
+ p->num.dub = d;
528
+ p->funcs[p->stack[p->depth]].add_float(p);
529
+ break;
530
+ }
531
+ case OJ_BIG: p->funcs[p->stack[p->depth]].add_big(p);
532
+ default:
533
+ // nothing to do
534
+ break;
535
+ }
536
+ p->type = OJ_NONE;
537
+ }
538
+
539
+ static void big_change(ojParser p) {
540
+ char buf[32];
541
+ int64_t i = p->num.fixnum;
542
+ int len = 0;
543
+
544
+ buf[sizeof(buf) - 1] = '\0';
545
+ p->buf.tail = p->buf.head;
546
+ switch (p->type) {
547
+ case OJ_INT:
548
+ // If an int then it will fit in the num.raw so no need to check length;
549
+ for (len = sizeof(buf) - 1; 0 < i; len--, i /= 10) {
550
+ buf[len] = '0' + (i % 10);
551
+ }
552
+ if (p->num.neg) {
553
+ buf[len] = '-';
554
+ len--;
555
+ }
556
+ buf_append_string(&p->buf, buf + len + 1, sizeof(buf) - len - 1);
557
+ p->type = OJ_BIG;
558
+ break;
559
+ case OJ_DECIMAL: {
560
+ int shift = p->num.shift;
561
+
562
+ for (len = sizeof(buf) - 1; 0 < i; len--, i /= 10, shift--) {
563
+ if (0 == shift) {
564
+ buf[len] = '.';
565
+ len--;
566
+ }
567
+ buf[len] = '0' + (i % 10);
568
+ }
569
+ if (p->num.neg) {
570
+ buf[len] = '-';
571
+ len--;
572
+ }
573
+ buf_append_string(&p->buf, buf + len + 1, sizeof(buf) - len - 1);
574
+ if (0 < p->num.exp) {
575
+ int x = p->num.exp;
576
+ int d, div;
577
+ bool started = false;
578
+
579
+ buf_append(&p->buf, 'e');
580
+ if (0 < p->num.exp_neg) {
581
+ buf_append(&p->buf, '-');
582
+ }
583
+ for (div = 1000; 0 < div; div /= 10) {
584
+ d = x / div % 10;
585
+ if (started || 0 < d) {
586
+ buf_append(&p->buf, '0' + d);
587
+ }
588
+ }
589
+ }
590
+ p->type = OJ_BIG;
591
+ break;
592
+ }
593
+ default: break;
594
+ }
595
+ }
596
+
597
+ static void parse(ojParser p, const byte *json) {
598
+ const byte *start;
599
+ const byte *b = json;
600
+ int i;
601
+
602
+ p->line = 1;
603
+ p->col = -1;
604
+ #if DEBUG
605
+ printf("*** parse - mode: %c %s\n", p->map[256], (const char *)json);
606
+ #endif
607
+ for (; '\0' != *b; b++) {
608
+ #if DEBUG
609
+ printf("*** parse - mode: %c %02x %s => %c\n", p->map[256], *b, b, p->map[*b]);
610
+ #endif
611
+ switch (p->map[*b]) {
612
+ case SKIP_NEWLINE:
613
+ p->line++;
614
+ p->col = b - json;
615
+ b++;
616
+ #ifdef SPACE_JUMP
617
+ // for (uint32_t *sj = (uint32_t*)b; 0x20202020 == *sj; sj++) { b += 4; }
618
+ for (uint16_t *sj = (uint16_t *)b; 0x2020 == *sj; sj++) {
619
+ b += 2;
620
+ }
621
+ #endif
622
+ for (; SKIP_CHAR == space_map[*b]; b++) {
623
+ }
624
+ b--;
625
+ break;
626
+ case COLON_COLON: p->map = value_map; break;
627
+ case SKIP_CHAR: break;
628
+ case KEY_QUOTE:
629
+ b++;
630
+ p->key.tail = p->key.head;
631
+ start = b;
632
+ for (; STR_OK == string_map[*b]; b++) {
633
+ }
634
+ buf_append_string(&p->key, (const char *)start, b - start);
635
+ if ('"' == *b) {
636
+ p->map = colon_map;
637
+ break;
638
+ }
639
+ b--;
640
+ p->map = string_map;
641
+ p->next_map = colon_map;
642
+ break;
643
+ case AFTER_COMMA:
644
+ if (0 < p->depth && OBJECT_FUN == p->stack[p->depth]) {
645
+ p->map = key_map;
646
+ } else {
647
+ p->map = comma_map;
648
+ }
649
+ break;
650
+ case VAL_QUOTE:
651
+ b++;
652
+ start = b;
653
+ p->buf.tail = p->buf.head;
654
+ for (; STR_OK == string_map[*b]; b++) {
655
+ }
656
+ buf_append_string(&p->buf, (const char *)start, b - start);
657
+ if ('"' == *b) {
658
+ p->cur = b - json;
659
+ p->funcs[p->stack[p->depth]].add_str(p);
660
+ p->map = (0 == p->depth) ? value_map : after_map;
661
+ break;
662
+ }
663
+ b--;
664
+ p->map = string_map;
665
+ p->next_map = (0 == p->depth) ? value_map : after_map;
666
+ break;
667
+ case OPEN_OBJECT:
668
+ p->cur = b - json;
669
+ p->funcs[p->stack[p->depth]].open_object(p);
670
+ p->depth++;
671
+ p->stack[p->depth] = OBJECT_FUN;
672
+ p->map = key1_map;
673
+ break;
674
+ case NUM_CLOSE_OBJECT:
675
+ p->cur = b - json;
676
+ calc_num(p);
677
+ // flow through
678
+ case CLOSE_OBJECT:
679
+ p->map = (1 == p->depth) ? value_map : after_map;
680
+ if (p->depth <= 0 || OBJECT_FUN != p->stack[p->depth]) {
681
+ p->col = b - json - p->col + 1;
682
+ parse_error(p, "unexpected object close");
683
+ return;
684
+ }
685
+ p->depth--;
686
+ p->cur = b - json;
687
+ p->funcs[p->stack[p->depth]].close_object(p);
688
+ break;
689
+ case OPEN_ARRAY:
690
+ p->cur = b - json;
691
+ p->funcs[p->stack[p->depth]].open_array(p);
692
+ p->depth++;
693
+ p->stack[p->depth] = ARRAY_FUN;
694
+ p->map = value_map;
695
+ break;
696
+ case NUM_CLOSE_ARRAY:
697
+ p->cur = b - json;
698
+ calc_num(p);
699
+ // flow through
700
+ case CLOSE_ARRAY:
701
+ p->map = (1 == p->depth) ? value_map : after_map;
702
+ if (p->depth <= 0 || ARRAY_FUN != p->stack[p->depth]) {
703
+ p->col = b - json - p->col + 1;
704
+ parse_error(p, "unexpected array close");
705
+ return;
706
+ }
707
+ p->depth--;
708
+ p->cur = b - json;
709
+ p->funcs[p->stack[p->depth]].close_array(p);
710
+ break;
711
+ case NUM_COMMA:
712
+ p->cur = b - json;
713
+ calc_num(p);
714
+ if (0 < p->depth && OBJECT_FUN == p->stack[p->depth]) {
715
+ p->map = key_map;
716
+ } else {
717
+ p->map = comma_map;
718
+ }
719
+ break;
720
+ case VAL0:
721
+ p->type = OJ_INT;
722
+ p->num.fixnum = 0;
723
+ p->num.neg = false;
724
+ p->num.shift = 0;
725
+ p->num.len = 0;
726
+ p->num.exp = 0;
727
+ p->num.exp_neg = false;
728
+ p->map = zero_map;
729
+ break;
730
+ case VAL_NEG:
731
+ p->type = OJ_INT;
732
+ p->num.fixnum = 0;
733
+ p->num.neg = true;
734
+ p->num.shift = 0;
735
+ p->num.len = 0;
736
+ p->num.exp = 0;
737
+ p->num.exp_neg = false;
738
+ p->map = neg_map;
739
+ break;
740
+ ;
741
+ case VAL_DIGIT:
742
+ p->type = OJ_INT;
743
+ p->num.fixnum = 0;
744
+ p->num.neg = false;
745
+ p->num.shift = 0;
746
+ p->num.exp = 0;
747
+ p->num.exp_neg = false;
748
+ p->num.len = 0;
749
+ p->map = digit_map;
750
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
751
+ uint64_t x = (uint64_t)p->num.fixnum * 10 + (uint64_t)(*b - '0');
752
+
753
+ // Tried just checking for an int less than zero but that
754
+ // fails when optimization is on for some reason with the
755
+ // clang compiler so us a bit mask instead.
756
+ if (x < BIG_LIMIT) {
757
+ p->num.fixnum = (int64_t)x;
758
+ } else {
759
+ big_change(p);
760
+ p->map = big_digit_map;
761
+ break;
762
+ }
763
+ }
764
+ b--;
765
+ break;
766
+ case NUM_DIGIT:
767
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
768
+ uint64_t x = p->num.fixnum * 10 + (uint64_t)(*b - '0');
769
+
770
+ if (x < BIG_LIMIT) {
771
+ p->num.fixnum = (int64_t)x;
772
+ } else {
773
+ big_change(p);
774
+ p->map = big_digit_map;
775
+ break;
776
+ }
777
+ }
778
+ b--;
779
+ break;
780
+ case NUM_DOT:
781
+ p->type = OJ_DECIMAL;
782
+ p->map = dot_map;
783
+ break;
784
+ case NUM_FRAC:
785
+ p->map = frac_map;
786
+ for (; NUM_FRAC == frac_map[*b]; b++) {
787
+ uint64_t x = p->num.fixnum * 10 + (uint64_t)(*b - '0');
788
+
789
+ if (x < FRAC_LIMIT) {
790
+ p->num.fixnum = (int64_t)x;
791
+ p->num.shift++;
792
+ } else {
793
+ big_change(p);
794
+ p->map = big_frac_map;
795
+ break;
796
+ }
797
+ }
798
+ b--;
799
+ break;
800
+ case FRAC_E:
801
+ p->type = OJ_DECIMAL;
802
+ p->map = exp_sign_map;
803
+ break;
804
+ case NUM_ZERO: p->map = zero_map; break;
805
+ case NEG_DIGIT:
806
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
807
+ uint64_t x = p->num.fixnum * 10 + (uint64_t)(*b - '0');
808
+
809
+ if (x < BIG_LIMIT) {
810
+ p->num.fixnum = (int64_t)x;
811
+ } else {
812
+ big_change(p);
813
+ p->map = big_digit_map;
814
+ break;
815
+ }
816
+ }
817
+ b--;
818
+ p->map = digit_map;
819
+ break;
820
+ case EXP_SIGN:
821
+ p->num.exp_neg = ('-' == *b);
822
+ p->map = exp_zero_map;
823
+ break;
824
+ case EXP_DIGIT:
825
+ p->map = exp_map;
826
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
827
+ int16_t x = p->num.exp * 10 + (int16_t)(*b - '0');
828
+
829
+ if (x <= MAX_EXP) {
830
+ p->num.exp = x;
831
+ } else {
832
+ big_change(p);
833
+ p->map = big_exp_map;
834
+ break;
835
+ }
836
+ }
837
+ b--;
838
+ break;
839
+ case BIG_DIGIT:
840
+ start = b;
841
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
842
+ }
843
+ buf_append_string(&p->buf, (const char *)start, b - start);
844
+ b--;
845
+ break;
846
+ case BIG_DOT:
847
+ buf_append(&p->buf, '.');
848
+ p->map = big_dot_map;
849
+ break;
850
+ case BIG_FRAC:
851
+ p->map = big_frac_map;
852
+ start = b;
853
+ for (; NUM_FRAC == frac_map[*b]; b++) {
854
+ }
855
+ buf_append_string(&p->buf, (const char *)start, b - start);
856
+ b--;
857
+ break;
858
+ case BIG_E:
859
+ buf_append(&p->buf, *b);
860
+ p->map = big_exp_sign_map;
861
+ break;
862
+ case BIG_EXP_SIGN:
863
+ buf_append(&p->buf, *b);
864
+ p->map = big_exp_zero_map;
865
+ break;
866
+ case BIG_EXP:
867
+ start = b;
868
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
869
+ }
870
+ buf_append_string(&p->buf, (const char *)start, b - start);
871
+ b--;
872
+ p->map = big_exp_map;
873
+ break;
874
+ case NUM_SPC:
875
+ p->cur = b - json;
876
+ calc_num(p);
877
+ break;
878
+ case NUM_NEWLINE:
879
+ p->cur = b - json;
880
+ calc_num(p);
881
+ b++;
882
+ #ifdef SPACE_JUMP
883
+ // for (uint32_t *sj = (uint32_t*)b; 0x20202020 == *sj; sj++) { b += 4; }
884
+ for (uint16_t *sj = (uint16_t *)b; 0x2020 == *sj; sj++) {
885
+ b += 2;
886
+ }
887
+ #endif
888
+ for (; SKIP_CHAR == space_map[*b]; b++) {
889
+ }
890
+ b--;
891
+ break;
892
+ case STR_OK:
893
+ start = b;
894
+ for (; STR_OK == string_map[*b]; b++) {
895
+ }
896
+ if (':' == p->next_map[256]) {
897
+ buf_append_string(&p->key, (const char *)start, b - start);
898
+ } else {
899
+ buf_append_string(&p->buf, (const char *)start, b - start);
900
+ }
901
+ if ('"' == *b) {
902
+ p->cur = b - json;
903
+ p->funcs[p->stack[p->depth]].add_str(p);
904
+ p->map = p->next_map;
905
+ break;
906
+ }
907
+ b--;
908
+ break;
909
+ case STR_SLASH: p->map = esc_map; break;
910
+ case STR_QUOTE:
911
+ p->cur = b - json;
912
+ p->funcs[p->stack[p->depth]].add_str(p);
913
+ p->map = p->next_map;
914
+ break;
915
+ case ESC_U:
916
+ p->map = u_map;
917
+ p->ri = 0;
918
+ p->ucode = 0;
919
+ break;
920
+ case U_OK:
921
+ p->ri++;
922
+ p->ucode = p->ucode << 4 | (uint32_t)hex_map[*b];
923
+ if (4 <= p->ri) {
924
+ byte utf8[8];
925
+ size_t ulen = unicodeToUtf8(p->ucode, utf8);
926
+
927
+ if (0 < ulen) {
928
+ if (':' == p->next_map[256]) {
929
+ buf_append_string(&p->key, (const char *)utf8, ulen);
930
+ } else {
931
+ buf_append_string(&p->buf, (const char *)utf8, ulen);
932
+ }
933
+ } else {
934
+ parse_error(p, "invalid unicode");
935
+ return;
936
+ }
937
+ p->map = string_map;
938
+ }
939
+ break;
940
+ case ESC_OK:
941
+ if (':' == p->next_map[256]) {
942
+ buf_append(&p->key, esc_byte_map[*b]);
943
+ } else {
944
+ buf_append(&p->buf, esc_byte_map[*b]);
945
+ }
946
+ p->map = string_map;
947
+ break;
948
+ case UTF1:
949
+ p->ri = 1;
950
+ p->map = utf_map;
951
+ if (':' == p->next_map[256]) {
952
+ buf_append(&p->key, *b);
953
+ } else {
954
+ buf_append(&p->buf, *b);
955
+ }
956
+ break;
957
+ case UTF2:
958
+ p->ri = 2;
959
+ p->map = utf_map;
960
+ if (':' == p->next_map[256]) {
961
+ buf_append(&p->key, *b);
962
+ } else {
963
+ buf_append(&p->buf, *b);
964
+ }
965
+ break;
966
+ case UTF3:
967
+ p->ri = 3;
968
+ p->map = utf_map;
969
+ if (':' == p->next_map[256]) {
970
+ buf_append(&p->key, *b);
971
+ } else {
972
+ buf_append(&p->buf, *b);
973
+ }
974
+ break;
975
+ case UTFX:
976
+ p->ri--;
977
+ if (':' == p->next_map[256]) {
978
+ buf_append(&p->key, *b);
979
+ } else {
980
+ buf_append(&p->buf, *b);
981
+ }
982
+ if (p->ri <= 0) {
983
+ p->map = string_map;
984
+ }
985
+ break;
986
+ case VAL_NULL:
987
+ if ('u' == b[1] && 'l' == b[2] && 'l' == b[3]) {
988
+ b += 3;
989
+ p->cur = b - json;
990
+ p->funcs[p->stack[p->depth]].add_null(p);
991
+ p->map = (0 == p->depth) ? value_map : after_map;
992
+ break;
993
+ }
994
+ p->ri = 0;
995
+ *p->token = *b++;
996
+ for (i = 1; i < 4; i++) {
997
+ if ('\0' == *b) {
998
+ p->ri = i;
999
+ break;
1000
+ } else {
1001
+ p->token[i] = *b++;
1002
+ }
1003
+ }
1004
+ if (0 < p->ri) {
1005
+ p->map = null_map;
1006
+ b--;
1007
+ break;
1008
+ }
1009
+ p->col = b - json - p->col;
1010
+ parse_error(p, "expected null");
1011
+ return;
1012
+ case VAL_TRUE:
1013
+ if ('r' == b[1] && 'u' == b[2] && 'e' == b[3]) {
1014
+ b += 3;
1015
+ p->cur = b - json;
1016
+ p->funcs[p->stack[p->depth]].add_true(p);
1017
+ p->map = (0 == p->depth) ? value_map : after_map;
1018
+ break;
1019
+ }
1020
+ p->ri = 0;
1021
+ *p->token = *b++;
1022
+ for (i = 1; i < 4; i++) {
1023
+ if ('\0' == *b) {
1024
+ p->ri = i;
1025
+ break;
1026
+ } else {
1027
+ p->token[i] = *b++;
1028
+ }
1029
+ }
1030
+ if (0 < p->ri) {
1031
+ p->map = true_map;
1032
+ b--;
1033
+ break;
1034
+ }
1035
+ p->col = b - json - p->col;
1036
+ parse_error(p, "expected true");
1037
+ return;
1038
+ case VAL_FALSE:
1039
+ if ('a' == b[1] && 'l' == b[2] && 's' == b[3] && 'e' == b[4]) {
1040
+ b += 4;
1041
+ p->cur = b - json;
1042
+ p->funcs[p->stack[p->depth]].add_false(p);
1043
+ p->map = (0 == p->depth) ? value_map : after_map;
1044
+ break;
1045
+ }
1046
+ p->ri = 0;
1047
+ *p->token = *b++;
1048
+ for (i = 1; i < 5; i++) {
1049
+ if ('\0' == *b) {
1050
+ p->ri = i;
1051
+ break;
1052
+ } else {
1053
+ p->token[i] = *b++;
1054
+ }
1055
+ }
1056
+ if (0 < p->ri) {
1057
+ p->map = false_map;
1058
+ b--;
1059
+ break;
1060
+ }
1061
+ p->col = b - json - p->col;
1062
+ parse_error(p, "expected false");
1063
+ return;
1064
+ case TOKEN_OK:
1065
+ p->token[p->ri] = *b;
1066
+ p->ri++;
1067
+ switch (p->map[256]) {
1068
+ case 'N':
1069
+ if (4 == p->ri) {
1070
+ if (0 != strncmp("null", p->token, 4)) {
1071
+ p->col = b - json - p->col;
1072
+ parse_error(p, "expected null");
1073
+ return;
1074
+ }
1075
+ p->cur = b - json;
1076
+ p->funcs[p->stack[p->depth]].add_null(p);
1077
+ p->map = (0 == p->depth) ? value_map : after_map;
1078
+ }
1079
+ break;
1080
+ case 'F':
1081
+ if (5 == p->ri) {
1082
+ if (0 != strncmp("false", p->token, 5)) {
1083
+ p->col = b - json - p->col;
1084
+ parse_error(p, "expected false");
1085
+ return;
1086
+ }
1087
+ p->cur = b - json;
1088
+ p->funcs[p->stack[p->depth]].add_false(p);
1089
+ p->map = (0 == p->depth) ? value_map : after_map;
1090
+ }
1091
+ break;
1092
+ case 'T':
1093
+ if (4 == p->ri) {
1094
+ if (0 != strncmp("true", p->token, 4)) {
1095
+ p->col = b - json - p->col;
1096
+ parse_error(p, "expected true");
1097
+ return;
1098
+ }
1099
+ p->cur = b - json;
1100
+ p->funcs[p->stack[p->depth]].add_true(p);
1101
+ p->map = (0 == p->depth) ? value_map : after_map;
1102
+ }
1103
+ break;
1104
+ default:
1105
+ p->col = b - json - p->col;
1106
+ parse_error(p, "parse error");
1107
+ return;
1108
+ }
1109
+ break;
1110
+ case CHAR_ERR: byte_error(p, *b); return;
1111
+ default: break;
1112
+ }
1113
+ if (0 == p->depth && 'v' == p->map[256] && p->just_one) {
1114
+ p->map = trail_map;
1115
+ }
1116
+ }
1117
+ if (0 < p->depth) {
1118
+ parse_error(p, "parse error, not closed");
1119
+ }
1120
+ if (0 == p->depth) {
1121
+ switch (p->map[256]) {
1122
+ case '0':
1123
+ case 'd':
1124
+ case 'f':
1125
+ case 'z':
1126
+ case 'X':
1127
+ case 'D':
1128
+ case 'g':
1129
+ case 'B':
1130
+ case 'Y':
1131
+ p->cur = b - json;
1132
+ calc_num(p);
1133
+ break;
1134
+ }
1135
+ }
1136
+ return;
1137
+ }
1138
+
1139
+ static void parser_free(void *ptr) {
1140
+ ojParser p;
1141
+
1142
+ if (0 == ptr) {
1143
+ return;
1144
+ }
1145
+ p = (ojParser)ptr;
1146
+ buf_cleanup(&p->key);
1147
+ buf_cleanup(&p->buf);
1148
+ if (NULL != p->free) {
1149
+ p->free(p);
1150
+ }
1151
+ xfree(ptr);
1152
+ }
1153
+
1154
+ static void parser_mark(void *ptr) {
1155
+ if (NULL != ptr) {
1156
+ ojParser p = (ojParser)ptr;
1157
+
1158
+ if (0 != p->reader) {
1159
+ rb_gc_mark(p->reader);
1160
+ }
1161
+ if (NULL != p->mark) {
1162
+ p->mark(p);
1163
+ }
1164
+ }
1165
+ }
1166
+
1167
+ extern void oj_set_parser_validator(ojParser p);
1168
+ extern void oj_set_parser_saj(ojParser p);
1169
+ extern void oj_set_parser_usual(ojParser p);
1170
+ extern void oj_set_parser_debug(ojParser p);
1171
+
1172
+ static int opt_cb(VALUE rkey, VALUE value, VALUE ptr) {
1173
+ ojParser p = (ojParser)ptr;
1174
+ const char *key = NULL;
1175
+ char set_key[64];
1176
+ long klen;
1177
+
1178
+ switch (rb_type(rkey)) {
1179
+ case RUBY_T_SYMBOL:
1180
+ rkey = rb_sym2str(rkey);
1181
+ // fall through
1182
+ case RUBY_T_STRING:
1183
+ key = rb_string_value_ptr(&rkey);
1184
+ klen = RSTRING_LEN(rkey);
1185
+ break;
1186
+ default: rb_raise(rb_eArgError, "option keys must be a symbol or string");
1187
+ }
1188
+ if ((long)sizeof(set_key) - 1 <= klen) {
1189
+ return ST_CONTINUE;
1190
+ }
1191
+ memcpy(set_key, key, klen);
1192
+ set_key[klen] = '=';
1193
+ set_key[klen + 1] = '\0';
1194
+ p->option(p, set_key, value);
1195
+
1196
+ return ST_CONTINUE;
1197
+ }
1198
+
1199
+ /* Document-method: new
1200
+ * call-seq: new(mode=nil)
1201
+ *
1202
+ * Creates a new Parser with the specified mode. If no mode is provided
1203
+ * validation is assumed. Optional arguments can be provided that match the
1204
+ * mode. For example with the :usual mode the call might look like
1205
+ * Oj::Parser.new(:usual, cache_keys: true).
1206
+ */
1207
+ static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
1208
+ ojParser p = ALLOC(struct _ojParser);
1209
+
1210
+ #if HAVE_RB_EXT_RACTOR_SAFE
1211
+ // This doesn't seem to do anything.
1212
+ rb_ext_ractor_safe(true);
1213
+ #endif
1214
+ memset(p, 0, sizeof(struct _ojParser));
1215
+ buf_init(&p->key);
1216
+ buf_init(&p->buf);
1217
+ p->map = value_map;
1218
+
1219
+ if (argc < 1) {
1220
+ oj_set_parser_validator(p);
1221
+ } else {
1222
+ VALUE mode = argv[0];
1223
+
1224
+ if (Qnil == mode) {
1225
+ oj_set_parser_validator(p);
1226
+ } else {
1227
+ const char *ms = NULL;
1228
+
1229
+ switch (rb_type(mode)) {
1230
+ case RUBY_T_SYMBOL:
1231
+ mode = rb_sym2str(mode);
1232
+ // fall through
1233
+ case RUBY_T_STRING: ms = RSTRING_PTR(mode); break;
1234
+ default: rb_raise(rb_eArgError, "mode must be :validate, :usual, :saj, or :object");
1235
+ }
1236
+ if (0 == strcmp("usual", ms) || 0 == strcmp("standard", ms) || 0 == strcmp("strict", ms) ||
1237
+ 0 == strcmp("compat", ms)) {
1238
+ oj_set_parser_usual(p);
1239
+ } else if (0 == strcmp("object", ms)) {
1240
+ // TBD
1241
+ } else if (0 == strcmp("saj", ms)) {
1242
+ oj_set_parser_saj(p);
1243
+ } else if (0 == strcmp("validate", ms)) {
1244
+ oj_set_parser_validator(p);
1245
+ } else if (0 == strcmp("debug", ms)) {
1246
+ oj_set_parser_debug(p);
1247
+ } else {
1248
+ rb_raise(rb_eArgError, "mode must be :validate, :usual, :saj, or :object");
1249
+ }
1250
+ }
1251
+ if (1 < argc) {
1252
+ VALUE ropts = argv[1];
1253
+
1254
+ Check_Type(ropts, T_HASH);
1255
+ rb_hash_foreach(ropts, opt_cb, (VALUE)p);
1256
+ }
1257
+ }
1258
+ return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1259
+ }
1260
+
1261
+ // Create a new parser without setting the delegate. The parser is
1262
+ // wrapped. The parser is (ojParser)DATA_PTR(value) where value is the return
1263
+ // from this function. A delegate must be added before the parser can be
1264
+ // used. Optionally oj_parser_set_options can be called if the options are not
1265
+ // set directly.
1266
+ VALUE oj_parser_new() {
1267
+ ojParser p = ALLOC(struct _ojParser);
1268
+
1269
+ #if HAVE_RB_EXT_RACTOR_SAFE
1270
+ // This doesn't seem to do anything.
1271
+ rb_ext_ractor_safe(true);
1272
+ #endif
1273
+ memset(p, 0, sizeof(struct _ojParser));
1274
+ buf_init(&p->key);
1275
+ buf_init(&p->buf);
1276
+ p->map = value_map;
1277
+
1278
+ return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1279
+ }
1280
+
1281
+ // Set set the options from a hash (ropts).
1282
+ void oj_parser_set_option(ojParser p, VALUE ropts) {
1283
+ Check_Type(ropts, T_HASH);
1284
+ rb_hash_foreach(ropts, opt_cb, (VALUE)p);
1285
+ }
1286
+
1287
+ /* Document-method: method_missing(value)
1288
+ * call-seq: method_missing(value)
1289
+ *
1290
+ * Methods not handled by the parser are passed to the delegate. The methods
1291
+ * supported by delegate are:
1292
+ *
1293
+ * - *:validate*
1294
+ * - no options
1295
+ *
1296
+ * - *:saj*
1297
+ * - _cache_keys=_ sets the value of the _cache_keys_ flag.
1298
+ * - _cache_keys_ returns the value of the _cache_keys_ flag.
1299
+ * - _cache_strings=_ sets the value of the _cache_strings_ to an positive integer less than 35. Strings shorter than
1300
+ * that length are cached.
1301
+ * - _cache_strings_ returns the value of the _cache_strings_ integer value.
1302
+ * - _handler=_ sets the SAJ handler
1303
+ * - _handler_ returns the SAJ handler
1304
+ *
1305
+ * - *:usual*
1306
+ * - _cache_keys=_ sets the value of the _cache_keys_ flag.
1307
+ * - _cache_keys_ returns the value of the _cache_keys_ flag.
1308
+ * - _cache_strings=_ sets the value of the _cache_strings_ to a positive integer less than 35. Strings shorter than
1309
+ * that length are cached.
1310
+ * - _cache_strings_ returns the value of the _cache_strings_ integer value.
1311
+ * - _cache_expunge=_ sets the value of the _cache_expunge_ where 0 never expunges, 1 expunges slowly, 2 expunges
1312
+ * faster, and 3 or higher expunges agressively.
1313
+ * - _cache_expunge_ returns the value of the _cache_expunge_ integer value.
1314
+ * - _capacity=_ sets the capacity of the parser. The parser grows automatically but can be updated directly with this
1315
+ * call.
1316
+ * - _capacity_ returns the current capacity of the parser's internal stack.
1317
+ * - _create_id_ returns the value _create_id_ or _nil_ if there is no _create_id_.
1318
+ * - _create_id=_ sets the value _create_id_ or if _nil_ unsets it. Parsed JSON objects that include the specified
1319
+ * element use the element value as the name of the class to create an object from instead of a Hash.
1320
+ * - _decimal=_ sets the approach to how decimals are parser. If _:auto_ then the decimals with significant digits are
1321
+ * 16 or less are Floats and long ones are BigDecimal. _:ruby_ uses a call to Ruby to convert a string to a Float.
1322
+ * _:float_ always generates a Float. _:bigdecimal_ always results in a BigDecimal.
1323
+ * - _decimal_ returns the value of the decimal conversion option which can be :auto (default), :ruby, :float, or
1324
+ * :bigdecimal.
1325
+ * - _ignore_json_create_ returns the value of the _ignore_json_create_ flag.
1326
+ * - _ignore_json_create=_ sets the value of the _ignore_json_create_ flag. When set the class json_create method is
1327
+ * ignored on parsing in favor of creating an instance and populating directly.
1328
+ * - _missing_class_ return the value of the _missing_class_ indicator.
1329
+ * - _missing_class=_ sets the value of the _missing_class_ flag. Valid values are _:auto_ which creates any missing
1330
+ * classes on parse, :ignore which ignores and continues as a Hash (default), and :raise which raises an exception if
1331
+ * the class is not found.
1332
+ * - _omit_null=_ sets the _omit_null_ flag. If true then null values in a map or object are omitted from the
1333
+ * resulting Hash or Object.
1334
+ * - _omit_null_ returns the value of the _omit_null_ flag.
1335
+ * - _symbol_keys=_ sets the flag that indicates Hash keys should be parsed to Symbols versus Strings.
1336
+ * - _symbol_keys_ returns the value of the _symbol_keys_ flag.
1337
+ */
1338
+ static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
1339
+ ojParser p = (ojParser)DATA_PTR(self);
1340
+ const char *key = NULL;
1341
+ volatile VALUE rkey = *argv;
1342
+ volatile VALUE rv = Qnil;
1343
+
1344
+ #if HAVE_RB_EXT_RACTOR_SAFE
1345
+ // This doesn't seem to do anything.
1346
+ rb_ext_ractor_safe(true);
1347
+ #endif
1348
+ switch (rb_type(rkey)) {
1349
+ case RUBY_T_SYMBOL:
1350
+ rkey = rb_sym2str(rkey);
1351
+ // fall through
1352
+ case RUBY_T_STRING: key = rb_string_value_ptr(&rkey); break;
1353
+ default: rb_raise(rb_eArgError, "option method must be a symbol or string");
1354
+ }
1355
+ if (1 < argc) {
1356
+ rv = argv[1];
1357
+ }
1358
+ return p->option(p, key, rv);
1359
+ }
1360
+
1361
+ /* Document-method: parse(json)
1362
+ * call-seq: parse(json)
1363
+ *
1364
+ * Parse a JSON string.
1365
+ *
1366
+ * Returns the result according to the delegate of the parser.
1367
+ */
1368
+ static VALUE parser_parse(VALUE self, VALUE json) {
1369
+ ojParser p = (ojParser)DATA_PTR(self);
1370
+
1371
+ Check_Type(json, T_STRING);
1372
+ parser_reset(p);
1373
+ p->start(p);
1374
+ parse(p, (const byte *)rb_string_value_ptr(&json));
1375
+
1376
+ return p->result(p);
1377
+ }
1378
+
1379
+ static VALUE load_rescue(VALUE self, VALUE x) {
1380
+ // Normal EOF. No action needed other than to stop loading.
1381
+ return Qfalse;
1382
+ }
1383
+
1384
+ static VALUE load(VALUE self) {
1385
+ ojParser p = (ojParser)DATA_PTR(self);
1386
+ volatile VALUE rbuf = rb_str_new2("");
1387
+
1388
+ p->start(p);
1389
+ while (true) {
1390
+ rb_funcall(p->reader, oj_readpartial_id, 2, INT2NUM(16385), rbuf);
1391
+ if (0 < RSTRING_LEN(rbuf)) {
1392
+ parse(p, (byte *)StringValuePtr(rbuf));
1393
+ }
1394
+ }
1395
+ return Qtrue;
1396
+ }
1397
+
1398
+ /* Document-method: load(reader)
1399
+ * call-seq: load(reader)
1400
+ *
1401
+ * Parse a JSON stream.
1402
+ *
1403
+ * Returns the result according to the delegate of the parser.
1404
+ */
1405
+ static VALUE parser_load(VALUE self, VALUE reader) {
1406
+ ojParser p = (ojParser)DATA_PTR(self);
1407
+
1408
+ parser_reset(p);
1409
+ p->reader = reader;
1410
+ rb_rescue2(load, self, load_rescue, Qnil, rb_eEOFError, 0);
1411
+
1412
+ return p->result(p);
1413
+ }
1414
+
1415
+ /* Document-method: file(filename)
1416
+ * call-seq: file(filename)
1417
+ *
1418
+ * Parse a JSON file.
1419
+ *
1420
+ * Returns the result according to the delegate of the parser.
1421
+ */
1422
+ static VALUE parser_file(VALUE self, VALUE filename) {
1423
+ ojParser p = (ojParser)DATA_PTR(self);
1424
+ const char *path;
1425
+ int fd;
1426
+
1427
+ Check_Type(filename, T_STRING);
1428
+ path = rb_string_value_ptr(&filename);
1429
+
1430
+ parser_reset(p);
1431
+ p->start(p);
1432
+
1433
+ if (0 > (fd = open(path, O_RDONLY))) {
1434
+ rb_raise(rb_eIOError, "error opening %s", path);
1435
+ }
1436
+ #if USE_THREAD_LIMIT
1437
+ struct stat info;
1438
+ // st_size will be 0 if not a file
1439
+ if (0 == fstat(fd, &info) && USE_THREAD_LIMIT < info.st_size) {
1440
+ // Use threaded version.
1441
+ // TBD only if has pthreads
1442
+ // TBD parse_large(p, fd);
1443
+ return p->result(p);
1444
+ }
1445
+ #endif
1446
+ byte buf[16385];
1447
+ size_t size = sizeof(buf) - 1;
1448
+ size_t rsize;
1449
+
1450
+ while (true) {
1451
+ if (0 < (rsize = read(fd, buf, size))) {
1452
+ buf[rsize] = '\0';
1453
+ parse(p, buf);
1454
+ }
1455
+ if (rsize <= 0) {
1456
+ if (0 != rsize) {
1457
+ rb_raise(rb_eIOError, "error reading from %s", path);
1458
+ }
1459
+ break;
1460
+ }
1461
+ }
1462
+ return p->result(p);
1463
+ }
1464
+
1465
+ /* Document-method: just_one
1466
+ * call-seq: just_one
1467
+ *
1468
+ * Returns the current state of the just_one [_Boolean_] option.
1469
+ */
1470
+ static VALUE parser_just_one(VALUE self) {
1471
+ ojParser p = (ojParser)DATA_PTR(self);
1472
+
1473
+ return p->just_one ? Qtrue : Qfalse;
1474
+ }
1475
+
1476
+ /* Document-method: just_one=
1477
+ * call-seq: just_one=(value)
1478
+ *
1479
+ * Sets the *just_one* option which limits the parsing of a string or or
1480
+ * stream to a single JSON element.
1481
+ *
1482
+ * Returns the current state of the just_one [_Boolean_] option.
1483
+ */
1484
+ static VALUE parser_just_one_set(VALUE self, VALUE v) {
1485
+ ojParser p = (ojParser)DATA_PTR(self);
1486
+
1487
+ p->just_one = (Qtrue == v);
1488
+
1489
+ return p->just_one ? Qtrue : Qfalse;
1490
+ }
1491
+
1492
+ static VALUE usual_parser = Qundef;
1493
+
1494
+ /* Document-method: usual
1495
+ * call-seq: usual
1496
+ *
1497
+ * Returns the default usual parser. Note the default usual parser can not be
1498
+ * used concurrently in more than one thread.
1499
+ */
1500
+ static VALUE parser_usual(VALUE self) {
1501
+ if (Qundef == usual_parser) {
1502
+ ojParser p = ALLOC(struct _ojParser);
1503
+
1504
+ memset(p, 0, sizeof(struct _ojParser));
1505
+ buf_init(&p->key);
1506
+ buf_init(&p->buf);
1507
+ p->map = value_map;
1508
+ oj_set_parser_usual(p);
1509
+ usual_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1510
+ rb_gc_register_address(&usual_parser);
1511
+ }
1512
+ return usual_parser;
1513
+ }
1514
+
1515
+ static VALUE saj_parser = Qundef;
1516
+
1517
+ /* Document-method: saj
1518
+ * call-seq: saj
1519
+ *
1520
+ * Returns the default SAJ parser. Note the default SAJ parser can not be used
1521
+ * concurrently in more than one thread.
1522
+ */
1523
+ static VALUE parser_saj(VALUE self) {
1524
+ if (Qundef == saj_parser) {
1525
+ ojParser p = ALLOC(struct _ojParser);
1526
+
1527
+ memset(p, 0, sizeof(struct _ojParser));
1528
+ buf_init(&p->key);
1529
+ buf_init(&p->buf);
1530
+ p->map = value_map;
1531
+ oj_set_parser_saj(p);
1532
+ saj_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1533
+ rb_gc_register_address(&saj_parser);
1534
+ }
1535
+ return saj_parser;
1536
+ }
1537
+
1538
+ static VALUE validate_parser = Qundef;
1539
+
1540
+ /* Document-method: validate
1541
+ * call-seq: validate
1542
+ *
1543
+ * Returns the default validate parser.
1544
+ */
1545
+ static VALUE parser_validate(VALUE self) {
1546
+ if (Qundef == validate_parser) {
1547
+ ojParser p = ALLOC(struct _ojParser);
1548
+
1549
+ memset(p, 0, sizeof(struct _ojParser));
1550
+ buf_init(&p->key);
1551
+ buf_init(&p->buf);
1552
+ p->map = value_map;
1553
+ oj_set_parser_validator(p);
1554
+ validate_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1555
+ rb_gc_register_address(&validate_parser);
1556
+ }
1557
+ return validate_parser;
1558
+ }
1559
+
1560
+ /* Document-class: Oj::Parser
1561
+ *
1562
+ * A reusable parser that makes use of named delegates to determine the
1563
+ * handling of parsed data. Delegates are available for validation, a callback
1564
+ * parser (SAJ), and a usual delegate that builds Ruby objects as parsing
1565
+ * proceeds.
1566
+ *
1567
+ * This parser is considerably faster than the older Oj.parse call and
1568
+ * isolates options to just the parser so that other parts of the code are not
1569
+ * forced to use the same options.
1570
+ */
1571
+ void oj_parser_init(void) {
1572
+ parser_class = rb_define_class_under(Oj, "Parser", rb_cObject);
1573
+ rb_gc_register_address(&parser_class);
1574
+ rb_undef_alloc_func(parser_class);
1575
+
1576
+ rb_define_module_function(parser_class, "new", parser_new, -1);
1577
+ rb_define_method(parser_class, "parse", parser_parse, 1);
1578
+ rb_define_method(parser_class, "load", parser_load, 1);
1579
+ rb_define_method(parser_class, "file", parser_file, 1);
1580
+ rb_define_method(parser_class, "just_one", parser_just_one, 0);
1581
+ rb_define_method(parser_class, "just_one=", parser_just_one_set, 1);
1582
+ rb_define_method(parser_class, "method_missing", parser_missing, -1);
1583
+
1584
+ rb_define_module_function(parser_class, "usual", parser_usual, 0);
1585
+ rb_define_module_function(parser_class, "saj", parser_saj, 0);
1586
+ rb_define_module_function(parser_class, "validate", parser_validate, 0);
1587
+ }