oj 3.13.8 → 3.13.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/oj/mimic_json.c CHANGED
@@ -6,8 +6,6 @@
6
6
  #include "oj.h"
7
7
  #include "parse.h"
8
8
 
9
- static VALUE symbolize_names_sym = Qundef;
10
-
11
9
  extern const char oj_json_class[];
12
10
 
13
11
  VALUE oj_array_nl_sym;
@@ -274,7 +272,7 @@ static int mimic_walk(VALUE key, VALUE obj, VALUE proc) {
274
272
  size_t i;
275
273
 
276
274
  for (i = 0; i < cnt; i++) {
277
- mimic_walk(Qnil, rb_ary_entry(obj, i), proc);
275
+ mimic_walk(Qnil, RARRAY_AREF(obj, i), proc);
278
276
  }
279
277
  break;
280
278
  }
@@ -501,6 +499,52 @@ oj_mimic_pretty_generate(int argc, VALUE *argv, VALUE self) {
501
499
  return mimic_generate_core(2, rargs, &copts);
502
500
  }
503
501
 
502
+ static int parse_options_cb(VALUE k, VALUE v, VALUE info) {
503
+ struct _parseInfo *pi = (struct _parseInfo *)info;
504
+
505
+ if (oj_symbolize_names_sym == k) {
506
+ pi->options.sym_key = (Qtrue == v) ? Yes : No;
507
+ } else if (oj_quirks_mode_sym == k) {
508
+ pi->options.quirks_mode = (Qtrue == v) ? Yes : No;
509
+ } else if (oj_create_additions_sym == k) {
510
+ pi->options.create_ok = (Qtrue == v) ? Yes : No;
511
+ } else if (oj_allow_nan_sym == k) {
512
+ pi->options.allow_nan = (Qtrue == v) ? Yes : No;
513
+ } else if (oj_hash_class_sym == k) {
514
+ if (Qnil == v) {
515
+ pi->options.hash_class = Qnil;
516
+ } else {
517
+ rb_check_type(v, T_CLASS);
518
+ pi->options.hash_class = v;
519
+ }
520
+ } else if (oj_object_class_sym == k) {
521
+ if (Qnil == v) {
522
+ pi->options.hash_class = Qnil;
523
+ } else {
524
+ rb_check_type(v, T_CLASS);
525
+ pi->options.hash_class = v;
526
+ }
527
+ } else if (oj_array_class_sym == k) {
528
+ if (Qnil == v) {
529
+ pi->options.array_class = Qnil;
530
+ } else {
531
+ rb_check_type(v, T_CLASS);
532
+ pi->options.array_class = v;
533
+ }
534
+ } else if (oj_decimal_class_sym == k) {
535
+ pi->options.compat_bigdec = (oj_bigdecimal_class == v);
536
+ } else if (oj_max_nesting_sym == k) {
537
+ if (Qtrue == v) {
538
+ pi->max_depth = 100;
539
+ } else if (Qfalse == v || Qnil == v) {
540
+ pi->max_depth = 0;
541
+ } else if (T_FIXNUM == rb_type(v)) {
542
+ pi->max_depth = NUM2INT(v);
543
+ }
544
+ }
545
+ return ST_CONTINUE;
546
+ }
547
+
504
548
  static VALUE mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
505
549
  struct _parseInfo pi;
506
550
  VALUE ropts;
@@ -526,63 +570,11 @@ static VALUE mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
526
570
  pi.max_depth = 100;
527
571
 
528
572
  if (Qnil != ropts) {
529
- VALUE v;
530
-
531
573
  if (T_HASH != rb_type(ropts)) {
532
574
  rb_raise(rb_eArgError, "options must be a hash.");
533
575
  }
534
- if (Qundef == symbolize_names_sym) {
535
- symbolize_names_sym = ID2SYM(rb_intern("symbolize_names"));
536
- rb_gc_register_address(&symbolize_names_sym);
537
- }
538
- if (Qnil != (v = rb_hash_lookup(ropts, symbolize_names_sym))) {
539
- pi.options.sym_key = (Qtrue == v) ? Yes : No;
540
- }
541
- if (Qnil != (v = rb_hash_lookup(ropts, oj_quirks_mode_sym))) {
542
- pi.options.quirks_mode = (Qtrue == v) ? Yes : No;
543
- }
544
- if (Qnil != (v = rb_hash_lookup(ropts, oj_create_additions_sym))) {
545
- pi.options.create_ok = (Qtrue == v) ? Yes : No;
546
- }
547
- if (Qnil != (v = rb_hash_lookup(ropts, oj_allow_nan_sym))) {
548
- pi.options.allow_nan = (Qtrue == v) ? Yes : No;
549
- }
550
576
 
551
- if (oj_hash_has_key(ropts, oj_hash_class_sym)) {
552
- if (Qnil == (v = rb_hash_lookup(ropts, oj_hash_class_sym))) {
553
- pi.options.hash_class = Qnil;
554
- } else {
555
- rb_check_type(v, T_CLASS);
556
- pi.options.hash_class = v;
557
- }
558
- }
559
- if (oj_hash_has_key(ropts, oj_object_class_sym)) {
560
- if (Qnil == (v = rb_hash_lookup(ropts, oj_object_class_sym))) {
561
- pi.options.hash_class = Qnil;
562
- } else {
563
- rb_check_type(v, T_CLASS);
564
- pi.options.hash_class = v;
565
- }
566
- }
567
- if (oj_hash_has_key(ropts, oj_array_class_sym)) {
568
- if (Qnil == (v = rb_hash_lookup(ropts, oj_array_class_sym))) {
569
- pi.options.array_class = Qnil;
570
- } else {
571
- rb_check_type(v, T_CLASS);
572
- pi.options.array_class = v;
573
- }
574
- }
575
- if (oj_hash_has_key(ropts, oj_decimal_class_sym)) {
576
- pi.options.compat_bigdec = (oj_bigdecimal_class == rb_hash_lookup(ropts, oj_decimal_class_sym));
577
- }
578
- v = rb_hash_lookup(ropts, oj_max_nesting_sym);
579
- if (Qtrue == v) {
580
- pi.max_depth = 100;
581
- } else if (Qfalse == v || Qnil == v) {
582
- pi.max_depth = 0;
583
- } else if (T_FIXNUM == rb_type(v)) {
584
- pi.max_depth = NUM2INT(v);
585
- }
577
+ rb_hash_foreach(ropts, parse_options_cb, (VALUE)&pi);
586
578
  oj_parse_opt_match_string(&pi.options.str_rx, ropts);
587
579
  if (Yes == pi.options.create_ok && Yes == pi.options.sym_key) {
588
580
  rb_raise(rb_eArgError, ":symbolize_names and :create_additions can not both be true.");
@@ -852,9 +844,6 @@ void oj_mimic_json_methods(VALUE json) {
852
844
  // Pull in the JSON::State mimic file.
853
845
  state_class = rb_const_get_at(generator, rb_intern("State"));
854
846
  rb_gc_register_mark_object(state_class);
855
-
856
- symbolize_names_sym = ID2SYM(rb_intern("symbolize_names"));
857
- rb_gc_register_address(&symbolize_names_sym);
858
847
  }
859
848
 
860
849
  /* Document-module: JSON
data/ext/oj/object.c CHANGED
@@ -325,7 +325,7 @@ static int hat_value(ParseInfo pi, Val parent, const char *key, size_t klen, vol
325
325
  int i, cnt = (int)RARRAY_LEN(e1);
326
326
 
327
327
  for (i = 0; i < cnt; i++) {
328
- rstr = rb_ary_entry(e1, i);
328
+ rstr = RARRAY_AREF(e1, i);
329
329
  args[i] = rb_funcall(rstr, oj_to_sym_id, 0);
330
330
  }
331
331
  sc = rb_funcall2(rb_cStruct, oj_new_id, cnt, args);
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
- ? Qtrue
313
- : ((No == oj_default_options.circular) ? Qfalse : Qnil));
314
- rb_hash_aset(opts,
315
- class_cache_sym,
316
- (Yes == oj_default_options.class_cache)
317
- ? Qtrue
318
- : ((No == oj_default_options.class_cache) ? Qfalse : Qnil));
319
- rb_hash_aset(opts,
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
- ? Qtrue
328
- : ((No == oj_default_options.sym_key) ? Qfalse : Qnil));
329
- rb_hash_aset(opts,
330
- bigdecimal_as_decimal_sym,
331
- (Yes == oj_default_options.bigdec_as_num)
332
- ? Qtrue
333
- : ((No == oj_default_options.bigdec_as_num) ? Qfalse : Qnil));
334
- rb_hash_aset(opts,
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
- ? Qtrue
363
- : ((No == oj_default_options.nilnil) ? Qfalse : Qnil));
364
- rb_hash_aset(opts,
365
- empty_string_sym,
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
- ? Qtrue
373
- : ((No == oj_default_options.allow_gc) ? Qfalse : Qnil));
374
- rb_hash_aset(opts,
375
- oj_quirks_mode_sym,
376
- (Yes == oj_default_options.quirks_mode)
377
- ? Qtrue
378
- : ((No == oj_default_options.quirks_mode) ? Qfalse : Qnil));
379
- rb_hash_aset(opts,
380
- allow_invalid_unicode_sym,
381
- (Yes == oj_default_options.allow_invalid)
382
- ? Qtrue
383
- : ((No == oj_default_options.allow_invalid) ? Qfalse : Qnil));
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(opts,
402
- ignore_under_sym,
403
- (Yes == oj_default_options.ignore_under)
404
- ? Qtrue
405
- : ((No == oj_default_options.ignore_under) ? Qfalse : Qnil));
406
- rb_hash_aset(opts,
407
- cache_keys_sym,
408
- (Yes == oj_default_options.cache_keys)
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
- create_id_sym,
459
- (NULL == oj_default_options.create_id) ? Qnil : rb_str_new2(oj_default_options.create_id));
460
- rb_hash_aset(opts,
461
- oj_space_sym,
462
- (0 == oj_default_options.dump_opts.after_size)
463
- ? Qnil
464
- : rb_str_new2(oj_default_options.dump_opts.after_sep));
465
- rb_hash_aset(opts,
466
- oj_space_before_sym,
467
- (0 == oj_default_options.dump_opts.before_size)
468
- ? Qnil
469
- : rb_str_new2(oj_default_options.dump_opts.before_sep));
470
- rb_hash_aset(opts,
471
- oj_object_nl_sym,
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 o;
620
-
621
- for (o = ynos; 0 != o->attr; o++) {
622
- if (key == o->sym) {
623
- if (Qnil == value) {
624
- *o->attr = NotSet;
625
- } else if (Qtrue == value) {
626
- *o->attr = Yes;
627
- } else if (Qfalse == value) {
628
- *o->attr = No;
629
- } else {
630
- rb_raise(rb_eArgError,
631
- "%s must be true, false, or nil.",
632
- rb_id2name(key));
633
- }
634
- return true;
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] = rb_ary_entry(v, 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 *out;
1223
+ struct _out * out;
1268
1224
  struct _options *copts;
1269
- int argc;
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 rstr;
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 = &out;
1277
+ arg.out = &out;
1324
1278
  arg.copts = &copts;
1325
- arg.argc = argc;
1326
- arg.argv = 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
@@ -311,6 +311,7 @@ extern VALUE oj_object_nl_sym;
311
311
  extern VALUE oj_quirks_mode_sym;
312
312
  extern VALUE oj_space_before_sym;
313
313
  extern VALUE oj_space_sym;
314
+ extern VALUE oj_symbolize_names_sym;
314
315
  extern VALUE oj_trace_sym;
315
316
 
316
317
  extern VALUE oj_slash_string;
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(rb_ary_entry(ma, i));
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 = rb_ary_entry(rcols, i);
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(rb_ary_entry(row, i), depth, out, true);
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(rb_ary_entry(rows, i), cols, ccnt, d2, out);
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(rb_ary_entry(a, i), d2, out, true);
1284
+ dump_rails_val(RARRAY_AREF(a, i), d2, out, true);
1285
1285
  if (i < cnt) {
1286
1286
  *out->cur++ = ',';
1287
1287
  }
data/ext/oj/wab.c CHANGED
@@ -128,7 +128,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
128
128
  for (i = 0; i <= cnt; i++) {
129
129
  assure_size(out, size);
130
130
  fill_indent(out, d2);
131
- oj_dump_wab_val(rb_ary_entry(a, i), d2, out);
131
+ oj_dump_wab_val(RARRAY_AREF(a, i), d2, out);
132
132
  if (i < cnt) {
133
133
  *out->cur++ = ',';
134
134
  }
data/lib/oj/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '3.13.8'
4
+ VERSION = '3.13.11'
5
5
  end
data/pages/JsonGem.md CHANGED
@@ -1,3 +1,18 @@
1
+ # JSON Quickstart
2
+
3
+ To have Oj universally "take over" many methods on the JSON constant (`load`, `parse`, etc.) with
4
+ their faster Oj counterparts, in a mode that is compatible with the json gem:
5
+
6
+ ```ruby
7
+ Oj.mimic_JSON()
8
+ ```
9
+
10
+ If the project does not already use the json gem, `JSON` will become available.
11
+ If the project does require the json gem, `Oj.mimic_JSON()` should be invoked after the
12
+ json gem has been required.
13
+
14
+ For more details and options, read on...
15
+
1
16
  # Oj JSON Gem Compatibility
2
17
 
3
18
  The `:compat` mode mimics the json gem. The json gem is built around the use