oj 3.13.6 → 3.13.10
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/CHANGELOG.md +27 -0
- data/README.md +9 -0
- data/RELEASE_NOTES.md +6 -0
- data/ext/oj/custom.c +4 -4
- data/ext/oj/dump.c +0 -14
- data/ext/oj/dump.h +14 -1
- data/ext/oj/dump_compat.c +1 -1
- data/ext/oj/dump_object.c +46 -39
- data/ext/oj/dump_strict.c +2 -2
- data/ext/oj/fast.c +13 -3
- data/ext/oj/intern.c +7 -1
- data/ext/oj/mimic_json.c +51 -63
- data/ext/oj/object.c +27 -46
- data/ext/oj/oj.c +109 -153
- data/ext/oj/oj.h +1 -0
- data/ext/oj/parse.c +9 -1
- data/ext/oj/rails.c +5 -5
- data/ext/oj/usual.c +4 -2
- data/ext/oj/wab.c +1 -1
- data/lib/oj/version.rb +1 -1
- data/pages/JsonGem.md +15 -0
- data/pages/Modes.md +6 -3
- data/pages/Rails.md +12 -0
- data/test/bug.rb +16 -0
- data/test/foo.rb +28 -6
- data/test/test_fast.rb +37 -7
- metadata +4 -2
data/ext/oj/object.c
CHANGED
@@ -35,7 +35,7 @@ static VALUE calc_hash_key(ParseInfo pi, Val kval, char k1) {
|
|
35
35
|
return ID2SYM(rb_intern3(kval->key + 1, kval->klen - 1, oj_utf8_encoding));
|
36
36
|
}
|
37
37
|
if (Yes == pi->options.sym_key) {
|
38
|
-
|
38
|
+
return ID2SYM(rb_intern3(kval->key, kval->klen, oj_utf8_encoding));
|
39
39
|
}
|
40
40
|
#if HAVE_RB_ENC_INTERNED_STR
|
41
41
|
rkey = rb_enc_interned_str(kval->key, kval->klen, oj_utf8_encoding);
|
@@ -60,21 +60,16 @@ static VALUE str_to_value(ParseInfo pi, const char *str, size_t len, const char
|
|
60
60
|
}
|
61
61
|
rstr = oj_circ_array_get(pi->circ_array, i);
|
62
62
|
} else {
|
63
|
-
|
63
|
+
rstr = rb_utf8_str_new(str, len);
|
64
64
|
}
|
65
65
|
return rstr;
|
66
66
|
}
|
67
67
|
|
68
|
-
#if (RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8)
|
69
|
-
static VALUE oj_parse_xml_time(const char *str, int len) {
|
70
|
-
return rb_funcall(rb_cTime, oj_parse_id, 1, rb_str_new(str, len));
|
71
|
-
}
|
72
|
-
#else
|
73
68
|
// The much faster approach (4x faster)
|
74
69
|
static int parse_num(const char *str, const char *end, int cnt) {
|
75
|
-
int
|
70
|
+
int n = 0;
|
76
71
|
char c;
|
77
|
-
int
|
72
|
+
int i;
|
78
73
|
|
79
74
|
for (i = cnt; 0 < i; i--, str++) {
|
80
75
|
c = *str;
|
@@ -88,9 +83,9 @@ static int parse_num(const char *str, const char *end, int cnt) {
|
|
88
83
|
|
89
84
|
VALUE
|
90
85
|
oj_parse_xml_time(const char *str, int len) {
|
91
|
-
VALUE
|
86
|
+
VALUE args[8];
|
92
87
|
const char *end = str + len;
|
93
|
-
int
|
88
|
+
int n;
|
94
89
|
|
95
90
|
// year
|
96
91
|
if (0 > (n = parse_num(str, end, 4))) {
|
@@ -201,7 +196,6 @@ oj_parse_xml_time(const char *str, int len) {
|
|
201
196
|
}
|
202
197
|
return rb_funcall2(rb_cTime, oj_new_id, 7, args);
|
203
198
|
}
|
204
|
-
#endif
|
205
199
|
|
206
200
|
static int hat_cstr(ParseInfo pi, Val parent, Val kval, const char *str, size_t len) {
|
207
201
|
const char *key = kval->key;
|
@@ -226,13 +220,10 @@ static int hat_cstr(ParseInfo pi, Val parent, Val kval, const char *str, size_t
|
|
226
220
|
}
|
227
221
|
parent->val = odd->clas;
|
228
222
|
parent->odd_args = oj_odd_alloc_args(odd);
|
229
|
-
} break;
|
230
|
-
case 'm':
|
231
|
-
parent->val = ID2SYM(rb_intern3(str + 1, len - 1, oj_utf8_encoding));
|
232
|
-
break;
|
233
|
-
case 's':
|
234
|
-
parent->val = rb_utf8_str_new(str, len);
|
235
223
|
break;
|
224
|
+
}
|
225
|
+
case 'm': parent->val = ID2SYM(rb_intern3(str + 1, len - 1, oj_utf8_encoding)); break;
|
226
|
+
case 's': parent->val = rb_utf8_str_new(str, len); break;
|
236
227
|
case 'c': // class
|
237
228
|
{
|
238
229
|
VALUE clas = oj_name2class(pi, str, len, Yes == pi->options.auto_define, rb_eArgError);
|
@@ -242,7 +233,8 @@ static int hat_cstr(ParseInfo pi, Val parent, Val kval, const char *str, size_t
|
|
242
233
|
} else {
|
243
234
|
parent->val = clas;
|
244
235
|
}
|
245
|
-
|
236
|
+
break;
|
237
|
+
}
|
246
238
|
case 't': // time
|
247
239
|
parent->val = oj_parse_xml_time(str, (int)len);
|
248
240
|
break;
|
@@ -282,22 +274,21 @@ static int hat_num(ParseInfo pi, Val parent, Val kval, NumInfo ni) {
|
|
282
274
|
VALUE args[8];
|
283
275
|
|
284
276
|
sec_as_time(t, &ti);
|
285
|
-
args[0]
|
286
|
-
args[1]
|
287
|
-
args[2]
|
288
|
-
args[3]
|
289
|
-
args[4]
|
290
|
-
args[5]
|
291
|
-
args[6]
|
277
|
+
args[0] = LONG2NUM((long)(ti.year));
|
278
|
+
args[1] = LONG2NUM(ti.mon);
|
279
|
+
args[2] = LONG2NUM(ti.day);
|
280
|
+
args[3] = LONG2NUM(ti.hour);
|
281
|
+
args[4] = LONG2NUM(ti.min);
|
282
|
+
args[5] = rb_float_new((double)ti.sec + ((double)nsec + 0.5) / 1000000000.0);
|
283
|
+
args[6] = LONG2NUM(ni->exp);
|
292
284
|
parent->val = rb_funcall2(rb_cTime, oj_new_id, 7, args);
|
293
285
|
} else {
|
294
286
|
parent->val = rb_time_nano_new(ni->i, (long)nsec);
|
295
287
|
}
|
296
288
|
}
|
297
289
|
break;
|
298
|
-
case 'i':
|
299
|
-
if (!ni->infinity && !ni->neg && 1 == ni->div && 0 == ni->exp &&
|
300
|
-
0 != pi->circ_array) { // fixnum
|
290
|
+
case 'i': // circular index
|
291
|
+
if (!ni->infinity && !ni->neg && 1 == ni->div && 0 == ni->exp && 0 != pi->circ_array) { // fixnum
|
301
292
|
if (Qnil == parent->val) {
|
302
293
|
parent->val = rb_hash_new();
|
303
294
|
}
|
@@ -334,7 +325,7 @@ static int hat_value(ParseInfo pi, Val parent, const char *key, size_t klen, vol
|
|
334
325
|
int i, cnt = (int)RARRAY_LEN(e1);
|
335
326
|
|
336
327
|
for (i = 0; i < cnt; i++) {
|
337
|
-
rstr =
|
328
|
+
rstr = RARRAY_AREF(e1, i);
|
338
329
|
args[i] = rb_funcall(rstr, oj_to_sym_id, 0);
|
339
330
|
}
|
340
331
|
sc = rb_funcall2(rb_cStruct, oj_new_id, cnt, args);
|
@@ -402,9 +393,7 @@ WHICH_TYPE:
|
|
402
393
|
}
|
403
394
|
break;
|
404
395
|
case T_HASH:
|
405
|
-
rb_hash_aset(parent->val,
|
406
|
-
calc_hash_key(pi, kval, parent->k1),
|
407
|
-
str_to_value(pi, str, len, orig));
|
396
|
+
rb_hash_aset(parent->val, calc_hash_key(pi, kval, parent->k1), str_to_value(pi, str, len, orig));
|
408
397
|
break;
|
409
398
|
case T_STRING:
|
410
399
|
rval = str_to_value(pi, str, len, orig);
|
@@ -481,8 +470,8 @@ WHICH_TYPE:
|
|
481
470
|
rb_hash_aset(parent->val, calc_hash_key(pi, kval, parent->k1), rval);
|
482
471
|
break;
|
483
472
|
case T_OBJECT:
|
484
|
-
if (2 == klen && '^' == *key && 'i' == key[1] && !ni->infinity && !ni->neg &&
|
485
|
-
|
473
|
+
if (2 == klen && '^' == *key && 'i' == key[1] && !ni->infinity && !ni->neg && 1 == ni->div && 0 == ni->exp &&
|
474
|
+
0 != pi->circ_array) { // fixnum
|
486
475
|
oj_circ_array_set(pi->circ_array, parent->val, ni->i);
|
487
476
|
} else {
|
488
477
|
rval = oj_num_as_value(ni);
|
@@ -559,11 +548,7 @@ WHICH_TYPE:
|
|
559
548
|
volatile VALUE *a = RARRAY_PTR(value);
|
560
549
|
|
561
550
|
if (2 != len) {
|
562
|
-
oj_set_error_at(pi,
|
563
|
-
oj_parse_error_class,
|
564
|
-
__FILE__,
|
565
|
-
__LINE__,
|
566
|
-
"invalid hash pair");
|
551
|
+
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "invalid hash pair");
|
567
552
|
return;
|
568
553
|
}
|
569
554
|
rb_hash_aset(parent->val, *a, a[1]);
|
@@ -637,10 +622,7 @@ static void end_hash(ParseInfo pi) {
|
|
637
622
|
} else if (NULL != parent->odd_args) {
|
638
623
|
OddArgs oa = parent->odd_args;
|
639
624
|
|
640
|
-
parent->val = rb_funcall2(oa->odd->create_obj,
|
641
|
-
oa->odd->create_op,
|
642
|
-
oa->odd->attr_cnt,
|
643
|
-
oa->args);
|
625
|
+
parent->val = rb_funcall2(oa->odd->create_obj, oa->odd->create_op, oa->odd->attr_cnt, oa->args);
|
644
626
|
oj_odd_free(oa);
|
645
627
|
parent->odd_args = NULL;
|
646
628
|
}
|
@@ -653,8 +635,7 @@ static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const c
|
|
653
635
|
volatile VALUE rval = Qnil;
|
654
636
|
|
655
637
|
// orig lets us know whether the string was ^r1 or \u005er1
|
656
|
-
if (3 <= len && 0 != pi->circ_array && '^' == orig[0] &&
|
657
|
-
0 == rb_array_len(stack_peek(&pi->stack)->val)) {
|
638
|
+
if (3 <= len && 0 != pi->circ_array && '^' == orig[0] && 0 == rb_array_len(stack_peek(&pi->stack)->val)) {
|
658
639
|
if ('i' == str[1]) {
|
659
640
|
long i = read_long(str + 2, len - 2);
|
660
641
|
|
data/ext/oj/oj.c
CHANGED
@@ -94,6 +94,7 @@ VALUE oj_indent_sym;
|
|
94
94
|
VALUE oj_object_class_sym;
|
95
95
|
VALUE oj_quirks_mode_sym;
|
96
96
|
VALUE oj_safe_sym;
|
97
|
+
VALUE oj_symbolize_names_sym;
|
97
98
|
VALUE oj_trace_sym;
|
98
99
|
|
99
100
|
static VALUE allow_blank_sym;
|
@@ -308,106 +309,76 @@ static VALUE get_def_opts(VALUE self) {
|
|
308
309
|
rb_hash_aset(opts, sec_prec_sym, INT2FIX(oj_default_options.sec_prec));
|
309
310
|
rb_hash_aset(opts,
|
310
311
|
circular_sym,
|
311
|
-
(Yes == oj_default_options.circular)
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
auto_define_sym,
|
321
|
-
(Yes == oj_default_options.auto_define)
|
322
|
-
? Qtrue
|
323
|
-
: ((No == oj_default_options.auto_define) ? Qfalse : Qnil));
|
312
|
+
(Yes == oj_default_options.circular) ? Qtrue : ((No == oj_default_options.circular) ? Qfalse : Qnil));
|
313
|
+
rb_hash_aset(
|
314
|
+
opts,
|
315
|
+
class_cache_sym,
|
316
|
+
(Yes == oj_default_options.class_cache) ? Qtrue : ((No == oj_default_options.class_cache) ? Qfalse : Qnil));
|
317
|
+
rb_hash_aset(
|
318
|
+
opts,
|
319
|
+
auto_define_sym,
|
320
|
+
(Yes == oj_default_options.auto_define) ? Qtrue : ((No == oj_default_options.auto_define) ? Qfalse : Qnil));
|
324
321
|
rb_hash_aset(opts,
|
325
322
|
symbol_keys_sym,
|
326
|
-
(Yes == oj_default_options.sym_key)
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
oj_create_additions_sym,
|
336
|
-
(Yes == oj_default_options.create_ok)
|
337
|
-
? Qtrue
|
338
|
-
: ((No == oj_default_options.create_ok) ? Qfalse : Qnil));
|
323
|
+
(Yes == oj_default_options.sym_key) ? Qtrue : ((No == oj_default_options.sym_key) ? Qfalse : Qnil));
|
324
|
+
rb_hash_aset(
|
325
|
+
opts,
|
326
|
+
bigdecimal_as_decimal_sym,
|
327
|
+
(Yes == oj_default_options.bigdec_as_num) ? Qtrue : ((No == oj_default_options.bigdec_as_num) ? Qfalse : Qnil));
|
328
|
+
rb_hash_aset(
|
329
|
+
opts,
|
330
|
+
oj_create_additions_sym,
|
331
|
+
(Yes == oj_default_options.create_ok) ? Qtrue : ((No == oj_default_options.create_ok) ? Qfalse : Qnil));
|
339
332
|
rb_hash_aset(opts,
|
340
333
|
use_to_json_sym,
|
341
|
-
(Yes == oj_default_options.to_json)
|
342
|
-
? Qtrue
|
343
|
-
: ((No == oj_default_options.to_json) ? Qfalse : Qnil));
|
334
|
+
(Yes == oj_default_options.to_json) ? Qtrue : ((No == oj_default_options.to_json) ? Qfalse : Qnil));
|
344
335
|
rb_hash_aset(opts,
|
345
336
|
use_to_hash_sym,
|
346
|
-
(Yes == oj_default_options.to_hash)
|
347
|
-
? Qtrue
|
348
|
-
: ((No == oj_default_options.to_hash) ? Qfalse : Qnil));
|
337
|
+
(Yes == oj_default_options.to_hash) ? Qtrue : ((No == oj_default_options.to_hash) ? Qfalse : Qnil));
|
349
338
|
rb_hash_aset(opts,
|
350
339
|
use_as_json_sym,
|
351
|
-
(Yes == oj_default_options.as_json)
|
352
|
-
? Qtrue
|
353
|
-
: ((No == oj_default_options.as_json) ? Qfalse : Qnil));
|
340
|
+
(Yes == oj_default_options.as_json) ? Qtrue : ((No == oj_default_options.as_json) ? Qfalse : Qnil));
|
354
341
|
rb_hash_aset(opts,
|
355
342
|
use_raw_json_sym,
|
356
|
-
(Yes == oj_default_options.raw_json)
|
357
|
-
? Qtrue
|
358
|
-
: ((No == oj_default_options.raw_json) ? Qfalse : Qnil));
|
343
|
+
(Yes == oj_default_options.raw_json) ? Qtrue : ((No == oj_default_options.raw_json) ? Qfalse : Qnil));
|
359
344
|
rb_hash_aset(opts,
|
360
345
|
nilnil_sym,
|
361
|
-
(Yes == oj_default_options.nilnil)
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
(Yes == oj_default_options.empty_string)
|
367
|
-
? Qtrue
|
368
|
-
: ((No == oj_default_options.empty_string) ? Qfalse : Qnil));
|
346
|
+
(Yes == oj_default_options.nilnil) ? Qtrue : ((No == oj_default_options.nilnil) ? Qfalse : Qnil));
|
347
|
+
rb_hash_aset(
|
348
|
+
opts,
|
349
|
+
empty_string_sym,
|
350
|
+
(Yes == oj_default_options.empty_string) ? Qtrue : ((No == oj_default_options.empty_string) ? Qfalse : Qnil));
|
369
351
|
rb_hash_aset(opts,
|
370
352
|
allow_gc_sym,
|
371
|
-
(Yes == oj_default_options.allow_gc)
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
rb_hash_aset(opts,
|
385
|
-
oj_allow_nan_sym,
|
386
|
-
(Yes == oj_default_options.allow_nan)
|
387
|
-
? Qtrue
|
388
|
-
: ((No == oj_default_options.allow_nan) ? Qfalse : Qnil));
|
353
|
+
(Yes == oj_default_options.allow_gc) ? Qtrue : ((No == oj_default_options.allow_gc) ? Qfalse : Qnil));
|
354
|
+
rb_hash_aset(
|
355
|
+
opts,
|
356
|
+
oj_quirks_mode_sym,
|
357
|
+
(Yes == oj_default_options.quirks_mode) ? Qtrue : ((No == oj_default_options.quirks_mode) ? Qfalse : Qnil));
|
358
|
+
rb_hash_aset(
|
359
|
+
opts,
|
360
|
+
allow_invalid_unicode_sym,
|
361
|
+
(Yes == oj_default_options.allow_invalid) ? Qtrue : ((No == oj_default_options.allow_invalid) ? Qfalse : Qnil));
|
362
|
+
rb_hash_aset(
|
363
|
+
opts,
|
364
|
+
oj_allow_nan_sym,
|
365
|
+
(Yes == oj_default_options.allow_nan) ? Qtrue : ((No == oj_default_options.allow_nan) ? Qfalse : Qnil));
|
389
366
|
rb_hash_aset(opts,
|
390
367
|
oj_trace_sym,
|
391
|
-
(Yes == oj_default_options.trace)
|
392
|
-
? Qtrue
|
393
|
-
: ((No == oj_default_options.trace) ? Qfalse : Qnil));
|
368
|
+
(Yes == oj_default_options.trace) ? Qtrue : ((No == oj_default_options.trace) ? Qfalse : Qnil));
|
394
369
|
rb_hash_aset(opts,
|
395
370
|
oj_safe_sym,
|
396
|
-
(Yes == oj_default_options.safe)
|
397
|
-
? Qtrue
|
398
|
-
: ((No == oj_default_options.safe) ? Qfalse : Qnil));
|
371
|
+
(Yes == oj_default_options.safe) ? Qtrue : ((No == oj_default_options.safe) ? Qfalse : Qnil));
|
399
372
|
rb_hash_aset(opts, float_prec_sym, INT2FIX(oj_default_options.float_prec));
|
400
373
|
rb_hash_aset(opts, cache_str_sym, INT2FIX(oj_default_options.cache_str));
|
401
|
-
rb_hash_aset(
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
? Qtrue
|
410
|
-
: ((No == oj_default_options.cache_keys) ? Qfalse : Qnil));
|
374
|
+
rb_hash_aset(
|
375
|
+
opts,
|
376
|
+
ignore_under_sym,
|
377
|
+
(Yes == oj_default_options.ignore_under) ? Qtrue : ((No == oj_default_options.ignore_under) ? Qfalse : Qnil));
|
378
|
+
rb_hash_aset(
|
379
|
+
opts,
|
380
|
+
cache_keys_sym,
|
381
|
+
(Yes == oj_default_options.cache_keys) ? Qtrue : ((No == oj_default_options.cache_keys) ? Qfalse : Qnil));
|
411
382
|
switch (oj_default_options.mode) {
|
412
383
|
case StrictMode: rb_hash_aset(opts, mode_sym, strict_sym); break;
|
413
384
|
case CompatMode: rb_hash_aset(opts, mode_sym, compat_sym); break;
|
@@ -453,30 +424,25 @@ static VALUE get_def_opts(VALUE self) {
|
|
453
424
|
default: rb_hash_aset(opts, bigdecimal_load_sym, auto_sym); break;
|
454
425
|
}
|
455
426
|
rb_hash_aset(opts, compat_bigdecimal_sym, oj_default_options.compat_bigdec ? Qtrue : Qfalse);
|
427
|
+
rb_hash_aset(opts,
|
428
|
+
create_id_sym,
|
429
|
+
(NULL == oj_default_options.create_id) ? Qnil : rb_str_new2(oj_default_options.create_id));
|
456
430
|
rb_hash_aset(
|
457
431
|
opts,
|
458
|
-
|
459
|
-
(
|
460
|
-
rb_hash_aset(
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
(0 == oj_default_options.dump_opts.hash_size)
|
473
|
-
? Qnil
|
474
|
-
: rb_str_new2(oj_default_options.dump_opts.hash_nl));
|
475
|
-
rb_hash_aset(opts,
|
476
|
-
oj_array_nl_sym,
|
477
|
-
(0 == oj_default_options.dump_opts.array_size)
|
478
|
-
? Qnil
|
479
|
-
: rb_str_new2(oj_default_options.dump_opts.array_nl));
|
432
|
+
oj_space_sym,
|
433
|
+
(0 == oj_default_options.dump_opts.after_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.after_sep));
|
434
|
+
rb_hash_aset(
|
435
|
+
opts,
|
436
|
+
oj_space_before_sym,
|
437
|
+
(0 == oj_default_options.dump_opts.before_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.before_sep));
|
438
|
+
rb_hash_aset(
|
439
|
+
opts,
|
440
|
+
oj_object_nl_sym,
|
441
|
+
(0 == oj_default_options.dump_opts.hash_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.hash_nl));
|
442
|
+
rb_hash_aset(
|
443
|
+
opts,
|
444
|
+
oj_array_nl_sym,
|
445
|
+
(0 == oj_default_options.dump_opts.array_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.array_nl));
|
480
446
|
|
481
447
|
switch (oj_default_options.dump_opts.nan_dump) {
|
482
448
|
case NullNan: rb_hash_aset(opts, nan_sym, null_sym); break;
|
@@ -584,16 +550,14 @@ static VALUE set_def_opts(VALUE self, VALUE opts) {
|
|
584
550
|
return Qnil;
|
585
551
|
}
|
586
552
|
|
587
|
-
bool oj_hash_has_key(VALUE hash, VALUE key)
|
588
|
-
{
|
553
|
+
bool oj_hash_has_key(VALUE hash, VALUE key) {
|
589
554
|
if (Qundef == rb_hash_lookup2(hash, key, Qundef)) {
|
590
555
|
return false;
|
591
556
|
}
|
592
557
|
return true;
|
593
558
|
}
|
594
559
|
|
595
|
-
bool set_yesno_options(VALUE key, VALUE value, Options copts)
|
596
|
-
{
|
560
|
+
bool set_yesno_options(VALUE key, VALUE value, Options copts) {
|
597
561
|
struct _yesNoOpt ynos[] = {{circular_sym, &copts->circular},
|
598
562
|
{auto_define_sym, &copts->auto_define},
|
599
563
|
{symbol_keys_sym, &copts->sym_key},
|
@@ -616,29 +580,26 @@ bool set_yesno_options(VALUE key, VALUE value, Options copts)
|
|
616
580
|
{oj_create_additions_sym, &copts->create_ok},
|
617
581
|
{cache_keys_sym, &copts->cache_keys},
|
618
582
|
{Qnil, 0}};
|
619
|
-
YesNoOpt
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
}
|
636
|
-
}
|
583
|
+
YesNoOpt o;
|
584
|
+
|
585
|
+
for (o = ynos; 0 != o->attr; o++) {
|
586
|
+
if (key == o->sym) {
|
587
|
+
if (Qnil == value) {
|
588
|
+
*o->attr = NotSet;
|
589
|
+
} else if (Qtrue == value) {
|
590
|
+
*o->attr = Yes;
|
591
|
+
} else if (Qfalse == value) {
|
592
|
+
*o->attr = No;
|
593
|
+
} else {
|
594
|
+
rb_raise(rb_eArgError, "%s must be true, false, or nil.", rb_id2name(key));
|
595
|
+
}
|
596
|
+
return true;
|
597
|
+
}
|
598
|
+
}
|
637
599
|
return false;
|
638
600
|
}
|
639
601
|
|
640
|
-
static int parse_options_cb(VALUE k, VALUE v, VALUE opts)
|
641
|
-
{
|
602
|
+
static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
642
603
|
Options copts = (Options)opts;
|
643
604
|
size_t len;
|
644
605
|
|
@@ -753,8 +714,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts)
|
|
753
714
|
} else if (rails_sym == v) {
|
754
715
|
copts->mode = RailsMode;
|
755
716
|
} else {
|
756
|
-
rb_raise(rb_eArgError,
|
757
|
-
":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
|
717
|
+
rb_raise(rb_eArgError, ":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
|
758
718
|
}
|
759
719
|
} else if (time_format_sym == k) {
|
760
720
|
if (unix_sym == v) {
|
@@ -780,8 +740,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts)
|
|
780
740
|
} else if (unicode_xss_sym == v) {
|
781
741
|
copts->escape_mode = JXEsc;
|
782
742
|
} else {
|
783
|
-
rb_raise(rb_eArgError,
|
784
|
-
":encoding must be :newline, :json, :xss_safe, :unicode_xss, or :ascii.");
|
743
|
+
rb_raise(rb_eArgError, ":encoding must be :newline, :json, :xss_safe, :unicode_xss, or :ascii.");
|
785
744
|
}
|
786
745
|
} else if (bigdecimal_load_sym == k) {
|
787
746
|
if (Qnil == v) {
|
@@ -892,7 +851,6 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts)
|
|
892
851
|
if (Qnil == v) {
|
893
852
|
return ST_CONTINUE;
|
894
853
|
}
|
895
|
-
|
896
854
|
if (null_sym == v) {
|
897
855
|
copts->dump_opts.nan_dump = NullNan;
|
898
856
|
} else if (huge_sym == v) {
|
@@ -910,7 +868,6 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts)
|
|
910
868
|
if (Qnil == v) {
|
911
869
|
return ST_CONTINUE;
|
912
870
|
}
|
913
|
-
|
914
871
|
if (Qtrue == v) {
|
915
872
|
copts->dump_opts.omit_nil = true;
|
916
873
|
} else if (Qfalse == v) {
|
@@ -918,7 +875,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts)
|
|
918
875
|
} else {
|
919
876
|
rb_raise(rb_eArgError, ":omit_nil must be true or false.");
|
920
877
|
}
|
921
|
-
} else if(oj_ascii_only_sym == k) {
|
878
|
+
} else if (oj_ascii_only_sym == k) {
|
922
879
|
// This is here only for backwards compatibility with the original Oj.
|
923
880
|
if (Qtrue == v) {
|
924
881
|
copts->escape_mode = ASCIIEsc;
|
@@ -959,7 +916,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts)
|
|
959
916
|
|
960
917
|
copts->ignore = ALLOC_N(VALUE, cnt + 1);
|
961
918
|
for (i = 0; i < cnt; i++) {
|
962
|
-
copts->ignore[i] =
|
919
|
+
copts->ignore[i] = RARRAY_AREF(v, i);
|
963
920
|
}
|
964
921
|
copts->ignore[i] = Qnil;
|
965
922
|
}
|
@@ -968,7 +925,6 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts)
|
|
968
925
|
if (Qnil == v) {
|
969
926
|
return ST_CONTINUE;
|
970
927
|
}
|
971
|
-
|
972
928
|
if (TYPE(v) == T_STRUCT && rb_obj_class(v) == rb_cRange) {
|
973
929
|
VALUE min = rb_funcall(v, oj_begin_id, 0);
|
974
930
|
VALUE max = rb_funcall(v, oj_end_id, 0);
|
@@ -982,8 +938,12 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts)
|
|
982
938
|
} else if (Qfalse != v) {
|
983
939
|
rb_raise(rb_eArgError, ":integer_range must be a range of Fixnum.");
|
984
940
|
}
|
941
|
+
} else if (symbol_keys_sym == k || oj_symbolize_names_sym == k) {
|
942
|
+
if (Qnil == v) {
|
943
|
+
return ST_CONTINUE;
|
944
|
+
}
|
945
|
+
copts->sym_key = (Qtrue == v) ? Yes : No;
|
985
946
|
}
|
986
|
-
|
987
947
|
return ST_CONTINUE;
|
988
948
|
}
|
989
949
|
|
@@ -1014,9 +974,7 @@ static int match_string_cb(VALUE key, VALUE value, VALUE rx) {
|
|
1014
974
|
rb_raise(rb_eArgError, "%s", rc->err);
|
1015
975
|
}
|
1016
976
|
break;
|
1017
|
-
default:
|
1018
|
-
rb_raise(rb_eArgError, "for :match_string, keys must either a String or RegExp.");
|
1019
|
-
break;
|
977
|
+
default: rb_raise(rb_eArgError, "for :match_string, keys must either a String or RegExp."); break;
|
1020
978
|
}
|
1021
979
|
return ST_CONTINUE;
|
1022
980
|
}
|
@@ -1184,9 +1142,7 @@ static VALUE load_file(int argc, VALUE *argv, VALUE self) {
|
|
1184
1142
|
} else if (wab_sym == v) {
|
1185
1143
|
mode = WabMode;
|
1186
1144
|
} else {
|
1187
|
-
rb_raise(
|
1188
|
-
rb_eArgError,
|
1189
|
-
":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
|
1145
|
+
rb_raise(rb_eArgError, ":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
|
1190
1146
|
}
|
1191
1147
|
}
|
1192
1148
|
}
|
@@ -1264,16 +1220,15 @@ static VALUE safe_load(VALUE self, VALUE doc) {
|
|
1264
1220
|
*/
|
1265
1221
|
|
1266
1222
|
struct dump_arg {
|
1267
|
-
struct _out
|
1223
|
+
struct _out * out;
|
1268
1224
|
struct _options *copts;
|
1269
|
-
int
|
1270
|
-
VALUE *argv;
|
1225
|
+
int argc;
|
1226
|
+
VALUE * argv;
|
1271
1227
|
};
|
1272
1228
|
|
1273
|
-
static VALUE dump_body(VALUE a)
|
1274
|
-
{
|
1229
|
+
static VALUE dump_body(VALUE a) {
|
1275
1230
|
volatile struct dump_arg *arg = (void *)a;
|
1276
|
-
VALUE
|
1231
|
+
VALUE rstr;
|
1277
1232
|
|
1278
1233
|
oj_dump_obj_to_json_using_params(*arg->argv, arg->copts, arg->out, arg->argc - 1, arg->argv + 1);
|
1279
1234
|
if (0 == arg->out->buf) {
|
@@ -1285,8 +1240,7 @@ static VALUE dump_body(VALUE a)
|
|
1285
1240
|
return rstr;
|
1286
1241
|
}
|
1287
1242
|
|
1288
|
-
static VALUE dump_ensure(VALUE a)
|
1289
|
-
{
|
1243
|
+
static VALUE dump_ensure(VALUE a) {
|
1290
1244
|
volatile struct dump_arg *arg = (void *)a;
|
1291
1245
|
|
1292
1246
|
if (arg->out->allocated) {
|
@@ -1320,10 +1274,10 @@ static VALUE dump(int argc, VALUE *argv, VALUE self) {
|
|
1320
1274
|
if (CompatMode == copts.mode && copts.escape_mode != ASCIIEsc) {
|
1321
1275
|
copts.escape_mode = JSONEsc;
|
1322
1276
|
}
|
1323
|
-
arg.out
|
1277
|
+
arg.out = &out;
|
1324
1278
|
arg.copts = &copts;
|
1325
|
-
arg.argc
|
1326
|
-
arg.argv
|
1279
|
+
arg.argc = argc;
|
1280
|
+
arg.argv = argv;
|
1327
1281
|
|
1328
1282
|
arg.out->buf = buf;
|
1329
1283
|
arg.out->end = buf + sizeof(buf) - 10;
|
@@ -2015,6 +1969,8 @@ void Init_oj() {
|
|
2015
1969
|
rb_gc_register_address(&strict_sym);
|
2016
1970
|
symbol_keys_sym = ID2SYM(rb_intern("symbol_keys"));
|
2017
1971
|
rb_gc_register_address(&symbol_keys_sym);
|
1972
|
+
oj_symbolize_names_sym = ID2SYM(rb_intern("symbolize_names"));
|
1973
|
+
rb_gc_register_address(&oj_symbolize_names_sym);
|
2018
1974
|
time_format_sym = ID2SYM(rb_intern("time_format"));
|
2019
1975
|
rb_gc_register_address(&time_format_sym);
|
2020
1976
|
unicode_xss_sym = ID2SYM(rb_intern("unicode_xss"));
|
data/ext/oj/oj.h
CHANGED
data/ext/oj/parse.c
CHANGED
@@ -904,9 +904,17 @@ void oj_set_error_at(ParseInfo pi,
|
|
904
904
|
char * end = p + sizeof(msg) - 2;
|
905
905
|
char * start;
|
906
906
|
Val vp;
|
907
|
+
int mlen;
|
907
908
|
|
908
909
|
va_start(ap, format);
|
909
|
-
|
910
|
+
mlen = vsnprintf(msg, sizeof(msg) - 1, format, ap);
|
911
|
+
if (0 < mlen) {
|
912
|
+
if (sizeof(msg) - 2 < (size_t)mlen) {
|
913
|
+
p = end - 2;
|
914
|
+
} else {
|
915
|
+
p += mlen;
|
916
|
+
}
|
917
|
+
}
|
910
918
|
va_end(ap);
|
911
919
|
pi->err.clas = err_clas;
|
912
920
|
if (p + 3 < end) {
|
data/ext/oj/rails.c
CHANGED
@@ -157,7 +157,7 @@ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
|
|
157
157
|
assure_size(out, 2);
|
158
158
|
*out->cur++ = '{';
|
159
159
|
for (i = 0; i < cnt; i++) {
|
160
|
-
volatile VALUE s = rb_sym2str(
|
160
|
+
volatile VALUE s = rb_sym2str(RARRAY_AREF(ma, i));
|
161
161
|
|
162
162
|
name = RSTRING_PTR(s);
|
163
163
|
len = (int)RSTRING_LEN(s);
|
@@ -383,7 +383,7 @@ static StrLen columns_array(VALUE rcols, int *ccnt) {
|
|
383
383
|
*ccnt = cnt;
|
384
384
|
cols = ALLOC_N(struct _strLen, cnt);
|
385
385
|
for (i = 0, cp = cols; i < cnt; i++, cp++) {
|
386
|
-
v =
|
386
|
+
v = RARRAY_AREF(rcols, i);
|
387
387
|
if (T_STRING != rb_type(v)) {
|
388
388
|
v = rb_funcall(v, oj_to_s_id, 0);
|
389
389
|
}
|
@@ -420,7 +420,7 @@ static void dump_row(VALUE row, StrLen cols, int ccnt, int depth, Out out) {
|
|
420
420
|
}
|
421
421
|
oj_dump_cstr(cols->str, cols->len, 0, 0, out);
|
422
422
|
*out->cur++ = ':';
|
423
|
-
dump_rails_val(
|
423
|
+
dump_rails_val(RARRAY_AREF(row, i), depth, out, true);
|
424
424
|
if (i < ccnt - 1) {
|
425
425
|
*out->cur++ = ',';
|
426
426
|
}
|
@@ -490,7 +490,7 @@ static void dump_activerecord_result(VALUE obj, int depth, Out out, bool as_ok)
|
|
490
490
|
} else {
|
491
491
|
fill_indent(out, d2);
|
492
492
|
}
|
493
|
-
dump_row(
|
493
|
+
dump_row(RARRAY_AREF(rows, i), cols, ccnt, d2, out);
|
494
494
|
if (i < rcnt - 1) {
|
495
495
|
*out->cur++ = ',';
|
496
496
|
}
|
@@ -1281,7 +1281,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
|
|
1281
1281
|
} else {
|
1282
1282
|
fill_indent(out, d2);
|
1283
1283
|
}
|
1284
|
-
dump_rails_val(
|
1284
|
+
dump_rails_val(RARRAY_AREF(a, i), d2, out, true);
|
1285
1285
|
if (i < cnt) {
|
1286
1286
|
*out->cur++ = ',';
|
1287
1287
|
}
|