/
oj.c
1996 lines (1896 loc) · 81.7 KB
/
oj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.
#include "oj.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "dump.h"
#include "encode.h"
#include "hash.h"
#include "odd.h"
#include "parse.h"
#include "rails.h"
typedef struct _yesNoOpt {
VALUE sym;
char *attr;
} * YesNoOpt;
void Init_oj();
VALUE Oj = Qnil;
ID oj_add_value_id;
ID oj_array_append_id;
ID oj_array_end_id;
ID oj_array_start_id;
ID oj_as_json_id;
ID oj_begin_id;
ID oj_bigdecimal_id;
ID oj_end_id;
ID oj_exclude_end_id;
ID oj_error_id;
ID oj_file_id;
ID oj_fileno_id;
ID oj_ftype_id;
ID oj_has_key_id;
ID oj_hash_end_id;
ID oj_hash_key_id;
ID oj_hash_set_id;
ID oj_hash_start_id;
ID oj_iconv_id;
ID oj_instance_variables_id;
ID oj_json_create_id;
ID oj_length_id;
ID oj_new_id;
ID oj_parse_id;
ID oj_pos_id;
ID oj_raw_json_id;
ID oj_read_id;
ID oj_readpartial_id;
ID oj_replace_id;
ID oj_stat_id;
ID oj_string_id;
ID oj_to_h_id;
ID oj_to_hash_id;
ID oj_to_json_id;
ID oj_to_s_id;
ID oj_to_sym_id;
ID oj_to_time_id;
ID oj_tv_nsec_id;
ID oj_tv_sec_id;
ID oj_tv_usec_id;
ID oj_utc_id;
ID oj_utc_offset_id;
ID oj_utcq_id;
ID oj_write_id;
VALUE oj_bag_class;
VALUE oj_bigdecimal_class;
VALUE oj_cstack_class;
VALUE oj_date_class;
VALUE oj_datetime_class;
VALUE oj_enumerable_class;
VALUE oj_parse_error_class;
VALUE oj_stream_writer_class;
VALUE oj_string_writer_class;
VALUE oj_stringio_class;
VALUE oj_struct_class;
VALUE oj_slash_string;
VALUE oj_allow_nan_sym;
VALUE oj_array_class_sym;
VALUE oj_create_additions_sym;
VALUE oj_decimal_class_sym;
VALUE oj_hash_class_sym;
VALUE oj_indent_sym;
VALUE oj_object_class_sym;
VALUE oj_quirks_mode_sym;
VALUE oj_safe_sym;
VALUE oj_trace_sym;
static VALUE allow_blank_sym;
static VALUE allow_gc_sym;
static VALUE allow_invalid_unicode_sym;
static VALUE ascii_sym;
static VALUE auto_define_sym;
static VALUE auto_sym;
static VALUE bigdecimal_as_decimal_sym;
static VALUE bigdecimal_load_sym;
static VALUE bigdecimal_sym;
static VALUE cache_keys_sym;
static VALUE cache_str_sym;
static VALUE circular_sym;
static VALUE class_cache_sym;
static VALUE compat_bigdecimal_sym;
static VALUE compat_sym;
static VALUE create_id_sym;
static VALUE custom_sym;
static VALUE empty_string_sym;
static VALUE escape_mode_sym;
static VALUE integer_range_sym;
static VALUE fast_sym;
static VALUE float_prec_sym;
static VALUE float_sym;
static VALUE huge_sym;
static VALUE ignore_sym;
static VALUE ignore_under_sym;
static VALUE json_sym;
static VALUE match_string_sym;
static VALUE mode_sym;
static VALUE nan_sym;
static VALUE newline_sym;
static VALUE nilnil_sym;
static VALUE null_sym;
static VALUE object_sym;
static VALUE omit_nil_sym;
static VALUE rails_sym;
static VALUE raise_sym;
static VALUE ruby_sym;
static VALUE sec_prec_sym;
static VALUE strict_sym;
static VALUE symbol_keys_sym;
static VALUE time_format_sym;
static VALUE unicode_xss_sym;
static VALUE unix_sym;
static VALUE unix_zone_sym;
static VALUE use_as_json_sym;
static VALUE use_raw_json_sym;
static VALUE use_to_hash_sym;
static VALUE use_to_json_sym;
static VALUE wab_sym;
static VALUE word_sym;
static VALUE xmlschema_sym;
static VALUE xss_safe_sym;
rb_encoding *oj_utf8_encoding = 0;
#ifdef HAVE_PTHREAD_MUTEX_INIT
pthread_mutex_t oj_cache_mutex;
#else
VALUE oj_cache_mutex = Qnil;
#endif
const char oj_json_class[] = "json_class";
struct _options oj_default_options = {
0, // indent
No, // circular
No, // auto_define
No, // sym_key
JSONEsc, // escape_mode
ObjectMode, // mode
Yes, // class_cache
UnixTime, // time_format
NotSet, // bigdec_as_num
AutoDec, // bigdec_load
false, // compat_bigdec
No, // to_hash
No, // to_json
No, // as_json
No, // raw_json
No, // nilnil
Yes, // empty_string
Yes, // allow_gc
Yes, // quirks_mode
No, // allow_invalid
No, // create_ok
Yes, // allow_nan
No, // trace
No, // safe
false, // sec_prec_set
No, // ignore_under
Yes, // cache_keys
3, // cache_str
0, // int_range_min
0, // int_range_max
oj_json_class, // create_id
10, // create_id_len
9, // sec_prec
16, // float_prec
"%0.15g", // float_fmt
Qnil, // hash_class
Qnil, // array_class
{
// dump_opts
false, // use
"", // indent
"", // before_sep
"", // after_sep
"", // hash_nl
"", // array_nl
0, // indent_size
0, // before_size
0, // after_size
0, // hash_size
0, // array_size
AutoNan, // nan_dump
false, // omit_nil
MAX_DEPTH, // max_depth
},
{
// str_rx
NULL, // head
NULL, // tail
{'\0'}, // err
},
NULL, // ignore
};
/* Document-method: default_options()
* call-seq: default_options()
*
* Returns the default load and dump options as a Hash. The options are
* - *:indent* [_Fixnum_|_String_|_nil_] number of spaces to indent each element in an JSON
*document, zero or nil is no newline between JSON elements, negative indicates no newline between
*top level JSON elements in a stream, a String indicates the string should be used for indentation
* - *:circular* [_Boolean_|_nil_] support circular references while dumping as well as shared
*references
* - *:auto_define* [_Boolean_|_nil_] automatically define classes if they do not exist
* - *:symbol_keys* [_Boolean_|_nil_] use symbols instead of strings for hash keys
* - *:escape_mode* [_:newline_|_:json_|_:xss_safe_|_:ascii_|_unicode_xss_|_nil_] determines the
*characters to escape
* - *:class_cache* [_Boolean_|_nil_] cache classes for faster parsing (if dynamically modifying
*classes or reloading classes then don't use this)
* - *:mode* [_:object_|_:strict_|_:compat_|_:null_|_:custom_|_:rails_|_:wab_] load and dump modes
*to use for JSON
* - *:time_format* [_:unix_|_:unix_zone_|_:xmlschema_|_:ruby_] time format when dumping
* - *:bigdecimal_as_decimal* [_Boolean_|_nil_] dump BigDecimal as a decimal number or as a String
* - *:bigdecimal_load* [_:bigdecimal_|_:float_|_:auto_|_:fast_] load decimals as BigDecimal instead
*of as a Float. :auto pick the most precise for the number of digits. :float should be the same as
*ruby. :fast may require rounding but is must faster.
* - *:compat_bigdecimal* [_true_|_false_] load decimals as BigDecimal instead of as a Float when in
*compat or rails mode.
* - *:create_id* [_String_|_nil_] create id for json compatible object encoding, default is
*'json_class'
* - *:create_additions* [_Boolean_|_nil_] if true allow creation of instances using create_id on
*load.
* - *:second_precision* [_Fixnum_|_nil_] number of digits after the decimal when dumping the
*seconds portion of time
* - *:float_precision* [_Fixnum_|_nil_] number of digits of precision when dumping floats, 0
*indicates use Ruby
* - *:use_to_json* [_Boolean_|_nil_] call to_json() methods on dump, default is false
* - *:use_as_json* [_Boolean_|_nil_] call as_json() methods on dump, default is false
* - *:use_raw_json* [_Boolean_|_nil_] call raw_json() methods on dump, default is false
* - *:nilnil* [_Boolean_|_nil_] if true a nil input to load will return nil and not raise an
*Exception
* - *:empty_string* [_Boolean_|_nil_] if true an empty input will not raise an Exception
* - *:allow_gc* [_Boolean_|_nil_] allow or prohibit GC during parsing, default is true (allow)
* - *:quirks_mode* [_true,_|_false_|_nil_] Allow single JSON values instead of documents, default
*is true (allow)
* - *:allow_invalid_unicode* [_true,_|_false_|_nil_] Allow invalid unicode, default is false (don't
*allow)
* - *:allow_nan* [_true,_|_false_|_nil_] Allow Nan, Infinity, and -Infinity to be parsed, default
*is true (allow)
* - *:indent_str* [_String_|_nil_] String to use for indentation, overriding the indent option is
*not nil
* - *:space* [_String_|_nil_] String to use for the space after the colon in JSON object fields
* - *:space_before* [_String_|_nil_] String to use before the colon separator in JSON object fields
* - *:object_nl* [_String_|_nil_] String to use after a JSON object field value
* - *:array_nl* [_String_|_nil_] String to use after a JSON array value
* - *:nan* [_:null_|_:huge_|_:word_|_:raise_|_:auto_] how to dump Infinity and NaN. :null places a
*null, :huge places a huge number, :word places Infinity or NaN, :raise raises and exception, :auto
*uses default for each mode.
* - *:hash_class* [_Class_|_nil_] Class to use instead of Hash on load, :object_class can also be
*used
* - *:array_class* [_Class_|_nil_] Class to use instead of Array on load
* - *:omit_nil* [_true_|_false_] if true Hash and Object attributes with nil values are omitted
* - *:ignore* [_nil_|_Array_] either nil or an Array of classes to ignore when dumping
* - *:ignore_under* [_Boolean_] if true then attributes that start with _ are ignored when dumping in
*object or custom mode.
* - *:cache_keys* [_Boolean_] if true then hash keys are cached
* - *:cache_str* [_Fixnum_] maximum string value length to cache
* - *:integer_range* [_Range_] Dump integers outside range as strings.
* - *:trace* [_true,_|_false_] Trace all load and dump calls, default is false (trace is off)
* - *:safe* [_true,_|_false_] Safe mimic breaks JSON mimic to be safer, default is false (safe is
*off)
*
* Return [_Hash_] all current option settings.
*/
static VALUE get_def_opts(VALUE self) {
VALUE opts = rb_hash_new();
if (0 == oj_default_options.dump_opts.indent_size) {
rb_hash_aset(opts, oj_indent_sym, INT2FIX(oj_default_options.indent));
} else {
rb_hash_aset(opts, oj_indent_sym, rb_str_new2(oj_default_options.dump_opts.indent_str));
}
rb_hash_aset(opts, sec_prec_sym, INT2FIX(oj_default_options.sec_prec));
rb_hash_aset(opts,
circular_sym,
(Yes == oj_default_options.circular)
? Qtrue
: ((No == oj_default_options.circular) ? Qfalse : Qnil));
rb_hash_aset(opts,
class_cache_sym,
(Yes == oj_default_options.class_cache)
? Qtrue
: ((No == oj_default_options.class_cache) ? Qfalse : Qnil));
rb_hash_aset(opts,
auto_define_sym,
(Yes == oj_default_options.auto_define)
? Qtrue
: ((No == oj_default_options.auto_define) ? Qfalse : Qnil));
rb_hash_aset(opts,
symbol_keys_sym,
(Yes == oj_default_options.sym_key)
? Qtrue
: ((No == oj_default_options.sym_key) ? Qfalse : Qnil));
rb_hash_aset(opts,
bigdecimal_as_decimal_sym,
(Yes == oj_default_options.bigdec_as_num)
? Qtrue
: ((No == oj_default_options.bigdec_as_num) ? Qfalse : Qnil));
rb_hash_aset(opts,
oj_create_additions_sym,
(Yes == oj_default_options.create_ok)
? Qtrue
: ((No == oj_default_options.create_ok) ? Qfalse : Qnil));
rb_hash_aset(opts,
use_to_json_sym,
(Yes == oj_default_options.to_json)
? Qtrue
: ((No == oj_default_options.to_json) ? Qfalse : Qnil));
rb_hash_aset(opts,
use_to_hash_sym,
(Yes == oj_default_options.to_hash)
? Qtrue
: ((No == oj_default_options.to_hash) ? Qfalse : Qnil));
rb_hash_aset(opts,
use_as_json_sym,
(Yes == oj_default_options.as_json)
? Qtrue
: ((No == oj_default_options.as_json) ? Qfalse : Qnil));
rb_hash_aset(opts,
use_raw_json_sym,
(Yes == oj_default_options.raw_json)
? Qtrue
: ((No == oj_default_options.raw_json) ? Qfalse : Qnil));
rb_hash_aset(opts,
nilnil_sym,
(Yes == oj_default_options.nilnil)
? Qtrue
: ((No == oj_default_options.nilnil) ? Qfalse : Qnil));
rb_hash_aset(opts,
empty_string_sym,
(Yes == oj_default_options.empty_string)
? Qtrue
: ((No == oj_default_options.empty_string) ? Qfalse : Qnil));
rb_hash_aset(opts,
allow_gc_sym,
(Yes == oj_default_options.allow_gc)
? Qtrue
: ((No == oj_default_options.allow_gc) ? Qfalse : Qnil));
rb_hash_aset(opts,
oj_quirks_mode_sym,
(Yes == oj_default_options.quirks_mode)
? Qtrue
: ((No == oj_default_options.quirks_mode) ? Qfalse : Qnil));
rb_hash_aset(opts,
allow_invalid_unicode_sym,
(Yes == oj_default_options.allow_invalid)
? Qtrue
: ((No == oj_default_options.allow_invalid) ? Qfalse : Qnil));
rb_hash_aset(opts,
oj_allow_nan_sym,
(Yes == oj_default_options.allow_nan)
? Qtrue
: ((No == oj_default_options.allow_nan) ? Qfalse : Qnil));
rb_hash_aset(opts,
oj_trace_sym,
(Yes == oj_default_options.trace)
? Qtrue
: ((No == oj_default_options.trace) ? Qfalse : Qnil));
rb_hash_aset(opts,
oj_safe_sym,
(Yes == oj_default_options.safe)
? Qtrue
: ((No == oj_default_options.safe) ? Qfalse : Qnil));
rb_hash_aset(opts, float_prec_sym, INT2FIX(oj_default_options.float_prec));
rb_hash_aset(opts, cache_str_sym, INT2FIX(oj_default_options.cache_str));
rb_hash_aset(opts,
ignore_under_sym,
(Yes == oj_default_options.ignore_under)
? Qtrue
: ((No == oj_default_options.ignore_under) ? Qfalse : Qnil));
rb_hash_aset(opts,
cache_keys_sym,
(Yes == oj_default_options.cache_keys)
? Qtrue
: ((No == oj_default_options.cache_keys) ? Qfalse : Qnil));
switch (oj_default_options.mode) {
case StrictMode: rb_hash_aset(opts, mode_sym, strict_sym); break;
case CompatMode: rb_hash_aset(opts, mode_sym, compat_sym); break;
case NullMode: rb_hash_aset(opts, mode_sym, null_sym); break;
case ObjectMode: rb_hash_aset(opts, mode_sym, object_sym); break;
case CustomMode: rb_hash_aset(opts, mode_sym, custom_sym); break;
case RailsMode: rb_hash_aset(opts, mode_sym, rails_sym); break;
case WabMode: rb_hash_aset(opts, mode_sym, wab_sym); break;
default: rb_hash_aset(opts, mode_sym, object_sym); break;
}
if (oj_default_options.int_range_max != 0 || oj_default_options.int_range_min != 0) {
VALUE range = rb_obj_alloc(rb_cRange);
VALUE min = LONG2FIX(oj_default_options.int_range_min);
VALUE max = LONG2FIX(oj_default_options.int_range_max);
rb_ivar_set(range, oj_begin_id, min);
rb_ivar_set(range, oj_end_id, max);
rb_hash_aset(opts, integer_range_sym, range);
} else {
rb_hash_aset(opts, integer_range_sym, Qnil);
}
switch (oj_default_options.escape_mode) {
case NLEsc: rb_hash_aset(opts, escape_mode_sym, newline_sym); break;
case JSONEsc: rb_hash_aset(opts, escape_mode_sym, json_sym); break;
case XSSEsc: rb_hash_aset(opts, escape_mode_sym, xss_safe_sym); break;
case ASCIIEsc: rb_hash_aset(opts, escape_mode_sym, ascii_sym); break;
case JXEsc: rb_hash_aset(opts, escape_mode_sym, unicode_xss_sym); break;
default: rb_hash_aset(opts, escape_mode_sym, json_sym); break;
}
switch (oj_default_options.time_format) {
case XmlTime: rb_hash_aset(opts, time_format_sym, xmlschema_sym); break;
case RubyTime: rb_hash_aset(opts, time_format_sym, ruby_sym); break;
case UnixZTime: rb_hash_aset(opts, time_format_sym, unix_zone_sym); break;
case UnixTime:
default: rb_hash_aset(opts, time_format_sym, unix_sym); break;
}
switch (oj_default_options.bigdec_load) {
case BigDec: rb_hash_aset(opts, bigdecimal_load_sym, bigdecimal_sym); break;
case FloatDec: rb_hash_aset(opts, bigdecimal_load_sym, float_sym); break;
case FastDec: rb_hash_aset(opts, bigdecimal_load_sym, fast_sym); break;
case AutoDec:
default: rb_hash_aset(opts, bigdecimal_load_sym, auto_sym); break;
}
rb_hash_aset(opts, compat_bigdecimal_sym, oj_default_options.compat_bigdec ? Qtrue : Qfalse);
rb_hash_aset(
opts,
create_id_sym,
(NULL == oj_default_options.create_id) ? Qnil : rb_str_new2(oj_default_options.create_id));
rb_hash_aset(opts,
oj_space_sym,
(0 == oj_default_options.dump_opts.after_size)
? Qnil
: rb_str_new2(oj_default_options.dump_opts.after_sep));
rb_hash_aset(opts,
oj_space_before_sym,
(0 == oj_default_options.dump_opts.before_size)
? Qnil
: rb_str_new2(oj_default_options.dump_opts.before_sep));
rb_hash_aset(opts,
oj_object_nl_sym,
(0 == oj_default_options.dump_opts.hash_size)
? Qnil
: rb_str_new2(oj_default_options.dump_opts.hash_nl));
rb_hash_aset(opts,
oj_array_nl_sym,
(0 == oj_default_options.dump_opts.array_size)
? Qnil
: rb_str_new2(oj_default_options.dump_opts.array_nl));
switch (oj_default_options.dump_opts.nan_dump) {
case NullNan: rb_hash_aset(opts, nan_sym, null_sym); break;
case RaiseNan: rb_hash_aset(opts, nan_sym, raise_sym); break;
case WordNan: rb_hash_aset(opts, nan_sym, word_sym); break;
case HugeNan: rb_hash_aset(opts, nan_sym, huge_sym); break;
case AutoNan:
default: rb_hash_aset(opts, nan_sym, auto_sym); break;
}
rb_hash_aset(opts, omit_nil_sym, oj_default_options.dump_opts.omit_nil ? Qtrue : Qfalse);
rb_hash_aset(opts, oj_hash_class_sym, oj_default_options.hash_class);
rb_hash_aset(opts, oj_array_class_sym, oj_default_options.array_class);
if (NULL == oj_default_options.ignore) {
rb_hash_aset(opts, ignore_sym, Qnil);
} else {
VALUE * vp;
volatile VALUE a = rb_ary_new();
for (vp = oj_default_options.ignore; Qnil != *vp; vp++) {
rb_ary_push(a, *vp);
}
rb_hash_aset(opts, ignore_sym, a);
}
return opts;
}
/* Document-method: default_options=
* call-seq: default_options=(opts)
*
* Sets the default options for load and dump.
* - *opts* [_Hash_] options to change
* - *:indent* [_Fixnum_|_String_|_nil_] number of spaces to indent each element in a JSON
*document or the String to use for identation.
* - :circular [_Boolean_|_nil_] support circular references while dumping.
* - *:auto_define* [_Boolean_|_nil_] automatically define classes if they do not exist.
* - *:symbol_keys* [_Boolean_|_nil_] convert hash keys to symbols.
* - *:class_cache* [_Boolean_|_nil_] cache classes for faster parsing.
* - *:escape* [_:newline_|_:json_|_:xss_safe_|_:ascii_|_unicode_xss_|_nil_] mode encodes all
*high-bit characters as escaped sequences if :ascii, :json is standand UTF-8 JSON encoding,
*:newline is the same as :json but newlines are not escaped, :unicode_xss allows unicode but
*escapes &, <, and >, and any \u20xx characters along with some others, and :xss_safe escapes &, <,
*and >, and some others.
* - *:bigdecimal_as_decimal* [_Boolean_|_nil_] dump BigDecimal as a decimal number or as a
*String.
* - *:bigdecimal_load* [_:bigdecimal_|_:float_|_:auto_|_nil_] load decimals as BigDecimal instead
*of as a Float. :auto pick the most precise for the number of digits.
* - *:compat_bigdecimal* [_true_|_false_] load decimals as BigDecimal instead of as a Float in
*compat mode.
* - *:mode* [_:object_|_:strict_|_:compat_|_:null_|_:custom_|_:rails_|_:wab_] load and dump mode
*to use for JSON :strict raises an exception when a non-supported Object is encountered. :compat
*attempts to extract variable values from an Object using to_json() or to_hash() then it walks the
*Object's variables if neither is found. The :object mode ignores to_hash() and to_json() methods
*and encodes variables using code internal to the Oj gem. The :null mode ignores non-supported
*Objects and replaces them with a null. The :custom mode honors all dump options. The :rails more
*mimics rails and Active behavior.
* - *:time_format* [_:unix_|_:xmlschema_|_:ruby_] time format when dumping in :compat mode :unix
*decimal number denoting the number of seconds since 1/1/1970, :unix_zone decimal number denoting
*the number of seconds since 1/1/1970 plus the utc_offset in the exponent, :xmlschema date-time
*format taken from XML Schema as a String, :ruby Time.to_s formatted String.
* - *:create_id* [_String_|_nil_] create id for json compatible object encoding
* - *:create_additions* [_Boolean_|_nil_] if true allow creation of instances using create_id on
*load.
* - *:second_precision* [_Fixnum_|_nil_] number of digits after the decimal when dumping the
*seconds portion of time.
* - *:float_precision* [_Fixnum_|_nil_] number of digits of precision when dumping floats, 0
*indicates use Ruby.
* - *:use_to_json* [_Boolean_|_nil_] call to_json() methods on dump, default is false.
* - *:use_as_json* [_Boolean_|_nil_] call as_json() methods on dump, default is false.
* - *:use_to_hash* [_Boolean_|_nil_] call to_hash() methods on dump, default is false.
* - *:use_raw_json* [_Boolean_|_nil_] call raw_json() methods on dump, default is false.
* - *:nilnil* [_Boolean_|_nil_] if true a nil input to load will return nil and not raise an
*Exception.
* - *:allow_gc* [_Boolean_|_nil_] allow or prohibit GC during parsing, default is true (allow).
* - *:quirks_mode* [_Boolean_|_nil_] allow single JSON values instead of documents, default is
*true (allow).
* - *:allow_invalid_unicode* [_Boolean_|_nil_] allow invalid unicode, default is false (don't
*allow).
* - *:allow_nan* [_Boolean_|_nil_] allow Nan, Infinity, and -Infinity, default is true (allow).
* - *:space* [_String_|_nil_] String to use for the space after the colon in JSON object fields.
* - *:space_before* [_String_|_nil_] String to use before the colon separator in JSON object
*fields.
* - *:object_nl* [_String_|_nil_] String to use after a JSON object field value.
* - *:array_nl* [_String_|_nil_] String to use after a JSON array value
* - *:nan* [_:null_|_:huge_|_:word_|_:raise_] how to dump Infinity and NaN in null, strict, and
*compat mode. :null places a null, :huge places a huge number, :word places Infinity or NaN, :raise
*raises and exception, :auto uses default for each mode.
* - *:hash_class* [_Class_|_nil_] Class to use instead of Hash on load, :object_class can also be
*used.
* - *:array_class* [_Class_|_nil_] Class to use instead of Array on load.
* - *:omit_nil* [_true_|_false_] if true Hash and Object attributes with nil values are omitted.
* - *:ignore* [_nil_|Array] either nil or an Array of classes to ignore when dumping
* - *:ignore_under* [_Boolean_] if true then attributes that start with _ are ignored when
*dumping in object or custom mode.
* - *:cache_keys* [_Boolean_] if true then hash keys are cached
* - *:cache_str* [_Fixnum_] maximum string vsalue length to cache
* - *:integer_range* [_Range_] Dump integers outside range as strings.
* - *:trace* [_Boolean_] turn trace on or off.
* - *:safe* [_Boolean_] turn safe mimic on or off.
*/
static VALUE set_def_opts(VALUE self, VALUE opts) {
Check_Type(opts, T_HASH);
oj_parse_options(opts, &oj_default_options);
return Qnil;
}
void oj_parse_options(VALUE ropts, Options copts) {
struct _yesNoOpt ynos[] = {{circular_sym, &copts->circular},
{auto_define_sym, &copts->auto_define},
{symbol_keys_sym, &copts->sym_key},
{class_cache_sym, &copts->class_cache},
{bigdecimal_as_decimal_sym, &copts->bigdec_as_num},
{use_to_hash_sym, &copts->to_hash},
{use_to_json_sym, &copts->to_json},
{use_as_json_sym, &copts->as_json},
{use_raw_json_sym, &copts->raw_json},
{nilnil_sym, &copts->nilnil},
{allow_blank_sym, &copts->nilnil}, // same as nilnil
{empty_string_sym, &copts->empty_string},
{allow_gc_sym, &copts->allow_gc},
{oj_quirks_mode_sym, &copts->quirks_mode},
{allow_invalid_unicode_sym, &copts->allow_invalid},
{oj_allow_nan_sym, &copts->allow_nan},
{oj_trace_sym, &copts->trace},
{oj_safe_sym, &copts->safe},
{ignore_under_sym, &copts->ignore_under},
{oj_create_additions_sym, &copts->create_ok},
{cache_keys_sym, &copts->cache_keys},
{Qnil, 0}};
YesNoOpt o;
volatile VALUE v;
size_t len;
if (T_HASH != rb_type(ropts)) {
return;
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_indent_sym)) {
v = rb_hash_lookup(ropts, oj_indent_sym);
switch (rb_type(v)) {
case T_NIL:
copts->dump_opts.indent_size = 0;
*copts->dump_opts.indent_str = '\0';
copts->indent = 0;
break;
case T_FIXNUM:
copts->dump_opts.indent_size = 0;
*copts->dump_opts.indent_str = '\0';
copts->indent = FIX2INT(v);
break;
case T_STRING:
if (sizeof(copts->dump_opts.indent_str) <= (len = RSTRING_LEN(v))) {
rb_raise(rb_eArgError,
"indent string is limited to %lu characters.",
(unsigned long)sizeof(copts->dump_opts.indent_str));
}
strcpy(copts->dump_opts.indent_str, StringValuePtr(v));
copts->dump_opts.indent_size = (uint8_t)len;
copts->indent = 0;
break;
default: rb_raise(rb_eTypeError, "indent must be a Fixnum, String, or nil."); break;
}
}
if (Qnil != (v = rb_hash_lookup(ropts, float_prec_sym))) {
int n;
#ifdef RUBY_INTEGER_UNIFICATION
if (rb_cInteger != rb_obj_class(v)) {
rb_raise(rb_eArgError, ":float_precision must be a Integer.");
}
#else
if (T_FIXNUM != rb_type(v)) {
rb_raise(rb_eArgError, ":float_precision must be a Fixnum.");
}
#endif
n = FIX2INT(v);
if (0 >= n) {
*copts->float_fmt = '\0';
copts->float_prec = 0;
} else {
if (20 < n) {
n = 20;
}
sprintf(copts->float_fmt, "%%0.%dg", n);
copts->float_prec = n;
}
}
if (Qnil != (v = rb_hash_lookup(ropts, cache_str_sym))) {
int n;
#ifdef RUBY_INTEGER_UNIFICATION
if (rb_cInteger != rb_obj_class(v)) {
rb_raise(rb_eArgError, ":cache_str must be a Integer.");
}
#else
if (T_FIXNUM != rb_type(v)) {
rb_raise(rb_eArgError, ":cache_str must be a Fixnum.");
}
#endif
n = FIX2INT(v);
if (0 >= n) {
copts->cache_str = 0;
} else {
if (32 < n) {
n = 32;
}
copts->cache_str = (char)n;
}
}
if (Qnil != (v = rb_hash_lookup(ropts, sec_prec_sym))) {
int n;
#ifdef RUBY_INTEGER_UNIFICATION
if (rb_cInteger != rb_obj_class(v)) {
rb_raise(rb_eArgError, ":second_precision must be a Integer.");
}
#else
if (T_FIXNUM != rb_type(v)) {
rb_raise(rb_eArgError, ":second_precision must be a Fixnum.");
}
#endif
n = NUM2INT(v);
if (0 > n) {
n = 0;
copts->sec_prec_set = false;
} else if (9 < n) {
n = 9;
copts->sec_prec_set = true;
} else {
copts->sec_prec_set = true;
}
copts->sec_prec = n;
}
if (Qnil != (v = rb_hash_lookup(ropts, mode_sym))) {
if (wab_sym == v) {
copts->mode = WabMode;
} else if (object_sym == v) {
copts->mode = ObjectMode;
} else if (strict_sym == v) {
copts->mode = StrictMode;
} else if (compat_sym == v || json_sym == v) {
copts->mode = CompatMode;
} else if (null_sym == v) {
copts->mode = NullMode;
} else if (custom_sym == v) {
copts->mode = CustomMode;
} else if (rails_sym == v) {
copts->mode = RailsMode;
} else {
rb_raise(rb_eArgError,
":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
}
}
if (Qnil != (v = rb_hash_lookup(ropts, time_format_sym))) {
if (unix_sym == v) {
copts->time_format = UnixTime;
} else if (unix_zone_sym == v) {
copts->time_format = UnixZTime;
} else if (xmlschema_sym == v) {
copts->time_format = XmlTime;
} else if (ruby_sym == v) {
copts->time_format = RubyTime;
} else {
rb_raise(rb_eArgError, ":time_format must be :unix, :unix_zone, :xmlschema, or :ruby.");
}
}
if (Qnil != (v = rb_hash_lookup(ropts, escape_mode_sym))) {
if (newline_sym == v) {
copts->escape_mode = NLEsc;
} else if (json_sym == v) {
copts->escape_mode = JSONEsc;
} else if (xss_safe_sym == v) {
copts->escape_mode = XSSEsc;
} else if (ascii_sym == v) {
copts->escape_mode = ASCIIEsc;
} else if (unicode_xss_sym == v) {
copts->escape_mode = JXEsc;
} else {
rb_raise(rb_eArgError,
":encoding must be :newline, :json, :xss_safe, :unicode_xss, or :ascii.");
}
}
if (Qnil != (v = rb_hash_lookup(ropts, bigdecimal_load_sym))) {
if (bigdecimal_sym == v || Qtrue == v) {
copts->bigdec_load = BigDec;
} else if (float_sym == v) {
copts->bigdec_load = FloatDec;
} else if (fast_sym == v) {
copts->bigdec_load = FastDec;
} else if (auto_sym == v || Qfalse == v) {
copts->bigdec_load = AutoDec;
} else {
rb_raise(rb_eArgError, ":bigdecimal_load must be :bigdecimal, :float, or :auto.");
}
}
if (Qnil != (v = rb_hash_lookup(ropts, compat_bigdecimal_sym))) {
copts->compat_bigdec = (Qtrue == v);
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_decimal_class_sym)) {
v = rb_hash_lookup(ropts, oj_decimal_class_sym);
if (rb_cFloat == v) {
copts->compat_bigdec = false;
} else if (oj_bigdecimal_class == v) {
copts->compat_bigdec = true;
} else {
rb_raise(rb_eArgError, ":decimal_class must be BigDecimal or Float.");
}
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, create_id_sym)) {
v = rb_hash_lookup(ropts, create_id_sym);
if (Qnil == v) {
if (oj_json_class != oj_default_options.create_id && NULL != copts->create_id) {
xfree((char *)oj_default_options.create_id);
}
copts->create_id = NULL;
copts->create_id_len = 0;
} else if (T_STRING == rb_type(v)) {
const char *str = StringValuePtr(v);
len = RSTRING_LEN(v);
if (len != copts->create_id_len || 0 != strcmp(copts->create_id, str)) {
copts->create_id = ALLOC_N(char, len + 1);
strcpy((char *)copts->create_id, str);
copts->create_id_len = len;
}
} else {
rb_raise(rb_eArgError, ":create_id must be string.");
}
}
for (o = ynos; 0 != o->attr; o++) {
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, o->sym)) {
v = rb_hash_lookup(ropts, o->sym);
if (Qnil == v) {
*o->attr = NotSet;
} else if (Qtrue == v) {
*o->attr = Yes;
} else if (Qfalse == v) {
*o->attr = No;
} else {
rb_raise(rb_eArgError,
"%s must be true, false, or nil.",
rb_id2name(SYM2ID(o->sym)));
}
}
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_space_sym)) {
if (Qnil == (v = rb_hash_lookup(ropts, oj_space_sym))) {
copts->dump_opts.after_size = 0;
*copts->dump_opts.after_sep = '\0';
} else {
rb_check_type(v, T_STRING);
if (sizeof(copts->dump_opts.after_sep) <= (len = RSTRING_LEN(v))) {
rb_raise(rb_eArgError,
"space string is limited to %lu characters.",
(unsigned long)sizeof(copts->dump_opts.after_sep));
}
strcpy(copts->dump_opts.after_sep, StringValuePtr(v));
copts->dump_opts.after_size = (uint8_t)len;
}
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_space_before_sym)) {
if (Qnil == (v = rb_hash_lookup(ropts, oj_space_before_sym))) {
copts->dump_opts.before_size = 0;
*copts->dump_opts.before_sep = '\0';
} else {
rb_check_type(v, T_STRING);
if (sizeof(copts->dump_opts.before_sep) <= (len = RSTRING_LEN(v))) {
rb_raise(rb_eArgError,
"sapce_before string is limited to %lu characters.",
(unsigned long)sizeof(copts->dump_opts.before_sep));
}
strcpy(copts->dump_opts.before_sep, StringValuePtr(v));
copts->dump_opts.before_size = (uint8_t)len;
}
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_object_nl_sym)) {
if (Qnil == (v = rb_hash_lookup(ropts, oj_object_nl_sym))) {
copts->dump_opts.hash_size = 0;
*copts->dump_opts.hash_nl = '\0';
} else {
rb_check_type(v, T_STRING);
if (sizeof(copts->dump_opts.hash_nl) <= (len = RSTRING_LEN(v))) {
rb_raise(rb_eArgError,
"object_nl string is limited to %lu characters.",
(unsigned long)sizeof(copts->dump_opts.hash_nl));
}
strcpy(copts->dump_opts.hash_nl, StringValuePtr(v));
copts->dump_opts.hash_size = (uint8_t)len;
}
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_array_nl_sym)) {
if (Qnil == (v = rb_hash_lookup(ropts, oj_array_nl_sym))) {
copts->dump_opts.array_size = 0;
*copts->dump_opts.array_nl = '\0';
} else {
rb_check_type(v, T_STRING);
if (sizeof(copts->dump_opts.array_nl) <= (len = RSTRING_LEN(v))) {
rb_raise(rb_eArgError,
"array_nl string is limited to %lu characters.",
(unsigned long)sizeof(copts->dump_opts.array_nl));
}
strcpy(copts->dump_opts.array_nl, StringValuePtr(v));
copts->dump_opts.array_size = (uint8_t)len;
}
}
if (Qnil != (v = rb_hash_lookup(ropts, nan_sym))) {
if (null_sym == v) {
copts->dump_opts.nan_dump = NullNan;
} else if (huge_sym == v) {
copts->dump_opts.nan_dump = HugeNan;
} else if (word_sym == v) {
copts->dump_opts.nan_dump = WordNan;
} else if (raise_sym == v) {
copts->dump_opts.nan_dump = RaiseNan;
} else if (auto_sym == v) {
copts->dump_opts.nan_dump = AutoNan;
} else {
rb_raise(rb_eArgError, ":nan must be :null, :huge, :word, :raise, or :auto.");
}
}
copts->dump_opts.use = (0 < copts->dump_opts.indent_size || 0 < copts->dump_opts.after_size ||
0 < copts->dump_opts.before_size || 0 < copts->dump_opts.hash_size ||
0 < copts->dump_opts.array_size);
if (Qnil != (v = rb_hash_lookup(ropts, omit_nil_sym))) {
if (Qtrue == v) {
copts->dump_opts.omit_nil = true;
} else if (Qfalse == v) {
copts->dump_opts.omit_nil = false;
} else {
rb_raise(rb_eArgError, ":omit_nil must be true or false.");
}
}
// This is here only for backwards compatibility with the original Oj.
v = rb_hash_lookup(ropts, oj_ascii_only_sym);
if (Qtrue == v) {
copts->escape_mode = ASCIIEsc;
} else if (Qfalse == v) {
copts->escape_mode = JSONEsc;
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_hash_class_sym)) {
if (Qnil == (v = rb_hash_lookup(ropts, oj_hash_class_sym))) {
copts->hash_class = Qnil;
} else {
rb_check_type(v, T_CLASS);
copts->hash_class = v;
}
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_object_class_sym)) {
if (Qnil == (v = rb_hash_lookup(ropts, oj_object_class_sym))) {
copts->hash_class = Qnil;
} else {
rb_check_type(v, T_CLASS);
copts->hash_class = v;
}
}
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_array_class_sym)) {
if (Qnil == (v = rb_hash_lookup(ropts, oj_array_class_sym))) {
copts->array_class = Qnil;
} else {
rb_check_type(v, T_CLASS);
copts->array_class = v;
}
}
oj_parse_opt_match_string(&copts->str_rx, ropts);
if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, ignore_sym)) {
xfree(copts->ignore);
copts->ignore = NULL;
if (Qnil != (v = rb_hash_lookup(ropts, ignore_sym))) {
int cnt;
rb_check_type(v, T_ARRAY);
cnt = (int)RARRAY_LEN(v);
if (0 < cnt) {
int i;
copts->ignore = ALLOC_N(VALUE, cnt + 1);
for (i = 0; i < cnt; i++) {
copts->ignore[i] = rb_ary_entry(v, i);
}
copts->ignore[i] = Qnil;
}
}
}
if (Qnil != (v = rb_hash_lookup(ropts, integer_range_sym))) {
if (TYPE(v) == T_STRUCT && rb_obj_class(v) == rb_cRange) {
VALUE min = rb_funcall(v, oj_begin_id, 0);
VALUE max = rb_funcall(v, oj_end_id, 0);
if (TYPE(min) != T_FIXNUM || TYPE(max) != T_FIXNUM) {
rb_raise(rb_eArgError, ":integer_range range bounds is not Fixnum.");
}
copts->int_range_min = FIX2LONG(min);
copts->int_range_max = FIX2LONG(max);
} else if (Qfalse != v) {
rb_raise(rb_eArgError, ":integer_range must be a range of Fixnum.");
}
}
}
static int match_string_cb(VALUE key, VALUE value, VALUE rx) {
RxClass rc = (RxClass)rx;
if (T_CLASS != rb_type(value)) {
rb_raise(rb_eArgError, "for :match_string, the hash values must be a Class.");
}
switch (rb_type(key)) {
case T_REGEXP: oj_rxclass_rappend(rc, key, value); break;
case T_STRING:
if (0 != oj_rxclass_append(rc, StringValuePtr(key), value)) {
rb_raise(rb_eArgError, "%s", rc->err);
}
break;
default:
rb_raise(rb_eArgError, "for :match_string, keys must either a String or RegExp.");
break;
}
return ST_CONTINUE;
}
void oj_parse_opt_match_string(RxClass rc, VALUE ropts) {
VALUE v;