json 2.7.3 → 2.10.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.
- checksums.yaml +4 -4
- data/CHANGES.md +61 -0
- data/LEGAL +0 -52
- data/README.md +61 -57
- data/ext/json/ext/fbuffer/fbuffer.h +123 -64
- data/ext/json/ext/generator/generator.c +795 -474
- data/ext/json/ext/parser/extconf.rb +5 -27
- data/ext/json/ext/parser/parser.c +1164 -1923
- data/json.gemspec +7 -5
- data/lib/json/add/bigdecimal.rb +1 -1
- data/lib/json/add/symbol.rb +7 -2
- data/lib/json/common.rb +341 -77
- data/lib/json/ext/generator/state.rb +2 -31
- data/lib/json/ext.rb +28 -11
- data/lib/json/{pure → truffle_ruby}/generator.rb +264 -154
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +15 -20
- metadata +8 -17
- data/ext/json/ext/generator/generator.h +0 -129
- data/ext/json/ext/parser/parser.h +0 -60
- data/ext/json/ext/parser/parser.rl +0 -997
- data/lib/json/pure/parser.rb +0 -331
- data/lib/json/pure.rb +0 -16
@@ -1,7 +1,345 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
|
4
|
-
|
1
|
+
#include "ruby.h"
|
2
|
+
#include "ruby/encoding.h"
|
3
|
+
|
4
|
+
/* shims */
|
5
|
+
/* This is the fallback definition from Ruby 3.4 */
|
6
|
+
|
7
|
+
#ifndef RBIMPL_STDBOOL_H
|
8
|
+
#if defined(__cplusplus)
|
9
|
+
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
|
10
|
+
# include <cstdbool>
|
11
|
+
# endif
|
12
|
+
#elif defined(HAVE_STDBOOL_H)
|
13
|
+
# include <stdbool.h>
|
14
|
+
#elif !defined(HAVE__BOOL)
|
15
|
+
typedef unsigned char _Bool;
|
16
|
+
# define bool _Bool
|
17
|
+
# define true ((_Bool)+1)
|
18
|
+
# define false ((_Bool)+0)
|
19
|
+
# define __bool_true_false_are_defined
|
20
|
+
#endif
|
21
|
+
#endif
|
22
|
+
|
23
|
+
#ifndef RB_UNLIKELY
|
24
|
+
#define RB_UNLIKELY(expr) expr
|
25
|
+
#endif
|
26
|
+
|
27
|
+
#ifndef RB_LIKELY
|
28
|
+
#define RB_LIKELY(expr) expr
|
29
|
+
#endif
|
30
|
+
|
31
|
+
static VALUE mJSON, eNestingError, Encoding_UTF_8;
|
32
|
+
static VALUE CNaN, CInfinity, CMinusInfinity;
|
33
|
+
|
34
|
+
static ID i_json_creatable_p, i_json_create, i_create_id,
|
35
|
+
i_chr, i_deep_const_get, i_match, i_aset, i_aref,
|
36
|
+
i_leftshift, i_new, i_try_convert, i_uminus, i_encode;
|
37
|
+
|
38
|
+
static VALUE sym_max_nesting, sym_allow_nan, sym_allow_trailing_comma, sym_symbolize_names, sym_freeze,
|
39
|
+
sym_create_additions, sym_create_id, sym_object_class, sym_array_class,
|
40
|
+
sym_decimal_class, sym_match_string;
|
41
|
+
|
42
|
+
static int binary_encindex;
|
43
|
+
static int utf8_encindex;
|
44
|
+
|
45
|
+
#ifdef HAVE_RB_CATEGORY_WARN
|
46
|
+
# define json_deprecated(message) rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, message)
|
47
|
+
#else
|
48
|
+
# define json_deprecated(message) rb_warn(message)
|
49
|
+
#endif
|
50
|
+
|
51
|
+
static const char deprecated_create_additions_warning[] =
|
52
|
+
"JSON.load implicit support for `create_additions: true` is deprecated "
|
53
|
+
"and will be removed in 3.0, use JSON.unsafe_load or explicitly "
|
54
|
+
"pass `create_additions: true`";
|
55
|
+
|
56
|
+
#ifndef HAVE_RB_HASH_BULK_INSERT
|
57
|
+
// For TruffleRuby
|
58
|
+
void
|
59
|
+
rb_hash_bulk_insert(long count, const VALUE *pairs, VALUE hash)
|
60
|
+
{
|
61
|
+
long index = 0;
|
62
|
+
while (index < count) {
|
63
|
+
VALUE name = pairs[index++];
|
64
|
+
VALUE value = pairs[index++];
|
65
|
+
rb_hash_aset(hash, name, value);
|
66
|
+
}
|
67
|
+
RB_GC_GUARD(hash);
|
68
|
+
}
|
69
|
+
#endif
|
70
|
+
|
71
|
+
#ifndef HAVE_RB_HASH_NEW_CAPA
|
72
|
+
#define rb_hash_new_capa(n) rb_hash_new()
|
73
|
+
#endif
|
74
|
+
|
75
|
+
|
76
|
+
/* name cache */
|
77
|
+
|
78
|
+
#include <string.h>
|
79
|
+
#include <ctype.h>
|
80
|
+
|
81
|
+
// Object names are likely to be repeated, and are frozen.
|
82
|
+
// As such we can re-use them if we keep a cache of the ones we've seen so far,
|
83
|
+
// and save much more expensive lookups into the global fstring table.
|
84
|
+
// This cache implementation is deliberately simple, as we're optimizing for compactness,
|
85
|
+
// to be able to fit safely on the stack.
|
86
|
+
// As such, binary search into a sorted array gives a good tradeoff between compactness and
|
87
|
+
// performance.
|
88
|
+
#define JSON_RVALUE_CACHE_CAPA 63
|
89
|
+
typedef struct rvalue_cache_struct {
|
90
|
+
int length;
|
91
|
+
VALUE entries[JSON_RVALUE_CACHE_CAPA];
|
92
|
+
} rvalue_cache;
|
93
|
+
|
94
|
+
static rb_encoding *enc_utf8;
|
95
|
+
|
96
|
+
#define JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH 55
|
97
|
+
|
98
|
+
static inline VALUE build_interned_string(const char *str, const long length)
|
99
|
+
{
|
100
|
+
# ifdef HAVE_RB_ENC_INTERNED_STR
|
101
|
+
return rb_enc_interned_str(str, length, enc_utf8);
|
102
|
+
# else
|
103
|
+
VALUE rstring = rb_utf8_str_new(str, length);
|
104
|
+
return rb_funcall(rb_str_freeze(rstring), i_uminus, 0);
|
105
|
+
# endif
|
106
|
+
}
|
107
|
+
|
108
|
+
static inline VALUE build_symbol(const char *str, const long length)
|
109
|
+
{
|
110
|
+
return rb_str_intern(build_interned_string(str, length));
|
111
|
+
}
|
112
|
+
|
113
|
+
static void rvalue_cache_insert_at(rvalue_cache *cache, int index, VALUE rstring)
|
114
|
+
{
|
115
|
+
MEMMOVE(&cache->entries[index + 1], &cache->entries[index], VALUE, cache->length - index);
|
116
|
+
cache->length++;
|
117
|
+
cache->entries[index] = rstring;
|
118
|
+
}
|
119
|
+
|
120
|
+
static inline int rstring_cache_cmp(const char *str, const long length, VALUE rstring)
|
121
|
+
{
|
122
|
+
long rstring_length = RSTRING_LEN(rstring);
|
123
|
+
if (length == rstring_length) {
|
124
|
+
return memcmp(str, RSTRING_PTR(rstring), length);
|
125
|
+
} else {
|
126
|
+
return (int)(length - rstring_length);
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
static VALUE rstring_cache_fetch(rvalue_cache *cache, const char *str, const long length)
|
131
|
+
{
|
132
|
+
if (RB_UNLIKELY(length > JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH)) {
|
133
|
+
// Common names aren't likely to be very long. So we just don't
|
134
|
+
// cache names above an arbitrary threshold.
|
135
|
+
return Qfalse;
|
136
|
+
}
|
137
|
+
|
138
|
+
if (RB_UNLIKELY(!isalpha((unsigned char)str[0]))) {
|
139
|
+
// Simple heuristic, if the first character isn't a letter,
|
140
|
+
// we're much less likely to see this string again.
|
141
|
+
// We mostly want to cache strings that are likely to be repeated.
|
142
|
+
return Qfalse;
|
143
|
+
}
|
144
|
+
|
145
|
+
int low = 0;
|
146
|
+
int high = cache->length - 1;
|
147
|
+
int mid = 0;
|
148
|
+
int last_cmp = 0;
|
149
|
+
|
150
|
+
while (low <= high) {
|
151
|
+
mid = (high + low) >> 1;
|
152
|
+
VALUE entry = cache->entries[mid];
|
153
|
+
last_cmp = rstring_cache_cmp(str, length, entry);
|
154
|
+
|
155
|
+
if (last_cmp == 0) {
|
156
|
+
return entry;
|
157
|
+
} else if (last_cmp > 0) {
|
158
|
+
low = mid + 1;
|
159
|
+
} else {
|
160
|
+
high = mid - 1;
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
if (RB_UNLIKELY(memchr(str, '\\', length))) {
|
165
|
+
// We assume the overwhelming majority of names don't need to be escaped.
|
166
|
+
// But if they do, we have to fallback to the slow path.
|
167
|
+
return Qfalse;
|
168
|
+
}
|
169
|
+
|
170
|
+
VALUE rstring = build_interned_string(str, length);
|
171
|
+
|
172
|
+
if (cache->length < JSON_RVALUE_CACHE_CAPA) {
|
173
|
+
if (last_cmp > 0) {
|
174
|
+
mid += 1;
|
175
|
+
}
|
176
|
+
|
177
|
+
rvalue_cache_insert_at(cache, mid, rstring);
|
178
|
+
}
|
179
|
+
return rstring;
|
180
|
+
}
|
181
|
+
|
182
|
+
static VALUE rsymbol_cache_fetch(rvalue_cache *cache, const char *str, const long length)
|
183
|
+
{
|
184
|
+
if (RB_UNLIKELY(length > JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH)) {
|
185
|
+
// Common names aren't likely to be very long. So we just don't
|
186
|
+
// cache names above an arbitrary threshold.
|
187
|
+
return Qfalse;
|
188
|
+
}
|
189
|
+
|
190
|
+
if (RB_UNLIKELY(!isalpha((unsigned char)str[0]))) {
|
191
|
+
// Simple heuristic, if the first character isn't a letter,
|
192
|
+
// we're much less likely to see this string again.
|
193
|
+
// We mostly want to cache strings that are likely to be repeated.
|
194
|
+
return Qfalse;
|
195
|
+
}
|
196
|
+
|
197
|
+
int low = 0;
|
198
|
+
int high = cache->length - 1;
|
199
|
+
int mid = 0;
|
200
|
+
int last_cmp = 0;
|
201
|
+
|
202
|
+
while (low <= high) {
|
203
|
+
mid = (high + low) >> 1;
|
204
|
+
VALUE entry = cache->entries[mid];
|
205
|
+
last_cmp = rstring_cache_cmp(str, length, rb_sym2str(entry));
|
206
|
+
|
207
|
+
if (last_cmp == 0) {
|
208
|
+
return entry;
|
209
|
+
} else if (last_cmp > 0) {
|
210
|
+
low = mid + 1;
|
211
|
+
} else {
|
212
|
+
high = mid - 1;
|
213
|
+
}
|
214
|
+
}
|
215
|
+
|
216
|
+
if (RB_UNLIKELY(memchr(str, '\\', length))) {
|
217
|
+
// We assume the overwhelming majority of names don't need to be escaped.
|
218
|
+
// But if they do, we have to fallback to the slow path.
|
219
|
+
return Qfalse;
|
220
|
+
}
|
221
|
+
|
222
|
+
VALUE rsymbol = build_symbol(str, length);
|
223
|
+
|
224
|
+
if (cache->length < JSON_RVALUE_CACHE_CAPA) {
|
225
|
+
if (last_cmp > 0) {
|
226
|
+
mid += 1;
|
227
|
+
}
|
228
|
+
|
229
|
+
rvalue_cache_insert_at(cache, mid, rsymbol);
|
230
|
+
}
|
231
|
+
return rsymbol;
|
232
|
+
}
|
233
|
+
|
234
|
+
/* rvalue stack */
|
235
|
+
|
236
|
+
#define RVALUE_STACK_INITIAL_CAPA 128
|
237
|
+
|
238
|
+
enum rvalue_stack_type {
|
239
|
+
RVALUE_STACK_HEAP_ALLOCATED = 0,
|
240
|
+
RVALUE_STACK_STACK_ALLOCATED = 1,
|
241
|
+
};
|
242
|
+
|
243
|
+
typedef struct rvalue_stack_struct {
|
244
|
+
enum rvalue_stack_type type;
|
245
|
+
long capa;
|
246
|
+
long head;
|
247
|
+
VALUE *ptr;
|
248
|
+
} rvalue_stack;
|
249
|
+
|
250
|
+
static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle, rvalue_stack **stack_ref);
|
251
|
+
|
252
|
+
static rvalue_stack *rvalue_stack_grow(rvalue_stack *stack, VALUE *handle, rvalue_stack **stack_ref)
|
253
|
+
{
|
254
|
+
long required = stack->capa * 2;
|
255
|
+
|
256
|
+
if (stack->type == RVALUE_STACK_STACK_ALLOCATED) {
|
257
|
+
stack = rvalue_stack_spill(stack, handle, stack_ref);
|
258
|
+
} else {
|
259
|
+
REALLOC_N(stack->ptr, VALUE, required);
|
260
|
+
stack->capa = required;
|
261
|
+
}
|
262
|
+
return stack;
|
263
|
+
}
|
264
|
+
|
265
|
+
static VALUE rvalue_stack_push(rvalue_stack *stack, VALUE value, VALUE *handle, rvalue_stack **stack_ref)
|
266
|
+
{
|
267
|
+
if (RB_UNLIKELY(stack->head >= stack->capa)) {
|
268
|
+
stack = rvalue_stack_grow(stack, handle, stack_ref);
|
269
|
+
}
|
270
|
+
stack->ptr[stack->head] = value;
|
271
|
+
stack->head++;
|
272
|
+
return value;
|
273
|
+
}
|
274
|
+
|
275
|
+
static inline VALUE *rvalue_stack_peek(rvalue_stack *stack, long count)
|
276
|
+
{
|
277
|
+
return stack->ptr + (stack->head - count);
|
278
|
+
}
|
279
|
+
|
280
|
+
static inline void rvalue_stack_pop(rvalue_stack *stack, long count)
|
281
|
+
{
|
282
|
+
stack->head -= count;
|
283
|
+
}
|
284
|
+
|
285
|
+
static void rvalue_stack_mark(void *ptr)
|
286
|
+
{
|
287
|
+
rvalue_stack *stack = (rvalue_stack *)ptr;
|
288
|
+
long index;
|
289
|
+
for (index = 0; index < stack->head; index++) {
|
290
|
+
rb_gc_mark(stack->ptr[index]);
|
291
|
+
}
|
292
|
+
}
|
293
|
+
|
294
|
+
static void rvalue_stack_free(void *ptr)
|
295
|
+
{
|
296
|
+
rvalue_stack *stack = (rvalue_stack *)ptr;
|
297
|
+
if (stack) {
|
298
|
+
ruby_xfree(stack->ptr);
|
299
|
+
ruby_xfree(stack);
|
300
|
+
}
|
301
|
+
}
|
302
|
+
|
303
|
+
static size_t rvalue_stack_memsize(const void *ptr)
|
304
|
+
{
|
305
|
+
const rvalue_stack *stack = (const rvalue_stack *)ptr;
|
306
|
+
return sizeof(rvalue_stack) + sizeof(VALUE) * stack->capa;
|
307
|
+
}
|
308
|
+
|
309
|
+
static const rb_data_type_t JSON_Parser_rvalue_stack_type = {
|
310
|
+
"JSON::Ext::Parser/rvalue_stack",
|
311
|
+
{
|
312
|
+
.dmark = rvalue_stack_mark,
|
313
|
+
.dfree = rvalue_stack_free,
|
314
|
+
.dsize = rvalue_stack_memsize,
|
315
|
+
},
|
316
|
+
0, 0,
|
317
|
+
RUBY_TYPED_FREE_IMMEDIATELY,
|
318
|
+
};
|
319
|
+
|
320
|
+
static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle, rvalue_stack **stack_ref)
|
321
|
+
{
|
322
|
+
rvalue_stack *stack;
|
323
|
+
*handle = TypedData_Make_Struct(0, rvalue_stack, &JSON_Parser_rvalue_stack_type, stack);
|
324
|
+
*stack_ref = stack;
|
325
|
+
MEMCPY(stack, old_stack, rvalue_stack, 1);
|
326
|
+
|
327
|
+
stack->capa = old_stack->capa << 1;
|
328
|
+
stack->ptr = ALLOC_N(VALUE, stack->capa);
|
329
|
+
stack->type = RVALUE_STACK_HEAP_ALLOCATED;
|
330
|
+
MEMCPY(stack->ptr, old_stack->ptr, VALUE, old_stack->head);
|
331
|
+
return stack;
|
332
|
+
}
|
333
|
+
|
334
|
+
static void rvalue_stack_eagerly_release(VALUE handle)
|
335
|
+
{
|
336
|
+
if (handle) {
|
337
|
+
rvalue_stack *stack;
|
338
|
+
TypedData_Get_Struct(handle, rvalue_stack, &JSON_Parser_rvalue_stack_type, stack);
|
339
|
+
RTYPEDDATA_DATA(handle) = NULL;
|
340
|
+
rvalue_stack_free(stack);
|
341
|
+
}
|
342
|
+
}
|
5
343
|
|
6
344
|
/* unicode */
|
7
345
|
|
@@ -69,6 +407,47 @@ static int convert_UTF32_to_UTF8(char *buf, uint32_t ch)
|
|
69
407
|
return len;
|
70
408
|
}
|
71
409
|
|
410
|
+
typedef struct JSON_ParserStruct {
|
411
|
+
VALUE create_id;
|
412
|
+
VALUE object_class;
|
413
|
+
VALUE array_class;
|
414
|
+
VALUE decimal_class;
|
415
|
+
ID decimal_method_id;
|
416
|
+
VALUE match_string;
|
417
|
+
int max_nesting;
|
418
|
+
bool allow_nan;
|
419
|
+
bool allow_trailing_comma;
|
420
|
+
bool parsing_name;
|
421
|
+
bool symbolize_names;
|
422
|
+
bool freeze;
|
423
|
+
bool create_additions;
|
424
|
+
bool deprecated_create_additions;
|
425
|
+
} JSON_ParserConfig;
|
426
|
+
|
427
|
+
typedef struct JSON_ParserStateStruct {
|
428
|
+
VALUE stack_handle;
|
429
|
+
const char *cursor;
|
430
|
+
const char *end;
|
431
|
+
rvalue_stack *stack;
|
432
|
+
rvalue_cache name_cache;
|
433
|
+
int in_array;
|
434
|
+
int current_nesting;
|
435
|
+
} JSON_ParserState;
|
436
|
+
|
437
|
+
#define GET_PARSER_CONFIG \
|
438
|
+
JSON_ParserConfig *config; \
|
439
|
+
TypedData_Get_Struct(self, JSON_ParserConfig, &JSON_ParserConfig_type, config)
|
440
|
+
|
441
|
+
static const rb_data_type_t JSON_ParserConfig_type;
|
442
|
+
|
443
|
+
#ifndef HAVE_STRNLEN
|
444
|
+
static size_t strnlen(const char *s, size_t maxlen)
|
445
|
+
{
|
446
|
+
char *p;
|
447
|
+
return ((p = memchr(s, '\0', maxlen)) ? p - s : maxlen);
|
448
|
+
}
|
449
|
+
#endif
|
450
|
+
|
72
451
|
#define PARSE_ERROR_FRAGMENT_LEN 32
|
73
452
|
#ifdef RBIMPL_ATTR_NORETURN
|
74
453
|
RBIMPL_ATTR_NORETURN()
|
@@ -77,7 +456,7 @@ static void raise_parse_error(const char *format, const char *start)
|
|
77
456
|
{
|
78
457
|
char buffer[PARSE_ERROR_FRAGMENT_LEN + 1];
|
79
458
|
|
80
|
-
size_t len = strnlen(start, PARSE_ERROR_FRAGMENT_LEN);
|
459
|
+
size_t len = start ? strnlen(start, PARSE_ERROR_FRAGMENT_LEN) : 0;
|
81
460
|
const char *ptr = start;
|
82
461
|
|
83
462
|
if (len == PARSE_ERROR_FRAGMENT_LEN) {
|
@@ -86,1690 +465,704 @@ static void raise_parse_error(const char *format, const char *start)
|
|
86
465
|
ptr = buffer;
|
87
466
|
}
|
88
467
|
|
89
|
-
rb_enc_raise(
|
468
|
+
rb_enc_raise(enc_utf8, rb_path2class("JSON::ParserError"), format, ptr);
|
90
469
|
}
|
91
470
|
|
92
|
-
static
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
i_leftshift, i_new, i_try_convert, i_freeze, i_uminus;
|
100
|
-
|
101
|
-
static int binary_encindex;
|
102
|
-
static int utf8_encindex;
|
103
|
-
|
471
|
+
static const bool whitespace[256] = {
|
472
|
+
[' '] = 1,
|
473
|
+
['\t'] = 1,
|
474
|
+
['\n'] = 1,
|
475
|
+
['\r'] = 1,
|
476
|
+
['/'] = 1,
|
477
|
+
};
|
104
478
|
|
479
|
+
static void
|
480
|
+
json_eat_comments(JSON_ParserState *state)
|
481
|
+
{
|
482
|
+
if (state->cursor + 1 < state->end) {
|
483
|
+
switch(state->cursor[1]) {
|
484
|
+
case '/': {
|
485
|
+
state->cursor = memchr(state->cursor, '\n', state->end - state->cursor);
|
486
|
+
if (!state->cursor) {
|
487
|
+
state->cursor = state->end;
|
488
|
+
} else {
|
489
|
+
state->cursor++;
|
490
|
+
}
|
491
|
+
break;
|
492
|
+
}
|
493
|
+
case '*': {
|
494
|
+
state->cursor += 2;
|
495
|
+
while (true) {
|
496
|
+
state->cursor = memchr(state->cursor, '*', state->end - state->cursor);
|
497
|
+
if (!state->cursor) {
|
498
|
+
state->cursor = state->end;
|
499
|
+
raise_parse_error("unexpected end of input, expected closing '*/'", state->cursor);
|
500
|
+
} else {
|
501
|
+
state->cursor++;
|
502
|
+
if (state->cursor < state->end && *state->cursor == '/') {
|
503
|
+
state->cursor++;
|
504
|
+
break;
|
505
|
+
}
|
506
|
+
}
|
507
|
+
}
|
508
|
+
break;
|
509
|
+
}
|
510
|
+
default:
|
511
|
+
raise_parse_error("unexpected token at '%s'", state->cursor);
|
512
|
+
break;
|
513
|
+
}
|
514
|
+
} else {
|
515
|
+
raise_parse_error("unexpected token at '%s'", state->cursor);
|
516
|
+
}
|
517
|
+
}
|
105
518
|
|
106
|
-
|
519
|
+
static inline void
|
520
|
+
json_eat_whitespace(JSON_ParserState *state)
|
521
|
+
{
|
522
|
+
while (state->cursor < state->end && RB_UNLIKELY(whitespace[(unsigned char)*state->cursor])) {
|
523
|
+
if (RB_LIKELY(*state->cursor != '/')) {
|
524
|
+
state->cursor++;
|
525
|
+
} else {
|
526
|
+
json_eat_comments(state);
|
527
|
+
}
|
528
|
+
}
|
529
|
+
}
|
107
530
|
|
531
|
+
static inline VALUE build_string(const char *start, const char *end, bool intern, bool symbolize)
|
532
|
+
{
|
533
|
+
if (symbolize) {
|
534
|
+
intern = true;
|
535
|
+
}
|
536
|
+
VALUE result;
|
537
|
+
# ifdef HAVE_RB_ENC_INTERNED_STR
|
538
|
+
if (intern) {
|
539
|
+
result = rb_enc_interned_str(start, (long)(end - start), enc_utf8);
|
540
|
+
} else {
|
541
|
+
result = rb_utf8_str_new(start, (long)(end - start));
|
542
|
+
}
|
543
|
+
# else
|
544
|
+
result = rb_utf8_str_new(start, (long)(end - start));
|
545
|
+
if (intern) {
|
546
|
+
result = rb_funcall(rb_str_freeze(result), i_uminus, 0);
|
547
|
+
}
|
548
|
+
# endif
|
108
549
|
|
550
|
+
if (symbolize) {
|
551
|
+
result = rb_str_intern(result);
|
552
|
+
}
|
109
553
|
|
110
|
-
|
111
|
-
|
112
|
-
enum {JSON_object_first_final = 27};
|
113
|
-
enum {JSON_object_error = 0};
|
554
|
+
return result;
|
555
|
+
}
|
114
556
|
|
115
|
-
|
557
|
+
static inline VALUE json_string_fastpath(JSON_ParserState *state, const char *string, const char *stringEnd, bool is_name, bool intern, bool symbolize)
|
558
|
+
{
|
559
|
+
size_t bufferSize = stringEnd - string;
|
116
560
|
|
561
|
+
if (is_name && state->in_array) {
|
562
|
+
VALUE cached_key;
|
563
|
+
if (RB_UNLIKELY(symbolize)) {
|
564
|
+
cached_key = rsymbol_cache_fetch(&state->name_cache, string, bufferSize);
|
565
|
+
} else {
|
566
|
+
cached_key = rstring_cache_fetch(&state->name_cache, string, bufferSize);
|
567
|
+
}
|
117
568
|
|
118
|
-
|
569
|
+
if (RB_LIKELY(cached_key)) {
|
570
|
+
return cached_key;
|
571
|
+
}
|
572
|
+
}
|
119
573
|
|
574
|
+
return build_string(string, stringEnd, intern, symbolize);
|
575
|
+
}
|
120
576
|
|
121
|
-
static
|
577
|
+
static VALUE json_string_unescape(JSON_ParserState *state, const char *string, const char *stringEnd, bool is_name, bool intern, bool symbolize)
|
122
578
|
{
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
rb_raise(eNestingError, "nesting of %d is too deep", current_nesting);
|
129
|
-
}
|
579
|
+
size_t bufferSize = stringEnd - string;
|
580
|
+
const char *p = string, *pe = string, *unescape, *bufferStart;
|
581
|
+
char *buffer;
|
582
|
+
int unescape_len;
|
583
|
+
char buf[4];
|
130
584
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
{
|
136
|
-
cs = JSON_object_start;
|
137
|
-
}
|
138
|
-
|
139
|
-
#line 186 "parser.rl"
|
140
|
-
|
141
|
-
#line 142 "parser.c"
|
142
|
-
{
|
143
|
-
if ( p == pe )
|
144
|
-
goto _test_eof;
|
145
|
-
switch ( cs )
|
146
|
-
{
|
147
|
-
case 1:
|
148
|
-
if ( (*p) == 123 )
|
149
|
-
goto st2;
|
150
|
-
goto st0;
|
151
|
-
st0:
|
152
|
-
cs = 0;
|
153
|
-
goto _out;
|
154
|
-
st2:
|
155
|
-
if ( ++p == pe )
|
156
|
-
goto _test_eof2;
|
157
|
-
case 2:
|
158
|
-
switch( (*p) ) {
|
159
|
-
case 13: goto st2;
|
160
|
-
case 32: goto st2;
|
161
|
-
case 34: goto tr2;
|
162
|
-
case 47: goto st23;
|
163
|
-
case 125: goto tr4;
|
164
|
-
}
|
165
|
-
if ( 9 <= (*p) && (*p) <= 10 )
|
166
|
-
goto st2;
|
167
|
-
goto st0;
|
168
|
-
tr2:
|
169
|
-
#line 153 "parser.rl"
|
170
|
-
{
|
171
|
-
char *np;
|
172
|
-
json->parsing_name = 1;
|
173
|
-
np = JSON_parse_string(json, p, pe, &last_name);
|
174
|
-
json->parsing_name = 0;
|
175
|
-
if (np == NULL) { p--; {p++; cs = 3; goto _out;} } else {p = (( np))-1;}
|
176
|
-
}
|
177
|
-
goto st3;
|
178
|
-
st3:
|
179
|
-
if ( ++p == pe )
|
180
|
-
goto _test_eof3;
|
181
|
-
case 3:
|
182
|
-
#line 183 "parser.c"
|
183
|
-
switch( (*p) ) {
|
184
|
-
case 13: goto st3;
|
185
|
-
case 32: goto st3;
|
186
|
-
case 47: goto st4;
|
187
|
-
case 58: goto st8;
|
188
|
-
}
|
189
|
-
if ( 9 <= (*p) && (*p) <= 10 )
|
190
|
-
goto st3;
|
191
|
-
goto st0;
|
192
|
-
st4:
|
193
|
-
if ( ++p == pe )
|
194
|
-
goto _test_eof4;
|
195
|
-
case 4:
|
196
|
-
switch( (*p) ) {
|
197
|
-
case 42: goto st5;
|
198
|
-
case 47: goto st7;
|
199
|
-
}
|
200
|
-
goto st0;
|
201
|
-
st5:
|
202
|
-
if ( ++p == pe )
|
203
|
-
goto _test_eof5;
|
204
|
-
case 5:
|
205
|
-
if ( (*p) == 42 )
|
206
|
-
goto st6;
|
207
|
-
goto st5;
|
208
|
-
st6:
|
209
|
-
if ( ++p == pe )
|
210
|
-
goto _test_eof6;
|
211
|
-
case 6:
|
212
|
-
switch( (*p) ) {
|
213
|
-
case 42: goto st6;
|
214
|
-
case 47: goto st3;
|
215
|
-
}
|
216
|
-
goto st5;
|
217
|
-
st7:
|
218
|
-
if ( ++p == pe )
|
219
|
-
goto _test_eof7;
|
220
|
-
case 7:
|
221
|
-
if ( (*p) == 10 )
|
222
|
-
goto st3;
|
223
|
-
goto st7;
|
224
|
-
st8:
|
225
|
-
if ( ++p == pe )
|
226
|
-
goto _test_eof8;
|
227
|
-
case 8:
|
228
|
-
switch( (*p) ) {
|
229
|
-
case 13: goto st8;
|
230
|
-
case 32: goto st8;
|
231
|
-
case 34: goto tr11;
|
232
|
-
case 45: goto tr11;
|
233
|
-
case 47: goto st19;
|
234
|
-
case 73: goto tr11;
|
235
|
-
case 78: goto tr11;
|
236
|
-
case 91: goto tr11;
|
237
|
-
case 102: goto tr11;
|
238
|
-
case 110: goto tr11;
|
239
|
-
case 116: goto tr11;
|
240
|
-
case 123: goto tr11;
|
241
|
-
}
|
242
|
-
if ( (*p) > 10 ) {
|
243
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
244
|
-
goto tr11;
|
245
|
-
} else if ( (*p) >= 9 )
|
246
|
-
goto st8;
|
247
|
-
goto st0;
|
248
|
-
tr11:
|
249
|
-
#line 137 "parser.rl"
|
250
|
-
{
|
251
|
-
VALUE v = Qnil;
|
252
|
-
char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
|
253
|
-
if (np == NULL) {
|
254
|
-
p--; {p++; cs = 9; goto _out;}
|
585
|
+
if (is_name && state->in_array) {
|
586
|
+
VALUE cached_key;
|
587
|
+
if (RB_UNLIKELY(symbolize)) {
|
588
|
+
cached_key = rsymbol_cache_fetch(&state->name_cache, string, bufferSize);
|
255
589
|
} else {
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
}
|
262
|
-
{p = (( np))-1;}
|
590
|
+
cached_key = rstring_cache_fetch(&state->name_cache, string, bufferSize);
|
591
|
+
}
|
592
|
+
|
593
|
+
if (RB_LIKELY(cached_key)) {
|
594
|
+
return cached_key;
|
263
595
|
}
|
264
596
|
}
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
case
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
}
|
334
|
-
goto st0;
|
335
|
-
st16:
|
336
|
-
if ( ++p == pe )
|
337
|
-
goto _test_eof16;
|
338
|
-
case 16:
|
339
|
-
if ( (*p) == 42 )
|
340
|
-
goto st17;
|
341
|
-
goto st16;
|
342
|
-
st17:
|
343
|
-
if ( ++p == pe )
|
344
|
-
goto _test_eof17;
|
345
|
-
case 17:
|
346
|
-
switch( (*p) ) {
|
347
|
-
case 42: goto st17;
|
348
|
-
case 47: goto st9;
|
349
|
-
}
|
350
|
-
goto st16;
|
351
|
-
st18:
|
352
|
-
if ( ++p == pe )
|
353
|
-
goto _test_eof18;
|
354
|
-
case 18:
|
355
|
-
if ( (*p) == 10 )
|
356
|
-
goto st9;
|
357
|
-
goto st18;
|
358
|
-
tr4:
|
359
|
-
#line 161 "parser.rl"
|
360
|
-
{ p--; {p++; cs = 27; goto _out;} }
|
361
|
-
goto st27;
|
362
|
-
st27:
|
363
|
-
if ( ++p == pe )
|
364
|
-
goto _test_eof27;
|
365
|
-
case 27:
|
366
|
-
#line 367 "parser.c"
|
367
|
-
goto st0;
|
368
|
-
st19:
|
369
|
-
if ( ++p == pe )
|
370
|
-
goto _test_eof19;
|
371
|
-
case 19:
|
372
|
-
switch( (*p) ) {
|
373
|
-
case 42: goto st20;
|
374
|
-
case 47: goto st22;
|
375
|
-
}
|
376
|
-
goto st0;
|
377
|
-
st20:
|
378
|
-
if ( ++p == pe )
|
379
|
-
goto _test_eof20;
|
380
|
-
case 20:
|
381
|
-
if ( (*p) == 42 )
|
382
|
-
goto st21;
|
383
|
-
goto st20;
|
384
|
-
st21:
|
385
|
-
if ( ++p == pe )
|
386
|
-
goto _test_eof21;
|
387
|
-
case 21:
|
388
|
-
switch( (*p) ) {
|
389
|
-
case 42: goto st21;
|
390
|
-
case 47: goto st8;
|
391
|
-
}
|
392
|
-
goto st20;
|
393
|
-
st22:
|
394
|
-
if ( ++p == pe )
|
395
|
-
goto _test_eof22;
|
396
|
-
case 22:
|
397
|
-
if ( (*p) == 10 )
|
398
|
-
goto st8;
|
399
|
-
goto st22;
|
400
|
-
st23:
|
401
|
-
if ( ++p == pe )
|
402
|
-
goto _test_eof23;
|
403
|
-
case 23:
|
404
|
-
switch( (*p) ) {
|
405
|
-
case 42: goto st24;
|
406
|
-
case 47: goto st26;
|
407
|
-
}
|
408
|
-
goto st0;
|
409
|
-
st24:
|
410
|
-
if ( ++p == pe )
|
411
|
-
goto _test_eof24;
|
412
|
-
case 24:
|
413
|
-
if ( (*p) == 42 )
|
414
|
-
goto st25;
|
415
|
-
goto st24;
|
416
|
-
st25:
|
417
|
-
if ( ++p == pe )
|
418
|
-
goto _test_eof25;
|
419
|
-
case 25:
|
420
|
-
switch( (*p) ) {
|
421
|
-
case 42: goto st25;
|
422
|
-
case 47: goto st2;
|
423
|
-
}
|
424
|
-
goto st24;
|
425
|
-
st26:
|
426
|
-
if ( ++p == pe )
|
427
|
-
goto _test_eof26;
|
428
|
-
case 26:
|
429
|
-
if ( (*p) == 10 )
|
430
|
-
goto st2;
|
431
|
-
goto st26;
|
432
|
-
}
|
433
|
-
_test_eof2: cs = 2; goto _test_eof;
|
434
|
-
_test_eof3: cs = 3; goto _test_eof;
|
435
|
-
_test_eof4: cs = 4; goto _test_eof;
|
436
|
-
_test_eof5: cs = 5; goto _test_eof;
|
437
|
-
_test_eof6: cs = 6; goto _test_eof;
|
438
|
-
_test_eof7: cs = 7; goto _test_eof;
|
439
|
-
_test_eof8: cs = 8; goto _test_eof;
|
440
|
-
_test_eof9: cs = 9; goto _test_eof;
|
441
|
-
_test_eof10: cs = 10; goto _test_eof;
|
442
|
-
_test_eof11: cs = 11; goto _test_eof;
|
443
|
-
_test_eof12: cs = 12; goto _test_eof;
|
444
|
-
_test_eof13: cs = 13; goto _test_eof;
|
445
|
-
_test_eof14: cs = 14; goto _test_eof;
|
446
|
-
_test_eof15: cs = 15; goto _test_eof;
|
447
|
-
_test_eof16: cs = 16; goto _test_eof;
|
448
|
-
_test_eof17: cs = 17; goto _test_eof;
|
449
|
-
_test_eof18: cs = 18; goto _test_eof;
|
450
|
-
_test_eof27: cs = 27; goto _test_eof;
|
451
|
-
_test_eof19: cs = 19; goto _test_eof;
|
452
|
-
_test_eof20: cs = 20; goto _test_eof;
|
453
|
-
_test_eof21: cs = 21; goto _test_eof;
|
454
|
-
_test_eof22: cs = 22; goto _test_eof;
|
455
|
-
_test_eof23: cs = 23; goto _test_eof;
|
456
|
-
_test_eof24: cs = 24; goto _test_eof;
|
457
|
-
_test_eof25: cs = 25; goto _test_eof;
|
458
|
-
_test_eof26: cs = 26; goto _test_eof;
|
459
|
-
|
460
|
-
_test_eof: {}
|
461
|
-
_out: {}
|
462
|
-
}
|
463
|
-
|
464
|
-
#line 187 "parser.rl"
|
465
|
-
|
466
|
-
if (cs >= JSON_object_first_final) {
|
467
|
-
if (json->create_additions) {
|
468
|
-
VALUE klassname;
|
469
|
-
if (NIL_P(json->object_class)) {
|
470
|
-
klassname = rb_hash_aref(*result, json->create_id);
|
471
|
-
} else {
|
472
|
-
klassname = rb_funcall(*result, i_aref, 1, json->create_id);
|
473
|
-
}
|
474
|
-
if (!NIL_P(klassname)) {
|
475
|
-
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
|
476
|
-
if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
|
477
|
-
*result = rb_funcall(klass, i_json_create, 1, *result);
|
597
|
+
|
598
|
+
VALUE result = rb_str_buf_new(bufferSize);
|
599
|
+
rb_enc_associate_index(result, utf8_encindex);
|
600
|
+
buffer = RSTRING_PTR(result);
|
601
|
+
bufferStart = buffer;
|
602
|
+
|
603
|
+
while ((pe = memchr(pe, '\\', stringEnd - pe))) {
|
604
|
+
unescape = (char *) "?";
|
605
|
+
unescape_len = 1;
|
606
|
+
if (pe > p) {
|
607
|
+
MEMCPY(buffer, p, char, pe - p);
|
608
|
+
buffer += pe - p;
|
609
|
+
}
|
610
|
+
switch (*++pe) {
|
611
|
+
case 'n':
|
612
|
+
unescape = (char *) "\n";
|
613
|
+
break;
|
614
|
+
case 'r':
|
615
|
+
unescape = (char *) "\r";
|
616
|
+
break;
|
617
|
+
case 't':
|
618
|
+
unescape = (char *) "\t";
|
619
|
+
break;
|
620
|
+
case '"':
|
621
|
+
unescape = (char *) "\"";
|
622
|
+
break;
|
623
|
+
case '\\':
|
624
|
+
unescape = (char *) "\\";
|
625
|
+
break;
|
626
|
+
case 'b':
|
627
|
+
unescape = (char *) "\b";
|
628
|
+
break;
|
629
|
+
case 'f':
|
630
|
+
unescape = (char *) "\f";
|
631
|
+
break;
|
632
|
+
case 'u':
|
633
|
+
if (pe > stringEnd - 5) {
|
634
|
+
raise_parse_error("incomplete unicode character escape sequence at '%s'", p);
|
635
|
+
} else {
|
636
|
+
uint32_t ch = unescape_unicode((unsigned char *) ++pe);
|
637
|
+
pe += 3;
|
638
|
+
/* To handle values above U+FFFF, we take a sequence of
|
639
|
+
* \uXXXX escapes in the U+D800..U+DBFF then
|
640
|
+
* U+DC00..U+DFFF ranges, take the low 10 bits from each
|
641
|
+
* to make a 20-bit number, then add 0x10000 to get the
|
642
|
+
* final codepoint.
|
643
|
+
*
|
644
|
+
* See Unicode 15: 3.8 "Surrogates", 5.3 "Handling
|
645
|
+
* Surrogate Pairs in UTF-16", and 23.6 "Surrogates
|
646
|
+
* Area".
|
647
|
+
*/
|
648
|
+
if ((ch & 0xFC00) == 0xD800) {
|
649
|
+
pe++;
|
650
|
+
if (pe > stringEnd - 6) {
|
651
|
+
raise_parse_error("incomplete surrogate pair at '%s'", p);
|
652
|
+
}
|
653
|
+
if (pe[0] == '\\' && pe[1] == 'u') {
|
654
|
+
uint32_t sur = unescape_unicode((unsigned char *) pe + 2);
|
655
|
+
ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
|
656
|
+
| (sur & 0x3FF));
|
657
|
+
pe += 5;
|
658
|
+
} else {
|
659
|
+
unescape = (char *) "?";
|
660
|
+
break;
|
661
|
+
}
|
662
|
+
}
|
663
|
+
unescape_len = convert_UTF32_to_UTF8(buf, ch);
|
664
|
+
unescape = buf;
|
478
665
|
}
|
479
|
-
|
666
|
+
break;
|
667
|
+
default:
|
668
|
+
p = pe;
|
669
|
+
continue;
|
480
670
|
}
|
481
|
-
|
482
|
-
|
483
|
-
|
671
|
+
MEMCPY(buffer, unescape, char, unescape_len);
|
672
|
+
buffer += unescape_len;
|
673
|
+
p = ++pe;
|
484
674
|
}
|
485
|
-
}
|
486
675
|
|
676
|
+
if (stringEnd > p) {
|
677
|
+
MEMCPY(buffer, p, char, stringEnd - p);
|
678
|
+
buffer += stringEnd - p;
|
679
|
+
}
|
680
|
+
rb_str_set_len(result, buffer - bufferStart);
|
487
681
|
|
682
|
+
if (symbolize) {
|
683
|
+
result = rb_str_intern(result);
|
684
|
+
} else if (intern) {
|
685
|
+
result = rb_funcall(rb_str_freeze(result), i_uminus, 0);
|
686
|
+
}
|
488
687
|
|
489
|
-
|
490
|
-
|
491
|
-
enum {JSON_value_first_final = 29};
|
492
|
-
enum {JSON_value_error = 0};
|
688
|
+
return result;
|
689
|
+
}
|
493
690
|
|
494
|
-
|
691
|
+
#define MAX_FAST_INTEGER_SIZE 18
|
692
|
+
static inline VALUE fast_decode_integer(const char *p, const char *pe)
|
693
|
+
{
|
694
|
+
bool negative = false;
|
695
|
+
if (*p == '-') {
|
696
|
+
negative = true;
|
697
|
+
p++;
|
698
|
+
}
|
495
699
|
|
700
|
+
long long memo = 0;
|
701
|
+
while (p < pe) {
|
702
|
+
memo *= 10;
|
703
|
+
memo += *p - '0';
|
704
|
+
p++;
|
705
|
+
}
|
496
706
|
|
497
|
-
|
707
|
+
if (negative) {
|
708
|
+
memo = -memo;
|
709
|
+
}
|
710
|
+
return LL2NUM(memo);
|
711
|
+
}
|
498
712
|
|
713
|
+
static VALUE json_decode_large_integer(const char *start, long len)
|
714
|
+
{
|
715
|
+
VALUE buffer_v;
|
716
|
+
char *buffer = RB_ALLOCV_N(char, buffer_v, len + 1);
|
717
|
+
MEMCPY(buffer, start, char, len);
|
718
|
+
buffer[len] = '\0';
|
719
|
+
VALUE number = rb_cstr2inum(buffer, 10);
|
720
|
+
RB_ALLOCV_END(buffer_v);
|
721
|
+
return number;
|
722
|
+
}
|
499
723
|
|
500
|
-
static
|
724
|
+
static inline VALUE
|
725
|
+
json_decode_integer(const char *start, const char *end)
|
501
726
|
{
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
#line 506 "parser.c"
|
506
|
-
{
|
507
|
-
cs = JSON_value_start;
|
508
|
-
}
|
509
|
-
|
510
|
-
#line 294 "parser.rl"
|
511
|
-
|
512
|
-
#line 513 "parser.c"
|
513
|
-
{
|
514
|
-
if ( p == pe )
|
515
|
-
goto _test_eof;
|
516
|
-
switch ( cs )
|
517
|
-
{
|
518
|
-
st1:
|
519
|
-
if ( ++p == pe )
|
520
|
-
goto _test_eof1;
|
521
|
-
case 1:
|
522
|
-
switch( (*p) ) {
|
523
|
-
case 13: goto st1;
|
524
|
-
case 32: goto st1;
|
525
|
-
case 34: goto tr2;
|
526
|
-
case 45: goto tr3;
|
527
|
-
case 47: goto st6;
|
528
|
-
case 73: goto st10;
|
529
|
-
case 78: goto st17;
|
530
|
-
case 91: goto tr7;
|
531
|
-
case 102: goto st19;
|
532
|
-
case 110: goto st23;
|
533
|
-
case 116: goto st26;
|
534
|
-
case 123: goto tr11;
|
535
|
-
}
|
536
|
-
if ( (*p) > 10 ) {
|
537
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
538
|
-
goto tr3;
|
539
|
-
} else if ( (*p) >= 9 )
|
540
|
-
goto st1;
|
541
|
-
goto st0;
|
542
|
-
st0:
|
543
|
-
cs = 0;
|
544
|
-
goto _out;
|
545
|
-
tr2:
|
546
|
-
#line 239 "parser.rl"
|
547
|
-
{
|
548
|
-
char *np = JSON_parse_string(json, p, pe, result);
|
549
|
-
if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
|
550
|
-
}
|
551
|
-
goto st29;
|
552
|
-
tr3:
|
553
|
-
#line 244 "parser.rl"
|
554
|
-
{
|
555
|
-
char *np;
|
556
|
-
if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) {
|
557
|
-
if (json->allow_nan) {
|
558
|
-
*result = CMinusInfinity;
|
559
|
-
{p = (( p + 10))-1;}
|
560
|
-
p--; {p++; cs = 29; goto _out;}
|
561
|
-
} else {
|
562
|
-
raise_parse_error("unexpected token at '%s'", p);
|
563
|
-
}
|
727
|
+
long len = end - start;
|
728
|
+
if (RB_LIKELY(len < MAX_FAST_INTEGER_SIZE)) {
|
729
|
+
return fast_decode_integer(start, end);
|
564
730
|
}
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
731
|
+
return json_decode_large_integer(start, len);
|
732
|
+
}
|
733
|
+
|
734
|
+
static VALUE json_decode_large_float(const char *start, long len)
|
735
|
+
{
|
736
|
+
VALUE buffer_v;
|
737
|
+
char *buffer = RB_ALLOCV_N(char, buffer_v, len + 1);
|
738
|
+
MEMCPY(buffer, start, char, len);
|
739
|
+
buffer[len] = '\0';
|
740
|
+
VALUE number = DBL2NUM(rb_cstr_to_dbl(buffer, 1));
|
741
|
+
RB_ALLOCV_END(buffer_v);
|
742
|
+
return number;
|
743
|
+
}
|
744
|
+
|
745
|
+
static VALUE json_decode_float(JSON_ParserConfig *config, const char *start, const char *end)
|
746
|
+
{
|
747
|
+
long len = end - start;
|
748
|
+
|
749
|
+
if (RB_UNLIKELY(config->decimal_class)) {
|
750
|
+
VALUE text = rb_str_new(start, len);
|
751
|
+
return rb_funcallv(config->decimal_class, config->decimal_method_id, 1, &text);
|
752
|
+
} else if (RB_LIKELY(len < 64)) {
|
753
|
+
char buffer[64];
|
754
|
+
MEMCPY(buffer, start, char, len);
|
755
|
+
buffer[len] = '\0';
|
756
|
+
return DBL2NUM(rb_cstr_to_dbl(buffer, 1));
|
757
|
+
} else {
|
758
|
+
return json_decode_large_float(start, len);
|
570
759
|
}
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
760
|
+
}
|
761
|
+
|
762
|
+
static inline VALUE json_decode_array(JSON_ParserState *state, JSON_ParserConfig *config, long count)
|
763
|
+
{
|
764
|
+
VALUE array;
|
765
|
+
if (RB_UNLIKELY(config->array_class)) {
|
766
|
+
array = rb_class_new_instance(0, 0, config->array_class);
|
767
|
+
VALUE *items = rvalue_stack_peek(state->stack, count);
|
768
|
+
long index;
|
769
|
+
for (index = 0; index < count; index++) {
|
770
|
+
rb_funcall(array, i_leftshift, 1, items[index]);
|
771
|
+
}
|
772
|
+
} else {
|
773
|
+
array = rb_ary_new_from_values(count, rvalue_stack_peek(state->stack, count));
|
578
774
|
}
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
np = JSON_parse_object(json, p, pe, result, current_nesting + 1);
|
585
|
-
if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
|
775
|
+
|
776
|
+
rvalue_stack_pop(state->stack, count);
|
777
|
+
|
778
|
+
if (config->freeze) {
|
779
|
+
RB_OBJ_FREEZE(array);
|
586
780
|
}
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
781
|
+
|
782
|
+
return array;
|
783
|
+
}
|
784
|
+
|
785
|
+
static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfig *config, long count)
|
786
|
+
{
|
787
|
+
VALUE object;
|
788
|
+
if (RB_UNLIKELY(config->object_class)) {
|
789
|
+
object = rb_class_new_instance(0, 0, config->object_class);
|
790
|
+
long index = 0;
|
791
|
+
VALUE *items = rvalue_stack_peek(state->stack, count);
|
792
|
+
while (index < count) {
|
793
|
+
VALUE name = items[index++];
|
794
|
+
VALUE value = items[index++];
|
795
|
+
rb_funcall(object, i_aset, 2, name, value);
|
595
796
|
}
|
797
|
+
} else {
|
798
|
+
object = rb_hash_new_capa(count);
|
799
|
+
rb_hash_bulk_insert(count, rvalue_stack_peek(state->stack, count), object);
|
596
800
|
}
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
801
|
+
|
802
|
+
rvalue_stack_pop(state->stack, count);
|
803
|
+
|
804
|
+
if (RB_UNLIKELY(config->create_additions)) {
|
805
|
+
VALUE klassname;
|
806
|
+
if (config->object_class) {
|
807
|
+
klassname = rb_funcall(object, i_aref, 1, config->create_id);
|
603
808
|
} else {
|
604
|
-
|
809
|
+
klassname = rb_hash_aref(object, config->create_id);
|
810
|
+
}
|
811
|
+
if (!NIL_P(klassname)) {
|
812
|
+
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
|
813
|
+
if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
|
814
|
+
if (config->deprecated_create_additions) {
|
815
|
+
json_deprecated(deprecated_create_additions_warning);
|
816
|
+
}
|
817
|
+
object = rb_funcall(klass, i_json_create, 1, object);
|
818
|
+
}
|
605
819
|
}
|
606
820
|
}
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
{
|
611
|
-
*result = Qfalse;
|
612
|
-
}
|
613
|
-
goto st29;
|
614
|
-
tr34:
|
615
|
-
#line 216 "parser.rl"
|
616
|
-
{
|
617
|
-
*result = Qnil;
|
618
|
-
}
|
619
|
-
goto st29;
|
620
|
-
tr37:
|
621
|
-
#line 222 "parser.rl"
|
622
|
-
{
|
623
|
-
*result = Qtrue;
|
821
|
+
|
822
|
+
if (config->freeze) {
|
823
|
+
RB_OBJ_FREEZE(object);
|
624
824
|
}
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
case 47: goto st2;
|
637
|
-
}
|
638
|
-
if ( 9 <= (*p) && (*p) <= 10 )
|
639
|
-
goto st29;
|
640
|
-
goto st0;
|
641
|
-
st2:
|
642
|
-
if ( ++p == pe )
|
643
|
-
goto _test_eof2;
|
644
|
-
case 2:
|
645
|
-
switch( (*p) ) {
|
646
|
-
case 42: goto st3;
|
647
|
-
case 47: goto st5;
|
648
|
-
}
|
649
|
-
goto st0;
|
650
|
-
st3:
|
651
|
-
if ( ++p == pe )
|
652
|
-
goto _test_eof3;
|
653
|
-
case 3:
|
654
|
-
if ( (*p) == 42 )
|
655
|
-
goto st4;
|
656
|
-
goto st3;
|
657
|
-
st4:
|
658
|
-
if ( ++p == pe )
|
659
|
-
goto _test_eof4;
|
660
|
-
case 4:
|
661
|
-
switch( (*p) ) {
|
662
|
-
case 42: goto st4;
|
663
|
-
case 47: goto st29;
|
664
|
-
}
|
665
|
-
goto st3;
|
666
|
-
st5:
|
667
|
-
if ( ++p == pe )
|
668
|
-
goto _test_eof5;
|
669
|
-
case 5:
|
670
|
-
if ( (*p) == 10 )
|
671
|
-
goto st29;
|
672
|
-
goto st5;
|
673
|
-
st6:
|
674
|
-
if ( ++p == pe )
|
675
|
-
goto _test_eof6;
|
676
|
-
case 6:
|
677
|
-
switch( (*p) ) {
|
678
|
-
case 42: goto st7;
|
679
|
-
case 47: goto st9;
|
680
|
-
}
|
681
|
-
goto st0;
|
682
|
-
st7:
|
683
|
-
if ( ++p == pe )
|
684
|
-
goto _test_eof7;
|
685
|
-
case 7:
|
686
|
-
if ( (*p) == 42 )
|
687
|
-
goto st8;
|
688
|
-
goto st7;
|
689
|
-
st8:
|
690
|
-
if ( ++p == pe )
|
691
|
-
goto _test_eof8;
|
692
|
-
case 8:
|
693
|
-
switch( (*p) ) {
|
694
|
-
case 42: goto st8;
|
695
|
-
case 47: goto st1;
|
696
|
-
}
|
697
|
-
goto st7;
|
698
|
-
st9:
|
699
|
-
if ( ++p == pe )
|
700
|
-
goto _test_eof9;
|
701
|
-
case 9:
|
702
|
-
if ( (*p) == 10 )
|
703
|
-
goto st1;
|
704
|
-
goto st9;
|
705
|
-
st10:
|
706
|
-
if ( ++p == pe )
|
707
|
-
goto _test_eof10;
|
708
|
-
case 10:
|
709
|
-
if ( (*p) == 110 )
|
710
|
-
goto st11;
|
711
|
-
goto st0;
|
712
|
-
st11:
|
713
|
-
if ( ++p == pe )
|
714
|
-
goto _test_eof11;
|
715
|
-
case 11:
|
716
|
-
if ( (*p) == 102 )
|
717
|
-
goto st12;
|
718
|
-
goto st0;
|
719
|
-
st12:
|
720
|
-
if ( ++p == pe )
|
721
|
-
goto _test_eof12;
|
722
|
-
case 12:
|
723
|
-
if ( (*p) == 105 )
|
724
|
-
goto st13;
|
725
|
-
goto st0;
|
726
|
-
st13:
|
727
|
-
if ( ++p == pe )
|
728
|
-
goto _test_eof13;
|
729
|
-
case 13:
|
730
|
-
if ( (*p) == 110 )
|
731
|
-
goto st14;
|
732
|
-
goto st0;
|
733
|
-
st14:
|
734
|
-
if ( ++p == pe )
|
735
|
-
goto _test_eof14;
|
736
|
-
case 14:
|
737
|
-
if ( (*p) == 105 )
|
738
|
-
goto st15;
|
739
|
-
goto st0;
|
740
|
-
st15:
|
741
|
-
if ( ++p == pe )
|
742
|
-
goto _test_eof15;
|
743
|
-
case 15:
|
744
|
-
if ( (*p) == 116 )
|
745
|
-
goto st16;
|
746
|
-
goto st0;
|
747
|
-
st16:
|
748
|
-
if ( ++p == pe )
|
749
|
-
goto _test_eof16;
|
750
|
-
case 16:
|
751
|
-
if ( (*p) == 121 )
|
752
|
-
goto tr25;
|
753
|
-
goto st0;
|
754
|
-
st17:
|
755
|
-
if ( ++p == pe )
|
756
|
-
goto _test_eof17;
|
757
|
-
case 17:
|
758
|
-
if ( (*p) == 97 )
|
759
|
-
goto st18;
|
760
|
-
goto st0;
|
761
|
-
st18:
|
762
|
-
if ( ++p == pe )
|
763
|
-
goto _test_eof18;
|
764
|
-
case 18:
|
765
|
-
if ( (*p) == 78 )
|
766
|
-
goto tr27;
|
767
|
-
goto st0;
|
768
|
-
st19:
|
769
|
-
if ( ++p == pe )
|
770
|
-
goto _test_eof19;
|
771
|
-
case 19:
|
772
|
-
if ( (*p) == 97 )
|
773
|
-
goto st20;
|
774
|
-
goto st0;
|
775
|
-
st20:
|
776
|
-
if ( ++p == pe )
|
777
|
-
goto _test_eof20;
|
778
|
-
case 20:
|
779
|
-
if ( (*p) == 108 )
|
780
|
-
goto st21;
|
781
|
-
goto st0;
|
782
|
-
st21:
|
783
|
-
if ( ++p == pe )
|
784
|
-
goto _test_eof21;
|
785
|
-
case 21:
|
786
|
-
if ( (*p) == 115 )
|
787
|
-
goto st22;
|
788
|
-
goto st0;
|
789
|
-
st22:
|
790
|
-
if ( ++p == pe )
|
791
|
-
goto _test_eof22;
|
792
|
-
case 22:
|
793
|
-
if ( (*p) == 101 )
|
794
|
-
goto tr31;
|
795
|
-
goto st0;
|
796
|
-
st23:
|
797
|
-
if ( ++p == pe )
|
798
|
-
goto _test_eof23;
|
799
|
-
case 23:
|
800
|
-
if ( (*p) == 117 )
|
801
|
-
goto st24;
|
802
|
-
goto st0;
|
803
|
-
st24:
|
804
|
-
if ( ++p == pe )
|
805
|
-
goto _test_eof24;
|
806
|
-
case 24:
|
807
|
-
if ( (*p) == 108 )
|
808
|
-
goto st25;
|
809
|
-
goto st0;
|
810
|
-
st25:
|
811
|
-
if ( ++p == pe )
|
812
|
-
goto _test_eof25;
|
813
|
-
case 25:
|
814
|
-
if ( (*p) == 108 )
|
815
|
-
goto tr34;
|
816
|
-
goto st0;
|
817
|
-
st26:
|
818
|
-
if ( ++p == pe )
|
819
|
-
goto _test_eof26;
|
820
|
-
case 26:
|
821
|
-
if ( (*p) == 114 )
|
822
|
-
goto st27;
|
823
|
-
goto st0;
|
824
|
-
st27:
|
825
|
-
if ( ++p == pe )
|
826
|
-
goto _test_eof27;
|
827
|
-
case 27:
|
828
|
-
if ( (*p) == 117 )
|
829
|
-
goto st28;
|
830
|
-
goto st0;
|
831
|
-
st28:
|
832
|
-
if ( ++p == pe )
|
833
|
-
goto _test_eof28;
|
834
|
-
case 28:
|
835
|
-
if ( (*p) == 101 )
|
836
|
-
goto tr37;
|
837
|
-
goto st0;
|
838
|
-
}
|
839
|
-
_test_eof1: cs = 1; goto _test_eof;
|
840
|
-
_test_eof29: cs = 29; goto _test_eof;
|
841
|
-
_test_eof2: cs = 2; goto _test_eof;
|
842
|
-
_test_eof3: cs = 3; goto _test_eof;
|
843
|
-
_test_eof4: cs = 4; goto _test_eof;
|
844
|
-
_test_eof5: cs = 5; goto _test_eof;
|
845
|
-
_test_eof6: cs = 6; goto _test_eof;
|
846
|
-
_test_eof7: cs = 7; goto _test_eof;
|
847
|
-
_test_eof8: cs = 8; goto _test_eof;
|
848
|
-
_test_eof9: cs = 9; goto _test_eof;
|
849
|
-
_test_eof10: cs = 10; goto _test_eof;
|
850
|
-
_test_eof11: cs = 11; goto _test_eof;
|
851
|
-
_test_eof12: cs = 12; goto _test_eof;
|
852
|
-
_test_eof13: cs = 13; goto _test_eof;
|
853
|
-
_test_eof14: cs = 14; goto _test_eof;
|
854
|
-
_test_eof15: cs = 15; goto _test_eof;
|
855
|
-
_test_eof16: cs = 16; goto _test_eof;
|
856
|
-
_test_eof17: cs = 17; goto _test_eof;
|
857
|
-
_test_eof18: cs = 18; goto _test_eof;
|
858
|
-
_test_eof19: cs = 19; goto _test_eof;
|
859
|
-
_test_eof20: cs = 20; goto _test_eof;
|
860
|
-
_test_eof21: cs = 21; goto _test_eof;
|
861
|
-
_test_eof22: cs = 22; goto _test_eof;
|
862
|
-
_test_eof23: cs = 23; goto _test_eof;
|
863
|
-
_test_eof24: cs = 24; goto _test_eof;
|
864
|
-
_test_eof25: cs = 25; goto _test_eof;
|
865
|
-
_test_eof26: cs = 26; goto _test_eof;
|
866
|
-
_test_eof27: cs = 27; goto _test_eof;
|
867
|
-
_test_eof28: cs = 28; goto _test_eof;
|
868
|
-
|
869
|
-
_test_eof: {}
|
870
|
-
_out: {}
|
871
|
-
}
|
872
|
-
|
873
|
-
#line 295 "parser.rl"
|
874
|
-
|
875
|
-
if (json->freeze) {
|
876
|
-
OBJ_FREEZE(*result);
|
825
|
+
|
826
|
+
return object;
|
827
|
+
}
|
828
|
+
|
829
|
+
static int match_i(VALUE regexp, VALUE klass, VALUE memo)
|
830
|
+
{
|
831
|
+
if (regexp == Qundef) return ST_STOP;
|
832
|
+
if (RTEST(rb_funcall(klass, i_json_creatable_p, 0)) &&
|
833
|
+
RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
|
834
|
+
rb_ary_push(memo, klass);
|
835
|
+
return ST_STOP;
|
877
836
|
}
|
837
|
+
return ST_CONTINUE;
|
838
|
+
}
|
878
839
|
|
879
|
-
|
880
|
-
|
840
|
+
static inline VALUE json_decode_string(JSON_ParserState *state, JSON_ParserConfig *config, const char *start, const char *end, bool escaped, bool is_name)
|
841
|
+
{
|
842
|
+
VALUE string;
|
843
|
+
bool intern = is_name || config->freeze;
|
844
|
+
bool symbolize = is_name && config->symbolize_names;
|
845
|
+
if (escaped) {
|
846
|
+
string = json_string_unescape(state, start, end, is_name, intern, symbolize);
|
881
847
|
} else {
|
882
|
-
|
848
|
+
string = json_string_fastpath(state, start, end, is_name, intern, symbolize);
|
883
849
|
}
|
884
|
-
}
|
885
850
|
|
851
|
+
if (RB_UNLIKELY(config->create_additions && RTEST(config->match_string))) {
|
852
|
+
VALUE klass;
|
853
|
+
VALUE memo = rb_ary_new2(2);
|
854
|
+
rb_ary_push(memo, string);
|
855
|
+
rb_hash_foreach(config->match_string, match_i, memo);
|
856
|
+
klass = rb_ary_entry(memo, 1);
|
857
|
+
if (RTEST(klass)) {
|
858
|
+
string = rb_funcall(klass, i_json_create, 1, string);
|
859
|
+
}
|
860
|
+
}
|
886
861
|
|
887
|
-
|
888
|
-
|
889
|
-
enum {JSON_integer_first_final = 3};
|
890
|
-
enum {JSON_integer_error = 0};
|
862
|
+
return string;
|
863
|
+
}
|
891
864
|
|
892
|
-
|
865
|
+
#define PUSH(result) rvalue_stack_push(state->stack, result, &state->stack_handle, &state->stack)
|
866
|
+
|
867
|
+
static const bool string_scan[256] = {
|
868
|
+
// ASCII Control Characters
|
869
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
870
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
871
|
+
// ASCII Characters
|
872
|
+
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
|
873
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
874
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
875
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // '\\'
|
876
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
877
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
878
|
+
};
|
893
879
|
|
880
|
+
static inline VALUE json_parse_string(JSON_ParserState *state, JSON_ParserConfig *config, bool is_name)
|
881
|
+
{
|
882
|
+
state->cursor++;
|
883
|
+
const char *start = state->cursor;
|
884
|
+
bool escaped = false;
|
885
|
+
|
886
|
+
while (state->cursor < state->end) {
|
887
|
+
if (RB_UNLIKELY(string_scan[(unsigned char)*state->cursor])) {
|
888
|
+
switch (*state->cursor) {
|
889
|
+
case '"': {
|
890
|
+
VALUE string = json_decode_string(state, config, start, state->cursor, escaped, is_name);
|
891
|
+
state->cursor++;
|
892
|
+
return PUSH(string);
|
893
|
+
}
|
894
|
+
case '\\': {
|
895
|
+
state->cursor++;
|
896
|
+
escaped = true;
|
897
|
+
if ((unsigned char)*state->cursor < 0x20) {
|
898
|
+
raise_parse_error("invalid ASCII control character in string: %s", state->cursor);
|
899
|
+
}
|
900
|
+
break;
|
901
|
+
}
|
902
|
+
default:
|
903
|
+
raise_parse_error("invalid ASCII control character in string: %s", state->cursor);
|
904
|
+
break;
|
905
|
+
}
|
906
|
+
}
|
894
907
|
|
895
|
-
|
908
|
+
state->cursor++;
|
909
|
+
}
|
896
910
|
|
911
|
+
raise_parse_error("unexpected end of input, expected closing \"", state->cursor);
|
912
|
+
return Qfalse;
|
913
|
+
}
|
897
914
|
|
898
|
-
static
|
915
|
+
static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
|
899
916
|
{
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
#line 904 "parser.c"
|
904
|
-
{
|
905
|
-
cs = JSON_integer_start;
|
906
|
-
}
|
907
|
-
|
908
|
-
#line 322 "parser.rl"
|
909
|
-
json->memo = p;
|
910
|
-
|
911
|
-
#line 912 "parser.c"
|
912
|
-
{
|
913
|
-
if ( p == pe )
|
914
|
-
goto _test_eof;
|
915
|
-
switch ( cs )
|
916
|
-
{
|
917
|
-
case 1:
|
918
|
-
switch( (*p) ) {
|
919
|
-
case 45: goto st2;
|
920
|
-
case 48: goto st3;
|
921
|
-
}
|
922
|
-
if ( 49 <= (*p) && (*p) <= 57 )
|
923
|
-
goto st5;
|
924
|
-
goto st0;
|
925
|
-
st0:
|
926
|
-
cs = 0;
|
927
|
-
goto _out;
|
928
|
-
st2:
|
929
|
-
if ( ++p == pe )
|
930
|
-
goto _test_eof2;
|
931
|
-
case 2:
|
932
|
-
if ( (*p) == 48 )
|
933
|
-
goto st3;
|
934
|
-
if ( 49 <= (*p) && (*p) <= 57 )
|
935
|
-
goto st5;
|
936
|
-
goto st0;
|
937
|
-
st3:
|
938
|
-
if ( ++p == pe )
|
939
|
-
goto _test_eof3;
|
940
|
-
case 3:
|
941
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
942
|
-
goto st0;
|
943
|
-
goto tr4;
|
944
|
-
tr4:
|
945
|
-
#line 312 "parser.rl"
|
946
|
-
{ p--; {p++; cs = 4; goto _out;} }
|
947
|
-
goto st4;
|
948
|
-
st4:
|
949
|
-
if ( ++p == pe )
|
950
|
-
goto _test_eof4;
|
951
|
-
case 4:
|
952
|
-
#line 953 "parser.c"
|
953
|
-
goto st0;
|
954
|
-
st5:
|
955
|
-
if ( ++p == pe )
|
956
|
-
goto _test_eof5;
|
957
|
-
case 5:
|
958
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
959
|
-
goto st5;
|
960
|
-
goto tr4;
|
961
|
-
}
|
962
|
-
_test_eof2: cs = 2; goto _test_eof;
|
963
|
-
_test_eof3: cs = 3; goto _test_eof;
|
964
|
-
_test_eof4: cs = 4; goto _test_eof;
|
965
|
-
_test_eof5: cs = 5; goto _test_eof;
|
966
|
-
|
967
|
-
_test_eof: {}
|
968
|
-
_out: {}
|
969
|
-
}
|
970
|
-
|
971
|
-
#line 324 "parser.rl"
|
972
|
-
|
973
|
-
if (cs >= JSON_integer_first_final) {
|
974
|
-
long len = p - json->memo;
|
975
|
-
fbuffer_clear(json->fbuffer);
|
976
|
-
fbuffer_append(json->fbuffer, json->memo, len);
|
977
|
-
fbuffer_append_char(json->fbuffer, '\0');
|
978
|
-
*result = rb_cstr2inum(FBUFFER_PTR(json->fbuffer), 10);
|
979
|
-
return p + 1;
|
980
|
-
} else {
|
981
|
-
return NULL;
|
917
|
+
json_eat_whitespace(state);
|
918
|
+
if (state->cursor >= state->end) {
|
919
|
+
raise_parse_error("unexpected end of input", state->cursor);
|
982
920
|
}
|
983
|
-
}
|
984
|
-
|
985
921
|
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
922
|
+
switch (*state->cursor) {
|
923
|
+
case 'n':
|
924
|
+
if ((state->end - state->cursor >= 4) && (memcmp(state->cursor, "null", 4) == 0)) {
|
925
|
+
state->cursor += 4;
|
926
|
+
return PUSH(Qnil);
|
927
|
+
}
|
992
928
|
|
929
|
+
raise_parse_error("unexpected token at '%s'", state->cursor);
|
930
|
+
break;
|
931
|
+
case 't':
|
932
|
+
if ((state->end - state->cursor >= 4) && (memcmp(state->cursor, "true", 4) == 0)) {
|
933
|
+
state->cursor += 4;
|
934
|
+
return PUSH(Qtrue);
|
935
|
+
}
|
993
936
|
|
994
|
-
|
937
|
+
raise_parse_error("unexpected token at '%s'", state->cursor);
|
938
|
+
break;
|
939
|
+
case 'f':
|
940
|
+
// Note: memcmp with a small power of two compile to an integer comparison
|
941
|
+
if ((state->end - state->cursor >= 5) && (memcmp(state->cursor + 1, "alse", 4) == 0)) {
|
942
|
+
state->cursor += 5;
|
943
|
+
return PUSH(Qfalse);
|
944
|
+
}
|
995
945
|
|
946
|
+
raise_parse_error("unexpected token at '%s'", state->cursor);
|
947
|
+
break;
|
948
|
+
case 'N':
|
949
|
+
// Note: memcmp with a small power of two compile to an integer comparison
|
950
|
+
if (config->allow_nan && (state->end - state->cursor >= 3) && (memcmp(state->cursor + 1, "aN", 2) == 0)) {
|
951
|
+
state->cursor += 3;
|
952
|
+
return PUSH(CNaN);
|
953
|
+
}
|
996
954
|
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
cs = JSON_float_start;
|
1005
|
-
}
|
1006
|
-
|
1007
|
-
#line 356 "parser.rl"
|
1008
|
-
json->memo = p;
|
1009
|
-
|
1010
|
-
#line 1011 "parser.c"
|
1011
|
-
{
|
1012
|
-
if ( p == pe )
|
1013
|
-
goto _test_eof;
|
1014
|
-
switch ( cs )
|
1015
|
-
{
|
1016
|
-
case 1:
|
1017
|
-
switch( (*p) ) {
|
1018
|
-
case 45: goto st2;
|
1019
|
-
case 48: goto st3;
|
1020
|
-
}
|
1021
|
-
if ( 49 <= (*p) && (*p) <= 57 )
|
1022
|
-
goto st7;
|
1023
|
-
goto st0;
|
1024
|
-
st0:
|
1025
|
-
cs = 0;
|
1026
|
-
goto _out;
|
1027
|
-
st2:
|
1028
|
-
if ( ++p == pe )
|
1029
|
-
goto _test_eof2;
|
1030
|
-
case 2:
|
1031
|
-
if ( (*p) == 48 )
|
1032
|
-
goto st3;
|
1033
|
-
if ( 49 <= (*p) && (*p) <= 57 )
|
1034
|
-
goto st7;
|
1035
|
-
goto st0;
|
1036
|
-
st3:
|
1037
|
-
if ( ++p == pe )
|
1038
|
-
goto _test_eof3;
|
1039
|
-
case 3:
|
1040
|
-
switch( (*p) ) {
|
1041
|
-
case 46: goto st4;
|
1042
|
-
case 69: goto st5;
|
1043
|
-
case 101: goto st5;
|
1044
|
-
}
|
1045
|
-
goto st0;
|
1046
|
-
st4:
|
1047
|
-
if ( ++p == pe )
|
1048
|
-
goto _test_eof4;
|
1049
|
-
case 4:
|
1050
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1051
|
-
goto st8;
|
1052
|
-
goto st0;
|
1053
|
-
st8:
|
1054
|
-
if ( ++p == pe )
|
1055
|
-
goto _test_eof8;
|
1056
|
-
case 8:
|
1057
|
-
switch( (*p) ) {
|
1058
|
-
case 69: goto st5;
|
1059
|
-
case 101: goto st5;
|
1060
|
-
}
|
1061
|
-
if ( (*p) > 46 ) {
|
1062
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1063
|
-
goto st8;
|
1064
|
-
} else if ( (*p) >= 45 )
|
1065
|
-
goto st0;
|
1066
|
-
goto tr9;
|
1067
|
-
tr9:
|
1068
|
-
#line 343 "parser.rl"
|
1069
|
-
{ p--; {p++; cs = 9; goto _out;} }
|
1070
|
-
goto st9;
|
1071
|
-
st9:
|
1072
|
-
if ( ++p == pe )
|
1073
|
-
goto _test_eof9;
|
1074
|
-
case 9:
|
1075
|
-
#line 1076 "parser.c"
|
1076
|
-
goto st0;
|
1077
|
-
st5:
|
1078
|
-
if ( ++p == pe )
|
1079
|
-
goto _test_eof5;
|
1080
|
-
case 5:
|
1081
|
-
switch( (*p) ) {
|
1082
|
-
case 43: goto st6;
|
1083
|
-
case 45: goto st6;
|
1084
|
-
}
|
1085
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1086
|
-
goto st10;
|
1087
|
-
goto st0;
|
1088
|
-
st6:
|
1089
|
-
if ( ++p == pe )
|
1090
|
-
goto _test_eof6;
|
1091
|
-
case 6:
|
1092
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1093
|
-
goto st10;
|
1094
|
-
goto st0;
|
1095
|
-
st10:
|
1096
|
-
if ( ++p == pe )
|
1097
|
-
goto _test_eof10;
|
1098
|
-
case 10:
|
1099
|
-
switch( (*p) ) {
|
1100
|
-
case 69: goto st0;
|
1101
|
-
case 101: goto st0;
|
1102
|
-
}
|
1103
|
-
if ( (*p) > 46 ) {
|
1104
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1105
|
-
goto st10;
|
1106
|
-
} else if ( (*p) >= 45 )
|
1107
|
-
goto st0;
|
1108
|
-
goto tr9;
|
1109
|
-
st7:
|
1110
|
-
if ( ++p == pe )
|
1111
|
-
goto _test_eof7;
|
1112
|
-
case 7:
|
1113
|
-
switch( (*p) ) {
|
1114
|
-
case 46: goto st4;
|
1115
|
-
case 69: goto st5;
|
1116
|
-
case 101: goto st5;
|
1117
|
-
}
|
1118
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1119
|
-
goto st7;
|
1120
|
-
goto st0;
|
1121
|
-
}
|
1122
|
-
_test_eof2: cs = 2; goto _test_eof;
|
1123
|
-
_test_eof3: cs = 3; goto _test_eof;
|
1124
|
-
_test_eof4: cs = 4; goto _test_eof;
|
1125
|
-
_test_eof8: cs = 8; goto _test_eof;
|
1126
|
-
_test_eof9: cs = 9; goto _test_eof;
|
1127
|
-
_test_eof5: cs = 5; goto _test_eof;
|
1128
|
-
_test_eof6: cs = 6; goto _test_eof;
|
1129
|
-
_test_eof10: cs = 10; goto _test_eof;
|
1130
|
-
_test_eof7: cs = 7; goto _test_eof;
|
1131
|
-
|
1132
|
-
_test_eof: {}
|
1133
|
-
_out: {}
|
1134
|
-
}
|
1135
|
-
|
1136
|
-
#line 358 "parser.rl"
|
1137
|
-
|
1138
|
-
if (cs >= JSON_float_first_final) {
|
1139
|
-
VALUE mod = Qnil;
|
1140
|
-
ID method_id = 0;
|
1141
|
-
if (!NIL_P(json->decimal_class)) {
|
1142
|
-
if (rb_respond_to(json->decimal_class, i_try_convert)) {
|
1143
|
-
mod = json->decimal_class;
|
1144
|
-
method_id = i_try_convert;
|
1145
|
-
} else if (rb_respond_to(json->decimal_class, i_new)) {
|
1146
|
-
mod = json->decimal_class;
|
1147
|
-
method_id = i_new;
|
1148
|
-
} else if (RB_TYPE_P(json->decimal_class, T_CLASS)) {
|
1149
|
-
VALUE name = rb_class_name(json->decimal_class);
|
1150
|
-
const char *name_cstr = RSTRING_PTR(name);
|
1151
|
-
const char *last_colon = strrchr(name_cstr, ':');
|
1152
|
-
if (last_colon) {
|
1153
|
-
const char *mod_path_end = last_colon - 1;
|
1154
|
-
VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
|
1155
|
-
mod = rb_path_to_class(mod_path);
|
955
|
+
raise_parse_error("unexpected token at '%s'", state->cursor);
|
956
|
+
break;
|
957
|
+
case 'I':
|
958
|
+
if (config->allow_nan && (state->end - state->cursor >= 8) && (memcmp(state->cursor, "Infinity", 8) == 0)) {
|
959
|
+
state->cursor += 8;
|
960
|
+
return PUSH(CInfinity);
|
961
|
+
}
|
1156
962
|
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
963
|
+
raise_parse_error("unexpected token at '%s'", state->cursor);
|
964
|
+
break;
|
965
|
+
case '-':
|
966
|
+
// Note: memcmp with a small power of two compile to an integer comparison
|
967
|
+
if ((state->end - state->cursor >= 9) && (memcmp(state->cursor + 1, "Infinity", 8) == 0)) {
|
968
|
+
if (config->allow_nan) {
|
969
|
+
state->cursor += 9;
|
970
|
+
return PUSH(CMinusInfinity);
|
1162
971
|
} else {
|
1163
|
-
|
1164
|
-
method_id = SYM2ID(rb_str_intern(name));
|
972
|
+
raise_parse_error("unexpected token at '%s'", state->cursor);
|
1165
973
|
}
|
1166
974
|
}
|
1167
|
-
|
975
|
+
// Fallthrough
|
976
|
+
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {
|
977
|
+
bool integer = true;
|
1168
978
|
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
fbuffer_append_char(json->fbuffer, '\0');
|
979
|
+
// /\A-?(0|[1-9]\d*)(\.\d+)?([Ee][-+]?\d+)?/
|
980
|
+
const char *start = state->cursor;
|
981
|
+
state->cursor++;
|
1173
982
|
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
} else {
|
1178
|
-
*result = DBL2NUM(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
|
1179
|
-
}
|
1180
|
-
|
1181
|
-
return p + 1;
|
1182
|
-
} else {
|
1183
|
-
return NULL;
|
1184
|
-
}
|
1185
|
-
}
|
983
|
+
while ((state->cursor < state->end) && (*state->cursor >= '0') && (*state->cursor <= '9')) {
|
984
|
+
state->cursor++;
|
985
|
+
}
|
1186
986
|
|
987
|
+
long integer_length = state->cursor - start;
|
1187
988
|
|
989
|
+
if (RB_UNLIKELY(start[0] == '0' && integer_length > 1)) {
|
990
|
+
raise_parse_error("invalid number: %s", start);
|
991
|
+
} else if (RB_UNLIKELY(integer_length > 2 && start[0] == '-' && start[1] == '0')) {
|
992
|
+
raise_parse_error("invalid number: %s", start);
|
993
|
+
} else if (RB_UNLIKELY(integer_length == 1 && start[0] == '-')) {
|
994
|
+
raise_parse_error("invalid number: %s", start);
|
995
|
+
}
|
1188
996
|
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
enum {JSON_array_error = 0};
|
997
|
+
if ((state->cursor < state->end) && (*state->cursor == '.')) {
|
998
|
+
integer = false;
|
999
|
+
state->cursor++;
|
1193
1000
|
|
1194
|
-
|
1001
|
+
if (state->cursor == state->end || *state->cursor < '0' || *state->cursor > '9') {
|
1002
|
+
raise_parse_error("invalid number: %s", state->cursor);
|
1003
|
+
}
|
1195
1004
|
|
1005
|
+
while ((state->cursor < state->end) && (*state->cursor >= '0') && (*state->cursor <= '9')) {
|
1006
|
+
state->cursor++;
|
1007
|
+
}
|
1008
|
+
}
|
1196
1009
|
|
1197
|
-
|
1010
|
+
if ((state->cursor < state->end) && ((*state->cursor == 'e') || (*state->cursor == 'E'))) {
|
1011
|
+
integer = false;
|
1012
|
+
state->cursor++;
|
1013
|
+
if ((state->cursor < state->end) && ((*state->cursor == '+') || (*state->cursor == '-'))) {
|
1014
|
+
state->cursor++;
|
1015
|
+
}
|
1198
1016
|
|
1017
|
+
if (state->cursor == state->end || *state->cursor < '0' || *state->cursor > '9') {
|
1018
|
+
raise_parse_error("invalid number: %s", state->cursor);
|
1019
|
+
}
|
1199
1020
|
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1021
|
+
while ((state->cursor < state->end) && (*state->cursor >= '0') && (*state->cursor <= '9')) {
|
1022
|
+
state->cursor++;
|
1023
|
+
}
|
1024
|
+
}
|
1204
1025
|
|
1205
|
-
|
1206
|
-
|
1207
|
-
}
|
1208
|
-
*result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
|
1209
|
-
|
1210
|
-
|
1211
|
-
#line 1212 "parser.c"
|
1212
|
-
{
|
1213
|
-
cs = JSON_array_start;
|
1214
|
-
}
|
1215
|
-
|
1216
|
-
#line 451 "parser.rl"
|
1217
|
-
|
1218
|
-
#line 1219 "parser.c"
|
1219
|
-
{
|
1220
|
-
if ( p == pe )
|
1221
|
-
goto _test_eof;
|
1222
|
-
switch ( cs )
|
1223
|
-
{
|
1224
|
-
case 1:
|
1225
|
-
if ( (*p) == 91 )
|
1226
|
-
goto st2;
|
1227
|
-
goto st0;
|
1228
|
-
st0:
|
1229
|
-
cs = 0;
|
1230
|
-
goto _out;
|
1231
|
-
st2:
|
1232
|
-
if ( ++p == pe )
|
1233
|
-
goto _test_eof2;
|
1234
|
-
case 2:
|
1235
|
-
switch( (*p) ) {
|
1236
|
-
case 13: goto st2;
|
1237
|
-
case 32: goto st2;
|
1238
|
-
case 34: goto tr2;
|
1239
|
-
case 45: goto tr2;
|
1240
|
-
case 47: goto st13;
|
1241
|
-
case 73: goto tr2;
|
1242
|
-
case 78: goto tr2;
|
1243
|
-
case 91: goto tr2;
|
1244
|
-
case 93: goto tr4;
|
1245
|
-
case 102: goto tr2;
|
1246
|
-
case 110: goto tr2;
|
1247
|
-
case 116: goto tr2;
|
1248
|
-
case 123: goto tr2;
|
1249
|
-
}
|
1250
|
-
if ( (*p) > 10 ) {
|
1251
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1252
|
-
goto tr2;
|
1253
|
-
} else if ( (*p) >= 9 )
|
1254
|
-
goto st2;
|
1255
|
-
goto st0;
|
1256
|
-
tr2:
|
1257
|
-
#line 415 "parser.rl"
|
1258
|
-
{
|
1259
|
-
VALUE v = Qnil;
|
1260
|
-
char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
|
1261
|
-
if (np == NULL) {
|
1262
|
-
p--; {p++; cs = 3; goto _out;}
|
1263
|
-
} else {
|
1264
|
-
if (NIL_P(json->array_class)) {
|
1265
|
-
rb_ary_push(*result, v);
|
1266
|
-
} else {
|
1267
|
-
rb_funcall(*result, i_leftshift, 1, v);
|
1026
|
+
if (integer) {
|
1027
|
+
return PUSH(json_decode_integer(start, state->cursor));
|
1268
1028
|
}
|
1269
|
-
|
1029
|
+
return PUSH(json_decode_float(config, start, state->cursor));
|
1270
1030
|
}
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
case
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
switch( (*p) ) {
|
1293
|
-
case 13: goto st4;
|
1294
|
-
case 32: goto st4;
|
1295
|
-
case 34: goto tr2;
|
1296
|
-
case 45: goto tr2;
|
1297
|
-
case 47: goto st5;
|
1298
|
-
case 73: goto tr2;
|
1299
|
-
case 78: goto tr2;
|
1300
|
-
case 91: goto tr2;
|
1301
|
-
case 102: goto tr2;
|
1302
|
-
case 110: goto tr2;
|
1303
|
-
case 116: goto tr2;
|
1304
|
-
case 123: goto tr2;
|
1305
|
-
}
|
1306
|
-
if ( (*p) > 10 ) {
|
1307
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1308
|
-
goto tr2;
|
1309
|
-
} else if ( (*p) >= 9 )
|
1310
|
-
goto st4;
|
1311
|
-
goto st0;
|
1312
|
-
st5:
|
1313
|
-
if ( ++p == pe )
|
1314
|
-
goto _test_eof5;
|
1315
|
-
case 5:
|
1316
|
-
switch( (*p) ) {
|
1317
|
-
case 42: goto st6;
|
1318
|
-
case 47: goto st8;
|
1319
|
-
}
|
1320
|
-
goto st0;
|
1321
|
-
st6:
|
1322
|
-
if ( ++p == pe )
|
1323
|
-
goto _test_eof6;
|
1324
|
-
case 6:
|
1325
|
-
if ( (*p) == 42 )
|
1326
|
-
goto st7;
|
1327
|
-
goto st6;
|
1328
|
-
st7:
|
1329
|
-
if ( ++p == pe )
|
1330
|
-
goto _test_eof7;
|
1331
|
-
case 7:
|
1332
|
-
switch( (*p) ) {
|
1333
|
-
case 42: goto st7;
|
1334
|
-
case 47: goto st4;
|
1335
|
-
}
|
1336
|
-
goto st6;
|
1337
|
-
st8:
|
1338
|
-
if ( ++p == pe )
|
1339
|
-
goto _test_eof8;
|
1340
|
-
case 8:
|
1341
|
-
if ( (*p) == 10 )
|
1342
|
-
goto st4;
|
1343
|
-
goto st8;
|
1344
|
-
st9:
|
1345
|
-
if ( ++p == pe )
|
1346
|
-
goto _test_eof9;
|
1347
|
-
case 9:
|
1348
|
-
switch( (*p) ) {
|
1349
|
-
case 42: goto st10;
|
1350
|
-
case 47: goto st12;
|
1351
|
-
}
|
1352
|
-
goto st0;
|
1353
|
-
st10:
|
1354
|
-
if ( ++p == pe )
|
1355
|
-
goto _test_eof10;
|
1356
|
-
case 10:
|
1357
|
-
if ( (*p) == 42 )
|
1358
|
-
goto st11;
|
1359
|
-
goto st10;
|
1360
|
-
st11:
|
1361
|
-
if ( ++p == pe )
|
1362
|
-
goto _test_eof11;
|
1363
|
-
case 11:
|
1364
|
-
switch( (*p) ) {
|
1365
|
-
case 42: goto st11;
|
1366
|
-
case 47: goto st3;
|
1367
|
-
}
|
1368
|
-
goto st10;
|
1369
|
-
st12:
|
1370
|
-
if ( ++p == pe )
|
1371
|
-
goto _test_eof12;
|
1372
|
-
case 12:
|
1373
|
-
if ( (*p) == 10 )
|
1374
|
-
goto st3;
|
1375
|
-
goto st12;
|
1376
|
-
tr4:
|
1377
|
-
#line 430 "parser.rl"
|
1378
|
-
{ p--; {p++; cs = 17; goto _out;} }
|
1379
|
-
goto st17;
|
1380
|
-
st17:
|
1381
|
-
if ( ++p == pe )
|
1382
|
-
goto _test_eof17;
|
1383
|
-
case 17:
|
1384
|
-
#line 1385 "parser.c"
|
1385
|
-
goto st0;
|
1386
|
-
st13:
|
1387
|
-
if ( ++p == pe )
|
1388
|
-
goto _test_eof13;
|
1389
|
-
case 13:
|
1390
|
-
switch( (*p) ) {
|
1391
|
-
case 42: goto st14;
|
1392
|
-
case 47: goto st16;
|
1393
|
-
}
|
1394
|
-
goto st0;
|
1395
|
-
st14:
|
1396
|
-
if ( ++p == pe )
|
1397
|
-
goto _test_eof14;
|
1398
|
-
case 14:
|
1399
|
-
if ( (*p) == 42 )
|
1400
|
-
goto st15;
|
1401
|
-
goto st14;
|
1402
|
-
st15:
|
1403
|
-
if ( ++p == pe )
|
1404
|
-
goto _test_eof15;
|
1405
|
-
case 15:
|
1406
|
-
switch( (*p) ) {
|
1407
|
-
case 42: goto st15;
|
1408
|
-
case 47: goto st2;
|
1409
|
-
}
|
1410
|
-
goto st14;
|
1411
|
-
st16:
|
1412
|
-
if ( ++p == pe )
|
1413
|
-
goto _test_eof16;
|
1414
|
-
case 16:
|
1415
|
-
if ( (*p) == 10 )
|
1416
|
-
goto st2;
|
1417
|
-
goto st16;
|
1418
|
-
}
|
1419
|
-
_test_eof2: cs = 2; goto _test_eof;
|
1420
|
-
_test_eof3: cs = 3; goto _test_eof;
|
1421
|
-
_test_eof4: cs = 4; goto _test_eof;
|
1422
|
-
_test_eof5: cs = 5; goto _test_eof;
|
1423
|
-
_test_eof6: cs = 6; goto _test_eof;
|
1424
|
-
_test_eof7: cs = 7; goto _test_eof;
|
1425
|
-
_test_eof8: cs = 8; goto _test_eof;
|
1426
|
-
_test_eof9: cs = 9; goto _test_eof;
|
1427
|
-
_test_eof10: cs = 10; goto _test_eof;
|
1428
|
-
_test_eof11: cs = 11; goto _test_eof;
|
1429
|
-
_test_eof12: cs = 12; goto _test_eof;
|
1430
|
-
_test_eof17: cs = 17; goto _test_eof;
|
1431
|
-
_test_eof13: cs = 13; goto _test_eof;
|
1432
|
-
_test_eof14: cs = 14; goto _test_eof;
|
1433
|
-
_test_eof15: cs = 15; goto _test_eof;
|
1434
|
-
_test_eof16: cs = 16; goto _test_eof;
|
1435
|
-
|
1436
|
-
_test_eof: {}
|
1437
|
-
_out: {}
|
1438
|
-
}
|
1439
|
-
|
1440
|
-
#line 452 "parser.rl"
|
1441
|
-
|
1442
|
-
if(cs >= JSON_array_first_final) {
|
1443
|
-
return p + 1;
|
1444
|
-
} else {
|
1445
|
-
raise_parse_error("unexpected token at '%s'", p);
|
1446
|
-
return NULL;
|
1447
|
-
}
|
1448
|
-
}
|
1031
|
+
case '"': {
|
1032
|
+
// %r{\A"[^"\\\t\n\x00]*(?:\\[bfnrtu\\/"][^"\\]*)*"}
|
1033
|
+
return json_parse_string(state, config, false);
|
1034
|
+
break;
|
1035
|
+
}
|
1036
|
+
case '[': {
|
1037
|
+
state->cursor++;
|
1038
|
+
json_eat_whitespace(state);
|
1039
|
+
long stack_head = state->stack->head;
|
1040
|
+
|
1041
|
+
if ((state->cursor < state->end) && (*state->cursor == ']')) {
|
1042
|
+
state->cursor++;
|
1043
|
+
return PUSH(json_decode_array(state, config, 0));
|
1044
|
+
} else {
|
1045
|
+
state->current_nesting++;
|
1046
|
+
if (RB_UNLIKELY(config->max_nesting && (config->max_nesting < state->current_nesting))) {
|
1047
|
+
rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
|
1048
|
+
}
|
1049
|
+
state->in_array++;
|
1050
|
+
json_parse_any(state, config);
|
1051
|
+
}
|
1449
1052
|
|
1450
|
-
|
1451
|
-
|
1452
|
-
{
|
1453
|
-
VALUE result = Qnil;
|
1454
|
-
size_t bufferSize = stringEnd - string;
|
1455
|
-
char *p = string, *pe = string, *unescape, *bufferStart, *buffer;
|
1456
|
-
int unescape_len;
|
1457
|
-
char buf[4];
|
1053
|
+
while (true) {
|
1054
|
+
json_eat_whitespace(state);
|
1458
1055
|
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1467
|
-
bufferStart = buffer = ALLOCA_N(char, bufferSize ? bufferSize : 1);
|
1468
|
-
# else
|
1469
|
-
bufferStart = buffer = ALLOCA_N(char, bufferSize);
|
1470
|
-
# endif
|
1471
|
-
}
|
1056
|
+
if (state->cursor < state->end) {
|
1057
|
+
if (*state->cursor == ']') {
|
1058
|
+
state->cursor++;
|
1059
|
+
long count = state->stack->head - stack_head;
|
1060
|
+
state->current_nesting--;
|
1061
|
+
state->in_array--;
|
1062
|
+
return PUSH(json_decode_array(state, config, count));
|
1063
|
+
}
|
1472
1064
|
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
buffer += pe - p;
|
1480
|
-
}
|
1481
|
-
switch (*++pe) {
|
1482
|
-
case 'n':
|
1483
|
-
unescape = (char *) "\n";
|
1484
|
-
break;
|
1485
|
-
case 'r':
|
1486
|
-
unescape = (char *) "\r";
|
1487
|
-
break;
|
1488
|
-
case 't':
|
1489
|
-
unescape = (char *) "\t";
|
1490
|
-
break;
|
1491
|
-
case '"':
|
1492
|
-
unescape = (char *) "\"";
|
1493
|
-
break;
|
1494
|
-
case '\\':
|
1495
|
-
unescape = (char *) "\\";
|
1496
|
-
break;
|
1497
|
-
case 'b':
|
1498
|
-
unescape = (char *) "\b";
|
1499
|
-
break;
|
1500
|
-
case 'f':
|
1501
|
-
unescape = (char *) "\f";
|
1502
|
-
break;
|
1503
|
-
case 'u':
|
1504
|
-
if (pe > stringEnd - 4) {
|
1505
|
-
if (bufferSize > MAX_STACK_BUFFER_SIZE) {
|
1506
|
-
ruby_xfree(bufferStart);
|
1507
|
-
}
|
1508
|
-
raise_parse_error("incomplete unicode character escape sequence at '%s'", p);
|
1509
|
-
} else {
|
1510
|
-
uint32_t ch = unescape_unicode((unsigned char *) ++pe);
|
1511
|
-
pe += 3;
|
1512
|
-
/* To handle values above U+FFFF, we take a sequence of
|
1513
|
-
* \uXXXX escapes in the U+D800..U+DBFF then
|
1514
|
-
* U+DC00..U+DFFF ranges, take the low 10 bits from each
|
1515
|
-
* to make a 20-bit number, then add 0x10000 to get the
|
1516
|
-
* final codepoint.
|
1517
|
-
*
|
1518
|
-
* See Unicode 15: 3.8 "Surrogates", 5.3 "Handling
|
1519
|
-
* Surrogate Pairs in UTF-16", and 23.6 "Surrogates
|
1520
|
-
* Area".
|
1521
|
-
*/
|
1522
|
-
if ((ch & 0xFC00) == 0xD800) {
|
1523
|
-
pe++;
|
1524
|
-
if (pe > stringEnd - 6) {
|
1525
|
-
if (bufferSize > MAX_STACK_BUFFER_SIZE) {
|
1526
|
-
ruby_xfree(bufferStart);
|
1527
|
-
}
|
1528
|
-
raise_parse_error("incomplete surrogate pair at '%s'", p);
|
1529
|
-
}
|
1530
|
-
if (pe[0] == '\\' && pe[1] == 'u') {
|
1531
|
-
uint32_t sur = unescape_unicode((unsigned char *) pe + 2);
|
1532
|
-
ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
|
1533
|
-
| (sur & 0x3FF));
|
1534
|
-
pe += 5;
|
1535
|
-
} else {
|
1536
|
-
unescape = (char *) "?";
|
1537
|
-
break;
|
1065
|
+
if (*state->cursor == ',') {
|
1066
|
+
state->cursor++;
|
1067
|
+
if (config->allow_trailing_comma) {
|
1068
|
+
json_eat_whitespace(state);
|
1069
|
+
if ((state->cursor < state->end) && (*state->cursor == ']')) {
|
1070
|
+
continue;
|
1538
1071
|
}
|
1539
1072
|
}
|
1540
|
-
|
1541
|
-
|
1073
|
+
json_parse_any(state, config);
|
1074
|
+
continue;
|
1542
1075
|
}
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
continue;
|
1076
|
+
}
|
1077
|
+
|
1078
|
+
raise_parse_error("expected ',' or ']' after array value", state->cursor);
|
1547
1079
|
}
|
1548
|
-
|
1549
|
-
buffer += unescape_len;
|
1550
|
-
p = ++pe;
|
1551
|
-
} else {
|
1552
|
-
pe++;
|
1080
|
+
break;
|
1553
1081
|
}
|
1554
|
-
|
1082
|
+
case '{': {
|
1083
|
+
state->cursor++;
|
1084
|
+
json_eat_whitespace(state);
|
1085
|
+
long stack_head = state->stack->head;
|
1086
|
+
|
1087
|
+
if ((state->cursor < state->end) && (*state->cursor == '}')) {
|
1088
|
+
state->cursor++;
|
1089
|
+
return PUSH(json_decode_object(state, config, 0));
|
1090
|
+
} else {
|
1091
|
+
state->current_nesting++;
|
1092
|
+
if (RB_UNLIKELY(config->max_nesting && (config->max_nesting < state->current_nesting))) {
|
1093
|
+
rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
|
1094
|
+
}
|
1555
1095
|
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1096
|
+
if (*state->cursor != '"') {
|
1097
|
+
raise_parse_error("expected object key, got '%s", state->cursor);
|
1098
|
+
}
|
1099
|
+
json_parse_string(state, config, true);
|
1560
1100
|
|
1561
|
-
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1565
|
-
|
1566
|
-
}
|
1567
|
-
if (bufferSize > MAX_STACK_BUFFER_SIZE) {
|
1568
|
-
ruby_xfree(bufferStart);
|
1569
|
-
}
|
1570
|
-
# else
|
1571
|
-
result = rb_utf8_str_new(bufferStart, (long)(buffer - bufferStart));
|
1101
|
+
json_eat_whitespace(state);
|
1102
|
+
if ((state->cursor >= state->end) || (*state->cursor != ':')) {
|
1103
|
+
raise_parse_error("expected ':' after object key", state->cursor);
|
1104
|
+
}
|
1105
|
+
state->cursor++;
|
1572
1106
|
|
1573
|
-
|
1574
|
-
|
1575
|
-
}
|
1107
|
+
json_parse_any(state, config);
|
1108
|
+
}
|
1576
1109
|
|
1577
|
-
|
1578
|
-
|
1579
|
-
// Starting from MRI 2.8 it is preferable to freeze the string
|
1580
|
-
// before deduplication so that it can be interned directly
|
1581
|
-
// otherwise it would be duplicated first which is wasteful.
|
1582
|
-
result = rb_funcall(rb_str_freeze(result), i_uminus, 0);
|
1583
|
-
# elif STR_UMINUS_DEDUPE
|
1584
|
-
// MRI 2.5 and older do not deduplicate strings that are already
|
1585
|
-
// frozen.
|
1586
|
-
result = rb_funcall(result, i_uminus, 0);
|
1587
|
-
# else
|
1588
|
-
result = rb_str_freeze(result);
|
1589
|
-
# endif
|
1590
|
-
}
|
1591
|
-
# endif
|
1110
|
+
while (true) {
|
1111
|
+
json_eat_whitespace(state);
|
1592
1112
|
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1113
|
+
if (state->cursor < state->end) {
|
1114
|
+
if (*state->cursor == '}') {
|
1115
|
+
state->cursor++;
|
1116
|
+
state->current_nesting--;
|
1117
|
+
long count = state->stack->head - stack_head;
|
1118
|
+
return PUSH(json_decode_object(state, config, count));
|
1119
|
+
}
|
1596
1120
|
|
1597
|
-
|
1598
|
-
|
1121
|
+
if (*state->cursor == ',') {
|
1122
|
+
state->cursor++;
|
1123
|
+
json_eat_whitespace(state);
|
1599
1124
|
|
1125
|
+
if (config->allow_trailing_comma) {
|
1126
|
+
if ((state->cursor < state->end) && (*state->cursor == '}')) {
|
1127
|
+
continue;
|
1128
|
+
}
|
1129
|
+
}
|
1600
1130
|
|
1601
|
-
|
1602
|
-
|
1603
|
-
|
1604
|
-
|
1131
|
+
if (*state->cursor != '"') {
|
1132
|
+
raise_parse_error("expected object key, got: '%s'", state->cursor);
|
1133
|
+
}
|
1134
|
+
json_parse_string(state, config, true);
|
1605
1135
|
|
1606
|
-
|
1136
|
+
json_eat_whitespace(state);
|
1137
|
+
if ((state->cursor >= state->end) || (*state->cursor != ':')) {
|
1138
|
+
raise_parse_error("expected ':' after object key, got: '%s", state->cursor);
|
1139
|
+
}
|
1140
|
+
state->cursor++;
|
1607
1141
|
|
1142
|
+
json_parse_any(state, config);
|
1608
1143
|
|
1609
|
-
|
1144
|
+
continue;
|
1145
|
+
}
|
1146
|
+
}
|
1610
1147
|
|
1148
|
+
raise_parse_error("expected ',' or '}' after object value, got: '%s'", state->cursor);
|
1149
|
+
}
|
1150
|
+
break;
|
1151
|
+
}
|
1611
1152
|
|
1612
|
-
|
1613
|
-
|
1614
|
-
|
1615
|
-
if (regexp == Qundef) return ST_STOP;
|
1616
|
-
if (RTEST(rb_funcall(klass, i_json_creatable_p, 0)) &&
|
1617
|
-
RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
|
1618
|
-
rb_ary_push(memo, klass);
|
1619
|
-
return ST_STOP;
|
1153
|
+
default:
|
1154
|
+
raise_parse_error("unexpected character: '%s'", state->cursor);
|
1155
|
+
break;
|
1620
1156
|
}
|
1621
|
-
|
1157
|
+
|
1158
|
+
raise_parse_error("unreacheable: '%s'", state->cursor);
|
1622
1159
|
}
|
1623
1160
|
|
1624
|
-
static
|
1161
|
+
static void json_ensure_eof(JSON_ParserState *state)
|
1625
1162
|
{
|
1626
|
-
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
#line 1631 "parser.c"
|
1631
|
-
{
|
1632
|
-
cs = JSON_string_start;
|
1633
|
-
}
|
1634
|
-
|
1635
|
-
#line 650 "parser.rl"
|
1636
|
-
json->memo = p;
|
1637
|
-
|
1638
|
-
#line 1639 "parser.c"
|
1639
|
-
{
|
1640
|
-
if ( p == pe )
|
1641
|
-
goto _test_eof;
|
1642
|
-
switch ( cs )
|
1643
|
-
{
|
1644
|
-
case 1:
|
1645
|
-
if ( (*p) == 34 )
|
1646
|
-
goto st2;
|
1647
|
-
goto st0;
|
1648
|
-
st0:
|
1649
|
-
cs = 0;
|
1650
|
-
goto _out;
|
1651
|
-
st2:
|
1652
|
-
if ( ++p == pe )
|
1653
|
-
goto _test_eof2;
|
1654
|
-
case 2:
|
1655
|
-
switch( (*p) ) {
|
1656
|
-
case 34: goto tr2;
|
1657
|
-
case 92: goto st3;
|
1658
|
-
}
|
1659
|
-
if ( 0 <= (signed char)(*(p)) && (*(p)) <= 31 )
|
1660
|
-
goto st0;
|
1661
|
-
goto st2;
|
1662
|
-
tr2:
|
1663
|
-
#line 617 "parser.rl"
|
1664
|
-
{
|
1665
|
-
*result = json_string_unescape(json->memo + 1, p, json->parsing_name || json-> freeze, json->parsing_name && json->symbolize_names);
|
1666
|
-
if (NIL_P(*result)) {
|
1667
|
-
p--;
|
1668
|
-
{p++; cs = 8; goto _out;}
|
1669
|
-
} else {
|
1670
|
-
{p = (( p + 1))-1;}
|
1671
|
-
}
|
1672
|
-
}
|
1673
|
-
#line 627 "parser.rl"
|
1674
|
-
{ p--; {p++; cs = 8; goto _out;} }
|
1675
|
-
goto st8;
|
1676
|
-
st8:
|
1677
|
-
if ( ++p == pe )
|
1678
|
-
goto _test_eof8;
|
1679
|
-
case 8:
|
1680
|
-
#line 1681 "parser.c"
|
1681
|
-
goto st0;
|
1682
|
-
st3:
|
1683
|
-
if ( ++p == pe )
|
1684
|
-
goto _test_eof3;
|
1685
|
-
case 3:
|
1686
|
-
if ( (*p) == 117 )
|
1687
|
-
goto st4;
|
1688
|
-
if ( 0 <= (signed char)(*(p)) && (*(p)) <= 31 )
|
1689
|
-
goto st0;
|
1690
|
-
goto st2;
|
1691
|
-
st4:
|
1692
|
-
if ( ++p == pe )
|
1693
|
-
goto _test_eof4;
|
1694
|
-
case 4:
|
1695
|
-
if ( (*p) < 65 ) {
|
1696
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1697
|
-
goto st5;
|
1698
|
-
} else if ( (*p) > 70 ) {
|
1699
|
-
if ( 97 <= (*p) && (*p) <= 102 )
|
1700
|
-
goto st5;
|
1701
|
-
} else
|
1702
|
-
goto st5;
|
1703
|
-
goto st0;
|
1704
|
-
st5:
|
1705
|
-
if ( ++p == pe )
|
1706
|
-
goto _test_eof5;
|
1707
|
-
case 5:
|
1708
|
-
if ( (*p) < 65 ) {
|
1709
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1710
|
-
goto st6;
|
1711
|
-
} else if ( (*p) > 70 ) {
|
1712
|
-
if ( 97 <= (*p) && (*p) <= 102 )
|
1713
|
-
goto st6;
|
1714
|
-
} else
|
1715
|
-
goto st6;
|
1716
|
-
goto st0;
|
1717
|
-
st6:
|
1718
|
-
if ( ++p == pe )
|
1719
|
-
goto _test_eof6;
|
1720
|
-
case 6:
|
1721
|
-
if ( (*p) < 65 ) {
|
1722
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1723
|
-
goto st7;
|
1724
|
-
} else if ( (*p) > 70 ) {
|
1725
|
-
if ( 97 <= (*p) && (*p) <= 102 )
|
1726
|
-
goto st7;
|
1727
|
-
} else
|
1728
|
-
goto st7;
|
1729
|
-
goto st0;
|
1730
|
-
st7:
|
1731
|
-
if ( ++p == pe )
|
1732
|
-
goto _test_eof7;
|
1733
|
-
case 7:
|
1734
|
-
if ( (*p) < 65 ) {
|
1735
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
1736
|
-
goto st2;
|
1737
|
-
} else if ( (*p) > 70 ) {
|
1738
|
-
if ( 97 <= (*p) && (*p) <= 102 )
|
1739
|
-
goto st2;
|
1740
|
-
} else
|
1741
|
-
goto st2;
|
1742
|
-
goto st0;
|
1743
|
-
}
|
1744
|
-
_test_eof2: cs = 2; goto _test_eof;
|
1745
|
-
_test_eof8: cs = 8; goto _test_eof;
|
1746
|
-
_test_eof3: cs = 3; goto _test_eof;
|
1747
|
-
_test_eof4: cs = 4; goto _test_eof;
|
1748
|
-
_test_eof5: cs = 5; goto _test_eof;
|
1749
|
-
_test_eof6: cs = 6; goto _test_eof;
|
1750
|
-
_test_eof7: cs = 7; goto _test_eof;
|
1751
|
-
|
1752
|
-
_test_eof: {}
|
1753
|
-
_out: {}
|
1754
|
-
}
|
1755
|
-
|
1756
|
-
#line 652 "parser.rl"
|
1757
|
-
|
1758
|
-
if (json->create_additions && RTEST(match_string = json->match_string)) {
|
1759
|
-
VALUE klass;
|
1760
|
-
VALUE memo = rb_ary_new2(2);
|
1761
|
-
rb_ary_push(memo, *result);
|
1762
|
-
rb_hash_foreach(match_string, match_i, memo);
|
1763
|
-
klass = rb_ary_entry(memo, 1);
|
1764
|
-
if (RTEST(klass)) {
|
1765
|
-
*result = rb_funcall(klass, i_json_create, 1, *result);
|
1766
|
-
}
|
1767
|
-
}
|
1768
|
-
|
1769
|
-
if (cs >= JSON_string_first_final) {
|
1770
|
-
return p + 1;
|
1771
|
-
} else {
|
1772
|
-
return NULL;
|
1163
|
+
json_eat_whitespace(state);
|
1164
|
+
if (state->cursor != state->end) {
|
1165
|
+
raise_parse_error("unexpected token at end of stream '%s'", state->cursor);
|
1773
1166
|
}
|
1774
1167
|
}
|
1775
1168
|
|
@@ -1789,24 +1182,102 @@ static VALUE convert_encoding(VALUE source)
|
|
1789
1182
|
{
|
1790
1183
|
int encindex = RB_ENCODING_GET(source);
|
1791
1184
|
|
1792
|
-
if (encindex == utf8_encindex) {
|
1185
|
+
if (RB_LIKELY(encindex == utf8_encindex)) {
|
1793
1186
|
return source;
|
1794
1187
|
}
|
1795
1188
|
|
1796
1189
|
if (encindex == binary_encindex) {
|
1797
|
-
// For historical reason, we silently reinterpret binary strings as UTF-8
|
1798
|
-
// TODO: Deprecate in 2.8.0
|
1799
|
-
// TODO: Remove in 3.0.0
|
1190
|
+
// For historical reason, we silently reinterpret binary strings as UTF-8
|
1800
1191
|
return rb_enc_associate_index(rb_str_dup(source), utf8_encindex);
|
1801
1192
|
}
|
1802
1193
|
|
1803
|
-
return
|
1194
|
+
return rb_funcall(source, i_encode, 1, Encoding_UTF_8);
|
1195
|
+
}
|
1196
|
+
|
1197
|
+
static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
|
1198
|
+
{
|
1199
|
+
JSON_ParserConfig *config = (JSON_ParserConfig *)data;
|
1200
|
+
|
1201
|
+
if (key == sym_max_nesting) { config->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
|
1202
|
+
else if (key == sym_allow_nan) { config->allow_nan = RTEST(val); }
|
1203
|
+
else if (key == sym_allow_trailing_comma) { config->allow_trailing_comma = RTEST(val); }
|
1204
|
+
else if (key == sym_symbolize_names) { config->symbolize_names = RTEST(val); }
|
1205
|
+
else if (key == sym_freeze) { config->freeze = RTEST(val); }
|
1206
|
+
else if (key == sym_create_id) { config->create_id = RTEST(val) ? val : Qfalse; }
|
1207
|
+
else if (key == sym_object_class) { config->object_class = RTEST(val) ? val : Qfalse; }
|
1208
|
+
else if (key == sym_array_class) { config->array_class = RTEST(val) ? val : Qfalse; }
|
1209
|
+
else if (key == sym_match_string) { config->match_string = RTEST(val) ? val : Qfalse; }
|
1210
|
+
else if (key == sym_decimal_class) {
|
1211
|
+
if (RTEST(val)) {
|
1212
|
+
if (rb_respond_to(val, i_try_convert)) {
|
1213
|
+
config->decimal_class = val;
|
1214
|
+
config->decimal_method_id = i_try_convert;
|
1215
|
+
} else if (rb_respond_to(val, i_new)) {
|
1216
|
+
config->decimal_class = val;
|
1217
|
+
config->decimal_method_id = i_new;
|
1218
|
+
} else if (RB_TYPE_P(val, T_CLASS)) {
|
1219
|
+
VALUE name = rb_class_name(val);
|
1220
|
+
const char *name_cstr = RSTRING_PTR(name);
|
1221
|
+
const char *last_colon = strrchr(name_cstr, ':');
|
1222
|
+
if (last_colon) {
|
1223
|
+
const char *mod_path_end = last_colon - 1;
|
1224
|
+
VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
|
1225
|
+
config->decimal_class = rb_path_to_class(mod_path);
|
1226
|
+
|
1227
|
+
const char *method_name_beg = last_colon + 1;
|
1228
|
+
long before_len = method_name_beg - name_cstr;
|
1229
|
+
long len = RSTRING_LEN(name) - before_len;
|
1230
|
+
VALUE method_name = rb_str_substr(name, before_len, len);
|
1231
|
+
config->decimal_method_id = SYM2ID(rb_str_intern(method_name));
|
1232
|
+
} else {
|
1233
|
+
config->decimal_class = rb_mKernel;
|
1234
|
+
config->decimal_method_id = SYM2ID(rb_str_intern(name));
|
1235
|
+
}
|
1236
|
+
}
|
1237
|
+
}
|
1238
|
+
}
|
1239
|
+
else if (key == sym_create_additions) {
|
1240
|
+
if (NIL_P(val)) {
|
1241
|
+
config->create_additions = true;
|
1242
|
+
config->deprecated_create_additions = true;
|
1243
|
+
} else {
|
1244
|
+
config->create_additions = RTEST(val);
|
1245
|
+
config->deprecated_create_additions = false;
|
1246
|
+
}
|
1247
|
+
}
|
1248
|
+
|
1249
|
+
return ST_CONTINUE;
|
1250
|
+
}
|
1251
|
+
|
1252
|
+
static void parser_config_init(JSON_ParserConfig *config, VALUE opts)
|
1253
|
+
{
|
1254
|
+
config->max_nesting = 100;
|
1255
|
+
|
1256
|
+
if (!NIL_P(opts)) {
|
1257
|
+
Check_Type(opts, T_HASH);
|
1258
|
+
if (RHASH_SIZE(opts) > 0) {
|
1259
|
+
// We assume in most cases few keys are set so it's faster to go over
|
1260
|
+
// the provided keys than to check all possible keys.
|
1261
|
+
rb_hash_foreach(opts, parser_config_init_i, (VALUE)config);
|
1262
|
+
|
1263
|
+
if (config->symbolize_names && config->create_additions) {
|
1264
|
+
rb_raise(rb_eArgError,
|
1265
|
+
"options :symbolize_names and :create_additions cannot be "
|
1266
|
+
" used in conjunction");
|
1267
|
+
}
|
1268
|
+
|
1269
|
+
if (config->create_additions && !config->create_id) {
|
1270
|
+
config->create_id = rb_funcall(mJSON, i_create_id, 0);
|
1271
|
+
}
|
1272
|
+
}
|
1273
|
+
|
1274
|
+
}
|
1804
1275
|
}
|
1805
1276
|
|
1806
1277
|
/*
|
1807
|
-
* call-seq: new(
|
1278
|
+
* call-seq: new(opts => {})
|
1808
1279
|
*
|
1809
|
-
* Creates a new JSON::Ext::
|
1280
|
+
* Creates a new JSON::Ext::ParserConfig instance.
|
1810
1281
|
*
|
1811
1282
|
* It will be configured by the _opts_ hash. _opts_ can have the following
|
1812
1283
|
* keys:
|
@@ -1835,344 +1306,112 @@ static VALUE convert_encoding(VALUE source)
|
|
1835
1306
|
* (Float) when parsing decimal numbers. This class must accept a single
|
1836
1307
|
* string argument in its constructor.
|
1837
1308
|
*/
|
1838
|
-
static VALUE
|
1309
|
+
static VALUE cParserConfig_initialize(VALUE self, VALUE opts)
|
1839
1310
|
{
|
1840
|
-
|
1841
|
-
GET_PARSER_INIT;
|
1311
|
+
GET_PARSER_CONFIG;
|
1842
1312
|
|
1843
|
-
|
1844
|
-
rb_raise(rb_eTypeError, "already initialized instance");
|
1845
|
-
}
|
1313
|
+
parser_config_init(config, opts);
|
1846
1314
|
|
1847
|
-
|
1848
|
-
|
1849
|
-
|
1850
|
-
|
1851
|
-
|
1852
|
-
Check_Type(argv[1], T_HASH);
|
1853
|
-
if (RHASH_SIZE(argv[1]) > 0) {
|
1854
|
-
opts = argv[1];
|
1855
|
-
}
|
1856
|
-
}
|
1315
|
+
RB_OBJ_WRITTEN(self, Qundef, config->create_id);
|
1316
|
+
RB_OBJ_WRITTEN(self, Qundef, config->object_class);
|
1317
|
+
RB_OBJ_WRITTEN(self, Qundef, config->array_class);
|
1318
|
+
RB_OBJ_WRITTEN(self, Qundef, config->decimal_class);
|
1319
|
+
RB_OBJ_WRITTEN(self, Qundef, config->match_string);
|
1857
1320
|
|
1858
|
-
if (!NIL_P(opts)) {
|
1859
|
-
VALUE tmp = ID2SYM(i_max_nesting);
|
1860
|
-
if (option_given_p(opts, tmp)) {
|
1861
|
-
VALUE max_nesting = rb_hash_aref(opts, tmp);
|
1862
|
-
if (RTEST(max_nesting)) {
|
1863
|
-
Check_Type(max_nesting, T_FIXNUM);
|
1864
|
-
json->max_nesting = FIX2INT(max_nesting);
|
1865
|
-
} else {
|
1866
|
-
json->max_nesting = 0;
|
1867
|
-
}
|
1868
|
-
} else {
|
1869
|
-
json->max_nesting = 100;
|
1870
|
-
}
|
1871
|
-
tmp = ID2SYM(i_allow_nan);
|
1872
|
-
if (option_given_p(opts, tmp)) {
|
1873
|
-
json->allow_nan = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
|
1874
|
-
} else {
|
1875
|
-
json->allow_nan = 0;
|
1876
|
-
}
|
1877
|
-
tmp = ID2SYM(i_symbolize_names);
|
1878
|
-
if (option_given_p(opts, tmp)) {
|
1879
|
-
json->symbolize_names = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
|
1880
|
-
} else {
|
1881
|
-
json->symbolize_names = 0;
|
1882
|
-
}
|
1883
|
-
tmp = ID2SYM(i_freeze);
|
1884
|
-
if (option_given_p(opts, tmp)) {
|
1885
|
-
json->freeze = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
|
1886
|
-
} else {
|
1887
|
-
json->freeze = 0;
|
1888
|
-
}
|
1889
|
-
tmp = ID2SYM(i_create_additions);
|
1890
|
-
if (option_given_p(opts, tmp)) {
|
1891
|
-
json->create_additions = RTEST(rb_hash_aref(opts, tmp));
|
1892
|
-
} else {
|
1893
|
-
json->create_additions = 0;
|
1894
|
-
}
|
1895
|
-
if (json->symbolize_names && json->create_additions) {
|
1896
|
-
rb_raise(rb_eArgError,
|
1897
|
-
"options :symbolize_names and :create_additions cannot be "
|
1898
|
-
" used in conjunction");
|
1899
|
-
}
|
1900
|
-
tmp = ID2SYM(i_create_id);
|
1901
|
-
if (option_given_p(opts, tmp)) {
|
1902
|
-
json->create_id = rb_hash_aref(opts, tmp);
|
1903
|
-
} else {
|
1904
|
-
json->create_id = rb_funcall(mJSON, i_create_id, 0);
|
1905
|
-
}
|
1906
|
-
tmp = ID2SYM(i_object_class);
|
1907
|
-
if (option_given_p(opts, tmp)) {
|
1908
|
-
json->object_class = rb_hash_aref(opts, tmp);
|
1909
|
-
} else {
|
1910
|
-
json->object_class = Qnil;
|
1911
|
-
}
|
1912
|
-
tmp = ID2SYM(i_array_class);
|
1913
|
-
if (option_given_p(opts, tmp)) {
|
1914
|
-
json->array_class = rb_hash_aref(opts, tmp);
|
1915
|
-
} else {
|
1916
|
-
json->array_class = Qnil;
|
1917
|
-
}
|
1918
|
-
tmp = ID2SYM(i_decimal_class);
|
1919
|
-
if (option_given_p(opts, tmp)) {
|
1920
|
-
json->decimal_class = rb_hash_aref(opts, tmp);
|
1921
|
-
} else {
|
1922
|
-
json->decimal_class = Qnil;
|
1923
|
-
}
|
1924
|
-
tmp = ID2SYM(i_match_string);
|
1925
|
-
if (option_given_p(opts, tmp)) {
|
1926
|
-
VALUE match_string = rb_hash_aref(opts, tmp);
|
1927
|
-
json->match_string = RTEST(match_string) ? match_string : Qnil;
|
1928
|
-
} else {
|
1929
|
-
json->match_string = Qnil;
|
1930
|
-
}
|
1931
|
-
} else {
|
1932
|
-
json->max_nesting = 100;
|
1933
|
-
json->allow_nan = 0;
|
1934
|
-
json->create_additions = 0;
|
1935
|
-
json->create_id = Qnil;
|
1936
|
-
json->object_class = Qnil;
|
1937
|
-
json->array_class = Qnil;
|
1938
|
-
json->decimal_class = Qnil;
|
1939
|
-
}
|
1940
|
-
source = convert_encoding(StringValue(source));
|
1941
|
-
StringValue(source);
|
1942
|
-
json->len = RSTRING_LEN(source);
|
1943
|
-
json->source = RSTRING_PTR(source);;
|
1944
|
-
json->Vsource = source;
|
1945
1321
|
return self;
|
1946
1322
|
}
|
1947
1323
|
|
1324
|
+
static VALUE cParser_parse(JSON_ParserConfig *config, VALUE Vsource)
|
1325
|
+
{
|
1326
|
+
Vsource = convert_encoding(StringValue(Vsource));
|
1327
|
+
StringValue(Vsource);
|
1328
|
+
|
1329
|
+
VALUE rvalue_stack_buffer[RVALUE_STACK_INITIAL_CAPA];
|
1330
|
+
rvalue_stack stack = {
|
1331
|
+
.type = RVALUE_STACK_STACK_ALLOCATED,
|
1332
|
+
.ptr = rvalue_stack_buffer,
|
1333
|
+
.capa = RVALUE_STACK_INITIAL_CAPA,
|
1334
|
+
};
|
1948
1335
|
|
1949
|
-
|
1950
|
-
|
1951
|
-
|
1952
|
-
|
1336
|
+
JSON_ParserState _state = {
|
1337
|
+
.cursor = RSTRING_PTR(Vsource),
|
1338
|
+
.end = RSTRING_END(Vsource),
|
1339
|
+
.stack = &stack,
|
1340
|
+
};
|
1341
|
+
JSON_ParserState *state = &_state;
|
1953
1342
|
|
1954
|
-
|
1343
|
+
VALUE result = json_parse_any(state, config);
|
1955
1344
|
|
1345
|
+
// This may be skipped in case of exception, but
|
1346
|
+
// it won't cause a leak.
|
1347
|
+
rvalue_stack_eagerly_release(state->stack_handle);
|
1956
1348
|
|
1957
|
-
|
1349
|
+
json_ensure_eof(state);
|
1958
1350
|
|
1351
|
+
return result;
|
1352
|
+
}
|
1959
1353
|
|
1960
1354
|
/*
|
1961
|
-
* call-seq: parse()
|
1355
|
+
* call-seq: parse(source)
|
1962
1356
|
*
|
1963
1357
|
* Parses the current JSON text _source_ and returns the complete data
|
1964
1358
|
* structure as a result.
|
1965
1359
|
* It raises JSON::ParserError if fail to parse.
|
1966
1360
|
*/
|
1967
|
-
static VALUE
|
1361
|
+
static VALUE cParserConfig_parse(VALUE self, VALUE Vsource)
|
1968
1362
|
{
|
1969
|
-
|
1970
|
-
|
1971
|
-
|
1972
|
-
|
1973
|
-
|
1974
|
-
|
1975
|
-
|
1976
|
-
|
1977
|
-
|
1978
|
-
|
1979
|
-
|
1980
|
-
|
1981
|
-
|
1982
|
-
|
1983
|
-
|
1984
|
-
#line 1985 "parser.c"
|
1985
|
-
{
|
1986
|
-
if ( p == pe )
|
1987
|
-
goto _test_eof;
|
1988
|
-
switch ( cs )
|
1989
|
-
{
|
1990
|
-
st1:
|
1991
|
-
if ( ++p == pe )
|
1992
|
-
goto _test_eof1;
|
1993
|
-
case 1:
|
1994
|
-
switch( (*p) ) {
|
1995
|
-
case 13: goto st1;
|
1996
|
-
case 32: goto st1;
|
1997
|
-
case 34: goto tr2;
|
1998
|
-
case 45: goto tr2;
|
1999
|
-
case 47: goto st6;
|
2000
|
-
case 73: goto tr2;
|
2001
|
-
case 78: goto tr2;
|
2002
|
-
case 91: goto tr2;
|
2003
|
-
case 102: goto tr2;
|
2004
|
-
case 110: goto tr2;
|
2005
|
-
case 116: goto tr2;
|
2006
|
-
case 123: goto tr2;
|
2007
|
-
}
|
2008
|
-
if ( (*p) > 10 ) {
|
2009
|
-
if ( 48 <= (*p) && (*p) <= 57 )
|
2010
|
-
goto tr2;
|
2011
|
-
} else if ( (*p) >= 9 )
|
2012
|
-
goto st1;
|
2013
|
-
goto st0;
|
2014
|
-
st0:
|
2015
|
-
cs = 0;
|
2016
|
-
goto _out;
|
2017
|
-
tr2:
|
2018
|
-
#line 850 "parser.rl"
|
2019
|
-
{
|
2020
|
-
char *np = JSON_parse_value(json, p, pe, &result, 0);
|
2021
|
-
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
|
2022
|
-
}
|
2023
|
-
goto st10;
|
2024
|
-
st10:
|
2025
|
-
if ( ++p == pe )
|
2026
|
-
goto _test_eof10;
|
2027
|
-
case 10:
|
2028
|
-
#line 2029 "parser.c"
|
2029
|
-
switch( (*p) ) {
|
2030
|
-
case 13: goto st10;
|
2031
|
-
case 32: goto st10;
|
2032
|
-
case 47: goto st2;
|
2033
|
-
}
|
2034
|
-
if ( 9 <= (*p) && (*p) <= 10 )
|
2035
|
-
goto st10;
|
2036
|
-
goto st0;
|
2037
|
-
st2:
|
2038
|
-
if ( ++p == pe )
|
2039
|
-
goto _test_eof2;
|
2040
|
-
case 2:
|
2041
|
-
switch( (*p) ) {
|
2042
|
-
case 42: goto st3;
|
2043
|
-
case 47: goto st5;
|
2044
|
-
}
|
2045
|
-
goto st0;
|
2046
|
-
st3:
|
2047
|
-
if ( ++p == pe )
|
2048
|
-
goto _test_eof3;
|
2049
|
-
case 3:
|
2050
|
-
if ( (*p) == 42 )
|
2051
|
-
goto st4;
|
2052
|
-
goto st3;
|
2053
|
-
st4:
|
2054
|
-
if ( ++p == pe )
|
2055
|
-
goto _test_eof4;
|
2056
|
-
case 4:
|
2057
|
-
switch( (*p) ) {
|
2058
|
-
case 42: goto st4;
|
2059
|
-
case 47: goto st10;
|
2060
|
-
}
|
2061
|
-
goto st3;
|
2062
|
-
st5:
|
2063
|
-
if ( ++p == pe )
|
2064
|
-
goto _test_eof5;
|
2065
|
-
case 5:
|
2066
|
-
if ( (*p) == 10 )
|
2067
|
-
goto st10;
|
2068
|
-
goto st5;
|
2069
|
-
st6:
|
2070
|
-
if ( ++p == pe )
|
2071
|
-
goto _test_eof6;
|
2072
|
-
case 6:
|
2073
|
-
switch( (*p) ) {
|
2074
|
-
case 42: goto st7;
|
2075
|
-
case 47: goto st9;
|
2076
|
-
}
|
2077
|
-
goto st0;
|
2078
|
-
st7:
|
2079
|
-
if ( ++p == pe )
|
2080
|
-
goto _test_eof7;
|
2081
|
-
case 7:
|
2082
|
-
if ( (*p) == 42 )
|
2083
|
-
goto st8;
|
2084
|
-
goto st7;
|
2085
|
-
st8:
|
2086
|
-
if ( ++p == pe )
|
2087
|
-
goto _test_eof8;
|
2088
|
-
case 8:
|
2089
|
-
switch( (*p) ) {
|
2090
|
-
case 42: goto st8;
|
2091
|
-
case 47: goto st1;
|
2092
|
-
}
|
2093
|
-
goto st7;
|
2094
|
-
st9:
|
2095
|
-
if ( ++p == pe )
|
2096
|
-
goto _test_eof9;
|
2097
|
-
case 9:
|
2098
|
-
if ( (*p) == 10 )
|
2099
|
-
goto st1;
|
2100
|
-
goto st9;
|
2101
|
-
}
|
2102
|
-
_test_eof1: cs = 1; goto _test_eof;
|
2103
|
-
_test_eof10: cs = 10; goto _test_eof;
|
2104
|
-
_test_eof2: cs = 2; goto _test_eof;
|
2105
|
-
_test_eof3: cs = 3; goto _test_eof;
|
2106
|
-
_test_eof4: cs = 4; goto _test_eof;
|
2107
|
-
_test_eof5: cs = 5; goto _test_eof;
|
2108
|
-
_test_eof6: cs = 6; goto _test_eof;
|
2109
|
-
_test_eof7: cs = 7; goto _test_eof;
|
2110
|
-
_test_eof8: cs = 8; goto _test_eof;
|
2111
|
-
_test_eof9: cs = 9; goto _test_eof;
|
2112
|
-
|
2113
|
-
_test_eof: {}
|
2114
|
-
_out: {}
|
2115
|
-
}
|
2116
|
-
|
2117
|
-
#line 878 "parser.rl"
|
2118
|
-
|
2119
|
-
if (cs >= JSON_first_final && p == pe) {
|
2120
|
-
return result;
|
2121
|
-
} else {
|
2122
|
-
raise_parse_error("unexpected token at '%s'", p);
|
2123
|
-
return Qnil;
|
2124
|
-
}
|
1363
|
+
GET_PARSER_CONFIG;
|
1364
|
+
return cParser_parse(config, Vsource);
|
1365
|
+
}
|
1366
|
+
|
1367
|
+
static VALUE cParser_m_parse(VALUE klass, VALUE Vsource, VALUE opts)
|
1368
|
+
{
|
1369
|
+
Vsource = convert_encoding(StringValue(Vsource));
|
1370
|
+
StringValue(Vsource);
|
1371
|
+
|
1372
|
+
JSON_ParserConfig _config = {0};
|
1373
|
+
JSON_ParserConfig *config = &_config;
|
1374
|
+
parser_config_init(config, opts);
|
1375
|
+
|
1376
|
+
return cParser_parse(config, Vsource);
|
2125
1377
|
}
|
2126
1378
|
|
2127
|
-
static void
|
1379
|
+
static void JSON_ParserConfig_mark(void *ptr)
|
2128
1380
|
{
|
2129
|
-
|
2130
|
-
rb_gc_mark(
|
2131
|
-
rb_gc_mark(
|
2132
|
-
rb_gc_mark(
|
2133
|
-
rb_gc_mark(
|
2134
|
-
rb_gc_mark(
|
2135
|
-
rb_gc_mark(json->match_string);
|
1381
|
+
JSON_ParserConfig *config = ptr;
|
1382
|
+
rb_gc_mark(config->create_id);
|
1383
|
+
rb_gc_mark(config->object_class);
|
1384
|
+
rb_gc_mark(config->array_class);
|
1385
|
+
rb_gc_mark(config->decimal_class);
|
1386
|
+
rb_gc_mark(config->match_string);
|
2136
1387
|
}
|
2137
1388
|
|
2138
|
-
static void
|
1389
|
+
static void JSON_ParserConfig_free(void *ptr)
|
2139
1390
|
{
|
2140
|
-
|
2141
|
-
|
2142
|
-
ruby_xfree(json);
|
1391
|
+
JSON_ParserConfig *config = ptr;
|
1392
|
+
ruby_xfree(config);
|
2143
1393
|
}
|
2144
1394
|
|
2145
|
-
static size_t
|
1395
|
+
static size_t JSON_ParserConfig_memsize(const void *ptr)
|
2146
1396
|
{
|
2147
|
-
|
2148
|
-
return sizeof(*json) + FBUFFER_CAPA(json->fbuffer);
|
1397
|
+
return sizeof(JSON_ParserConfig);
|
2149
1398
|
}
|
2150
1399
|
|
2151
|
-
static const rb_data_type_t
|
2152
|
-
"JSON/
|
2153
|
-
{
|
1400
|
+
static const rb_data_type_t JSON_ParserConfig_type = {
|
1401
|
+
"JSON::Ext::Parser/ParserConfig",
|
1402
|
+
{
|
1403
|
+
JSON_ParserConfig_mark,
|
1404
|
+
JSON_ParserConfig_free,
|
1405
|
+
JSON_ParserConfig_memsize,
|
1406
|
+
},
|
2154
1407
|
0, 0,
|
2155
|
-
RUBY_TYPED_FREE_IMMEDIATELY,
|
1408
|
+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
|
2156
1409
|
};
|
2157
1410
|
|
2158
1411
|
static VALUE cJSON_parser_s_allocate(VALUE klass)
|
2159
1412
|
{
|
2160
|
-
|
2161
|
-
|
2162
|
-
json->fbuffer = fbuffer_alloc(0);
|
2163
|
-
return obj;
|
2164
|
-
}
|
2165
|
-
|
2166
|
-
/*
|
2167
|
-
* call-seq: source()
|
2168
|
-
*
|
2169
|
-
* Returns a copy of the current _source_ string, that was used to construct
|
2170
|
-
* this Parser.
|
2171
|
-
*/
|
2172
|
-
static VALUE cParser_source(VALUE self)
|
2173
|
-
{
|
2174
|
-
GET_PARSER;
|
2175
|
-
return rb_str_dup(json->Vsource);
|
1413
|
+
JSON_ParserConfig *config;
|
1414
|
+
return TypedData_Make_Struct(klass, JSON_ParserConfig, &JSON_ParserConfig_type, config);
|
2176
1415
|
}
|
2177
1416
|
|
2178
1417
|
void Init_parser(void)
|
@@ -2184,14 +1423,16 @@ void Init_parser(void)
|
|
2184
1423
|
#undef rb_intern
|
2185
1424
|
rb_require("json/common");
|
2186
1425
|
mJSON = rb_define_module("JSON");
|
2187
|
-
mExt = rb_define_module_under(mJSON, "Ext");
|
2188
|
-
|
1426
|
+
VALUE mExt = rb_define_module_under(mJSON, "Ext");
|
1427
|
+
VALUE cParserConfig = rb_define_class_under(mExt, "ParserConfig", rb_cObject);
|
2189
1428
|
eNestingError = rb_path2class("JSON::NestingError");
|
2190
1429
|
rb_gc_register_mark_object(eNestingError);
|
2191
|
-
rb_define_alloc_func(
|
2192
|
-
rb_define_method(
|
2193
|
-
rb_define_method(
|
2194
|
-
|
1430
|
+
rb_define_alloc_func(cParserConfig, cJSON_parser_s_allocate);
|
1431
|
+
rb_define_method(cParserConfig, "initialize", cParserConfig_initialize, 1);
|
1432
|
+
rb_define_method(cParserConfig, "parse", cParserConfig_parse, 1);
|
1433
|
+
|
1434
|
+
VALUE cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
|
1435
|
+
rb_define_singleton_method(cParser, "parse", cParser_m_parse, 2);
|
2195
1436
|
|
2196
1437
|
CNaN = rb_const_get(mJSON, rb_intern("NaN"));
|
2197
1438
|
rb_gc_register_mark_object(CNaN);
|
@@ -2202,36 +1443,36 @@ void Init_parser(void)
|
|
2202
1443
|
CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity"));
|
2203
1444
|
rb_gc_register_mark_object(CMinusInfinity);
|
2204
1445
|
|
1446
|
+
rb_global_variable(&Encoding_UTF_8);
|
1447
|
+
Encoding_UTF_8 = rb_const_get(rb_path2class("Encoding"), rb_intern("UTF_8"));
|
1448
|
+
|
1449
|
+
sym_max_nesting = ID2SYM(rb_intern("max_nesting"));
|
1450
|
+
sym_allow_nan = ID2SYM(rb_intern("allow_nan"));
|
1451
|
+
sym_allow_trailing_comma = ID2SYM(rb_intern("allow_trailing_comma"));
|
1452
|
+
sym_symbolize_names = ID2SYM(rb_intern("symbolize_names"));
|
1453
|
+
sym_freeze = ID2SYM(rb_intern("freeze"));
|
1454
|
+
sym_create_additions = ID2SYM(rb_intern("create_additions"));
|
1455
|
+
sym_create_id = ID2SYM(rb_intern("create_id"));
|
1456
|
+
sym_object_class = ID2SYM(rb_intern("object_class"));
|
1457
|
+
sym_array_class = ID2SYM(rb_intern("array_class"));
|
1458
|
+
sym_decimal_class = ID2SYM(rb_intern("decimal_class"));
|
1459
|
+
sym_match_string = ID2SYM(rb_intern("match_string"));
|
1460
|
+
|
1461
|
+
i_create_id = rb_intern("create_id");
|
2205
1462
|
i_json_creatable_p = rb_intern("json_creatable?");
|
2206
1463
|
i_json_create = rb_intern("json_create");
|
2207
|
-
i_create_id = rb_intern("create_id");
|
2208
|
-
i_create_additions = rb_intern("create_additions");
|
2209
1464
|
i_chr = rb_intern("chr");
|
2210
|
-
i_max_nesting = rb_intern("max_nesting");
|
2211
|
-
i_allow_nan = rb_intern("allow_nan");
|
2212
|
-
i_symbolize_names = rb_intern("symbolize_names");
|
2213
|
-
i_object_class = rb_intern("object_class");
|
2214
|
-
i_array_class = rb_intern("array_class");
|
2215
|
-
i_decimal_class = rb_intern("decimal_class");
|
2216
1465
|
i_match = rb_intern("match");
|
2217
|
-
i_match_string = rb_intern("match_string");
|
2218
1466
|
i_deep_const_get = rb_intern("deep_const_get");
|
2219
1467
|
i_aset = rb_intern("[]=");
|
2220
1468
|
i_aref = rb_intern("[]");
|
2221
1469
|
i_leftshift = rb_intern("<<");
|
2222
1470
|
i_new = rb_intern("new");
|
2223
1471
|
i_try_convert = rb_intern("try_convert");
|
2224
|
-
i_freeze = rb_intern("freeze");
|
2225
1472
|
i_uminus = rb_intern("-@");
|
1473
|
+
i_encode = rb_intern("encode");
|
2226
1474
|
|
2227
1475
|
binary_encindex = rb_ascii8bit_encindex();
|
2228
1476
|
utf8_encindex = rb_utf8_encindex();
|
1477
|
+
enc_utf8 = rb_utf8_encoding();
|
2229
1478
|
}
|
2230
|
-
|
2231
|
-
/*
|
2232
|
-
* Local variables:
|
2233
|
-
* mode: c
|
2234
|
-
* c-file-style: ruby
|
2235
|
-
* indent-tabs-mode: nil
|
2236
|
-
* End:
|
2237
|
-
*/
|