oj_windows 3.16.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +44 -0
  3. data/LICENSE +21 -0
  4. data/README.md +164 -0
  5. data/ext/oj_windows/buf.h +85 -0
  6. data/ext/oj_windows/cache.c +339 -0
  7. data/ext/oj_windows/cache.h +22 -0
  8. data/ext/oj_windows/cache8.c +105 -0
  9. data/ext/oj_windows/cache8.h +21 -0
  10. data/ext/oj_windows/circarray.c +64 -0
  11. data/ext/oj_windows/circarray.h +22 -0
  12. data/ext/oj_windows/code.c +214 -0
  13. data/ext/oj_windows/code.h +40 -0
  14. data/ext/oj_windows/compat.c +239 -0
  15. data/ext/oj_windows/custom.c +1074 -0
  16. data/ext/oj_windows/debug.c +126 -0
  17. data/ext/oj_windows/dump.c +1556 -0
  18. data/ext/oj_windows/dump.h +110 -0
  19. data/ext/oj_windows/dump_compat.c +901 -0
  20. data/ext/oj_windows/dump_leaf.c +162 -0
  21. data/ext/oj_windows/dump_object.c +710 -0
  22. data/ext/oj_windows/dump_strict.c +405 -0
  23. data/ext/oj_windows/encode.h +16 -0
  24. data/ext/oj_windows/err.c +57 -0
  25. data/ext/oj_windows/err.h +67 -0
  26. data/ext/oj_windows/extconf.rb +77 -0
  27. data/ext/oj_windows/fast.c +1710 -0
  28. data/ext/oj_windows/intern.c +325 -0
  29. data/ext/oj_windows/intern.h +22 -0
  30. data/ext/oj_windows/mem.c +320 -0
  31. data/ext/oj_windows/mem.h +53 -0
  32. data/ext/oj_windows/mimic_json.c +919 -0
  33. data/ext/oj_windows/object.c +726 -0
  34. data/ext/oj_windows/odd.c +245 -0
  35. data/ext/oj_windows/odd.h +43 -0
  36. data/ext/oj_windows/oj.c +2097 -0
  37. data/ext/oj_windows/oj.h +420 -0
  38. data/ext/oj_windows/parse.c +1317 -0
  39. data/ext/oj_windows/parse.h +113 -0
  40. data/ext/oj_windows/parser.c +1600 -0
  41. data/ext/oj_windows/parser.h +103 -0
  42. data/ext/oj_windows/rails.c +1484 -0
  43. data/ext/oj_windows/rails.h +18 -0
  44. data/ext/oj_windows/reader.c +222 -0
  45. data/ext/oj_windows/reader.h +137 -0
  46. data/ext/oj_windows/resolve.c +80 -0
  47. data/ext/oj_windows/resolve.h +12 -0
  48. data/ext/oj_windows/rxclass.c +144 -0
  49. data/ext/oj_windows/rxclass.h +26 -0
  50. data/ext/oj_windows/saj.c +675 -0
  51. data/ext/oj_windows/saj2.c +584 -0
  52. data/ext/oj_windows/saj2.h +23 -0
  53. data/ext/oj_windows/scp.c +187 -0
  54. data/ext/oj_windows/simd.h +47 -0
  55. data/ext/oj_windows/sparse.c +946 -0
  56. data/ext/oj_windows/stream_writer.c +329 -0
  57. data/ext/oj_windows/strict.c +189 -0
  58. data/ext/oj_windows/string_writer.c +517 -0
  59. data/ext/oj_windows/trace.c +72 -0
  60. data/ext/oj_windows/trace.h +55 -0
  61. data/ext/oj_windows/usual.c +1218 -0
  62. data/ext/oj_windows/usual.h +69 -0
  63. data/ext/oj_windows/util.c +136 -0
  64. data/ext/oj_windows/util.h +20 -0
  65. data/ext/oj_windows/val_stack.c +101 -0
  66. data/ext/oj_windows/val_stack.h +151 -0
  67. data/ext/oj_windows/validate.c +46 -0
  68. data/ext/oj_windows/wab.c +584 -0
  69. data/lib/oj/active_support_helper.rb +39 -0
  70. data/lib/oj/bag.rb +95 -0
  71. data/lib/oj/easy_hash.rb +52 -0
  72. data/lib/oj/error.rb +21 -0
  73. data/lib/oj/json.rb +188 -0
  74. data/lib/oj/mimic.rb +301 -0
  75. data/lib/oj/saj.rb +80 -0
  76. data/lib/oj/schandler.rb +143 -0
  77. data/lib/oj/state.rb +135 -0
  78. data/lib/oj/version.rb +4 -0
  79. data/lib/oj_windows/active_support_helper.rb +39 -0
  80. data/lib/oj_windows/bag.rb +95 -0
  81. data/lib/oj_windows/easy_hash.rb +52 -0
  82. data/lib/oj_windows/error.rb +21 -0
  83. data/lib/oj_windows/json.rb +188 -0
  84. data/lib/oj_windows/mimic.rb +301 -0
  85. data/lib/oj_windows/saj.rb +80 -0
  86. data/lib/oj_windows/schandler.rb +143 -0
  87. data/lib/oj_windows/state.rb +135 -0
  88. data/lib/oj_windows/version.rb +4 -0
  89. data/lib/oj_windows.rb +15 -0
  90. data/pages/Advanced.md +38 -0
  91. data/pages/Compatibility.md +49 -0
  92. data/pages/Custom.md +37 -0
  93. data/pages/Encoding.md +61 -0
  94. data/pages/InstallOptions.md +20 -0
  95. data/pages/JsonGem.md +60 -0
  96. data/pages/Modes.md +94 -0
  97. data/pages/Options.md +339 -0
  98. data/pages/Parser.md +134 -0
  99. data/pages/Rails.md +85 -0
  100. data/pages/Security.md +43 -0
  101. data/pages/WAB.md +12 -0
  102. metadata +242 -0
@@ -0,0 +1,2097 @@
1
+ // Copyright (c) 2012 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include "oj.h"
5
+
6
+ #include <errno.h>
7
+ #include <fcntl.h>
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+ #ifdef HAVE_SYS_TYPES_H
12
+ #include <sys/types.h>
13
+ #endif
14
+ #if !defined(_WIN32)
15
+ #include <unistd.h>
16
+ #endif
17
+
18
+ #include "dump.h"
19
+ #include "encode.h"
20
+ #include "intern.h"
21
+ #include "mem.h"
22
+ #include "odd.h"
23
+ #include "parse.h"
24
+ #include "rails.h"
25
+ #include "simd.h"
26
+
27
+ typedef struct _yesNoOpt {
28
+ VALUE sym;
29
+ char *attr;
30
+ } *YesNoOpt;
31
+
32
+ void Init_oj();
33
+
34
+ VALUE Oj = Qnil;
35
+
36
+ ID oj_add_value_id;
37
+ ID oj_array_append_id;
38
+ ID oj_array_end_id;
39
+ ID oj_array_start_id;
40
+ ID oj_as_json_id;
41
+ ID oj_begin_id;
42
+ ID oj_bigdecimal_id;
43
+ ID oj_end_id;
44
+ ID oj_eofq_id;
45
+ ID oj_exclude_end_id;
46
+ ID oj_error_id;
47
+ ID oj_file_id;
48
+ ID oj_fileno_id;
49
+ ID oj_ftype_id;
50
+ ID oj_hash_end_id;
51
+ ID oj_hash_key_id;
52
+ ID oj_hash_set_id;
53
+ ID oj_hash_start_id;
54
+ ID oj_iconv_id;
55
+ ID oj_json_create_id;
56
+ ID oj_length_id;
57
+ ID oj_new_id;
58
+ ID oj_parse_id;
59
+ ID oj_plus_id;
60
+ ID oj_pos_id;
61
+ ID oj_raw_json_id;
62
+ ID oj_read_id;
63
+ ID oj_readpartial_id;
64
+ ID oj_replace_id;
65
+ ID oj_stat_id;
66
+ ID oj_string_id;
67
+ ID oj_to_h_id;
68
+ ID oj_to_hash_id;
69
+ ID oj_to_json_id;
70
+ ID oj_to_s_id;
71
+ ID oj_to_sym_id;
72
+ ID oj_to_time_id;
73
+ ID oj_tv_nsec_id;
74
+ ID oj_tv_sec_id;
75
+ ID oj_tv_usec_id;
76
+ ID oj_utc_id;
77
+ ID oj_utc_offset_id;
78
+ ID oj_utcq_id;
79
+ ID oj_write_id;
80
+
81
+ VALUE oj_bag_class;
82
+ VALUE oj_bigdecimal_class;
83
+ VALUE oj_cstack_class;
84
+ VALUE oj_date_class;
85
+ VALUE oj_datetime_class;
86
+ VALUE oj_enumerable_class;
87
+ VALUE oj_parse_error_class;
88
+ VALUE oj_stream_writer_class;
89
+ VALUE oj_string_writer_class;
90
+ VALUE oj_stringio_class;
91
+ VALUE oj_struct_class;
92
+
93
+ VALUE oj_slash_string;
94
+
95
+ VALUE oj_allow_nan_sym;
96
+ VALUE oj_array_class_sym;
97
+ VALUE oj_create_additions_sym;
98
+ VALUE oj_decimal_class_sym;
99
+ VALUE oj_hash_class_sym;
100
+ VALUE oj_indent_sym;
101
+ VALUE oj_object_class_sym;
102
+ VALUE oj_quirks_mode_sym;
103
+ VALUE oj_safe_sym;
104
+ VALUE oj_symbolize_names_sym;
105
+ VALUE oj_trace_sym;
106
+
107
+ static VALUE allow_blank_sym;
108
+ static VALUE allow_gc_sym;
109
+ static VALUE allow_invalid_unicode_sym;
110
+ static VALUE ascii_sym;
111
+ static VALUE auto_define_sym;
112
+ static VALUE auto_sym;
113
+ static VALUE bigdecimal_as_decimal_sym;
114
+ static VALUE bigdecimal_load_sym;
115
+ static VALUE bigdecimal_sym;
116
+ static VALUE cache_keys_sym;
117
+ static VALUE cache_str_sym;
118
+ static VALUE cache_string_sym;
119
+ static VALUE circular_sym;
120
+ static VALUE class_cache_sym;
121
+ static VALUE compat_bigdecimal_sym;
122
+ static VALUE compat_sym;
123
+ static VALUE create_id_sym;
124
+ static VALUE custom_sym;
125
+ static VALUE empty_string_sym;
126
+ static VALUE escape_mode_sym;
127
+ static VALUE integer_range_sym;
128
+ static VALUE fast_sym;
129
+ static VALUE float_prec_sym;
130
+ static VALUE float_format_sym;
131
+ static VALUE float_sym;
132
+ static VALUE huge_sym;
133
+ static VALUE ignore_sym;
134
+ static VALUE ignore_under_sym;
135
+ static VALUE json_sym;
136
+ static VALUE match_string_sym;
137
+ static VALUE mode_sym;
138
+ static VALUE nan_sym;
139
+ static VALUE newline_sym;
140
+ static VALUE nilnil_sym;
141
+ static VALUE null_sym;
142
+ static VALUE object_sym;
143
+ static VALUE omit_null_byte_sym;
144
+ static VALUE omit_nil_sym;
145
+ static VALUE rails_sym;
146
+ static VALUE raise_sym;
147
+ static VALUE ruby_sym;
148
+ static VALUE sec_prec_sym;
149
+ static VALUE slash_sym;
150
+ static VALUE strict_sym;
151
+ static VALUE symbol_keys_sym;
152
+ static VALUE time_format_sym;
153
+ static VALUE unicode_xss_sym;
154
+ static VALUE unix_sym;
155
+ static VALUE unix_zone_sym;
156
+ static VALUE use_as_json_sym;
157
+ static VALUE use_raw_json_sym;
158
+ static VALUE use_to_hash_sym;
159
+ static VALUE use_to_json_sym;
160
+ static VALUE wab_sym;
161
+ static VALUE word_sym;
162
+ static VALUE xmlschema_sym;
163
+ static VALUE xss_safe_sym;
164
+
165
+ rb_encoding *oj_utf8_encoding = 0;
166
+ int oj_utf8_encoding_index = 0;
167
+
168
+ #if defined(_WIN32)
169
+ CRITICAL_SECTION oj_cache_mutex;
170
+ #elif HAVE_PTHREAD_MUTEX_INIT
171
+ pthread_mutex_t oj_cache_mutex;
172
+ #else
173
+ VALUE oj_cache_mutex = Qnil;
174
+ #endif
175
+
176
+ extern void oj_parser_init();
177
+
178
+ const char oj_json_class[] = "json_class";
179
+
180
+ struct _options oj_default_options = {
181
+ 0, // indent
182
+ No, // circular
183
+ No, // auto_define
184
+ No, // sym_key
185
+ JSONEsc, // escape_mode
186
+ ObjectMode, // mode
187
+ Yes, // class_cache
188
+ UnixTime, // time_format
189
+ NotSet, // bigdec_as_num
190
+ AutoDec, // bigdec_load
191
+ false, // compat_bigdec
192
+ No, // to_hash
193
+ No, // to_json
194
+ No, // as_json
195
+ No, // raw_json
196
+ No, // nilnil
197
+ Yes, // empty_string
198
+ Yes, // allow_gc
199
+ Yes, // quirks_mode
200
+ No, // allow_invalid
201
+ No, // create_ok
202
+ Yes, // allow_nan
203
+ No, // trace
204
+ No, // safe
205
+ false, // sec_prec_set
206
+ No, // ignore_under
207
+ Yes, // cache_keys
208
+ 0, // cache_str
209
+ 0, // int_range_min
210
+ 0, // int_range_max
211
+ oj_json_class, // create_id
212
+ 10, // create_id_len
213
+ 9, // sec_prec
214
+ 16, // float_prec
215
+ "%0.15g", // float_fmt
216
+ Qnil, // hash_class
217
+ Qnil, // array_class
218
+ {
219
+ // dump_opts
220
+ false, // use
221
+ "", // indent
222
+ "", // before_sep
223
+ "", // after_sep
224
+ "", // hash_nl
225
+ "", // array_nl
226
+ 0, // indent_size
227
+ 0, // before_size
228
+ 0, // after_size
229
+ 0, // hash_size
230
+ 0, // array_size
231
+ AutoNan, // nan_dump
232
+ false, // omit_nil
233
+ false, // omit_null_byte
234
+ MAX_DEPTH, // max_depth
235
+ },
236
+ {
237
+ // str_rx
238
+ NULL, // head
239
+ NULL, // tail
240
+ {'\0'}, // err
241
+ },
242
+ NULL,
243
+ };
244
+
245
+ /* Document-method: default_options()
246
+ * call-seq: default_options()
247
+ *
248
+ * Returns the default load and dump options as a Hash. The options are
249
+ * - *:indent* [_Fixnum_|_String_|_nil_] number of spaces to indent each element
250
+ * in an JSON document, zero or nil is no newline between JSON elements,
251
+ * negative indicates no newline between top level JSON elements in a stream,
252
+ * a String indicates the string should be used for indentation
253
+ * - *:circular* [_Boolean_|_nil_] support circular references while dumping as
254
+ * well as shared references
255
+ * - *:auto_define* [_Boolean_|_nil_] automatically define classes if they do not exist
256
+ * - *:symbol_keys* [_Boolean_|_nil_] use symbols instead of strings for hash keys
257
+ * - *:escape_mode* [_:newline_|_:json_|_:slash_|_:xss_safe_|_:ascii_|_:unicode_xss_|_nil_]
258
+ * determines the characters to escape
259
+ * - *:class_cache* [_Boolean_|_nil_] cache classes for faster parsing (if dynamically
260
+ * modifying classes or reloading classes then don't use this)
261
+ * - *:mode* [_:object_|_:strict_|_:compat_|_:null_|_:custom_|_:rails_|_:wab_] load and
262
+ * dump modes to use for JSON
263
+ * - *:time_format* [_:unix_|_:unix_zone_|_:xmlschema_|_:ruby_] time format when dumping
264
+ * - *:bigdecimal_as_decimal* [_Boolean_|_nil_] dump BigDecimal as a decimal number or
265
+ * as a String
266
+ * - *:bigdecimal_load* [_:bigdecimal_|_:float_|_:auto_|_:fast_|_:ruby_] load decimals
267
+ * as BigDecimal instead of as a Float. :auto pick the most precise for the number
268
+ * of digits. :float should be the same as ruby. :fast may require rounding but is
269
+ * must faster.
270
+ * - *:compat_bigdecimal* [_true_|_false_] load decimals as BigDecimal instead of as
271
+ * a Float when in compat or rails mode.
272
+ * - *:create_id* [_String_|_nil_] create id for json compatible object encoding,
273
+ * default is 'json_class'
274
+ * - *:create_additions* [_Boolean_|_nil_] if true allow creation of instances using
275
+ * create_id on load.
276
+ * - *:second_precision* [_Fixnum_|_nil_] number of digits after the decimal when
277
+ * dumping the seconds portion of time
278
+ * - *:float_precision* [_Fixnum_|_nil_] number of digits of precision when dumping
279
+ * floats, 0 indicates use Ruby
280
+ * - *:float_format* [_String_] the C printf format string for printing floats.
281
+ * Default follows the float_precision and will be changed if float_precision is
282
+ * changed. The string can be no more than 6 bytes.
283
+ * - *:use_to_json* [_Boolean_|_nil_] call to_json() methods on dump, default is false
284
+ * - *:use_as_json* [_Boolean_|_nil_] call as_json() methods on dump, default is false
285
+ * - *:use_raw_json* [_Boolean_|_nil_] call raw_json() methods on dump, default is false
286
+ * - *:nilnil* [_Boolean_|_nil_] if true a nil input to load will return nil and
287
+ * not raise an Exception
288
+ * - *:empty_string* [_Boolean_|_nil_] if true an empty input will not raise an Exception
289
+ * - *:allow_gc* [_Boolean_|_nil_] allow or prohibit GC during parsing, default is true (allow)
290
+ * - *:quirks_mode* [_true,_|_false_|_nil_] Allow single JSON values instead of
291
+ * documents, default is true (allow)
292
+ * - *:allow_invalid_unicode* [_true,_|_false_|_nil_] Allow invalid unicode,
293
+ * default is false (don't allow)
294
+ * - *:allow_nan* [_true,_|_false_|_nil_] Allow Nan, Infinity, and -Infinity to
295
+ * be parsed, default is true (allow)
296
+ * - *:indent_str* [_String_|_nil_] String to use for indentation, overriding the
297
+ * indent option is not nil
298
+ * - *:space* [_String_|_nil_] String to use for the space after the colon in JSON
299
+ * object fields
300
+ * - *:space_before* [_String_|_nil_] String to use before the colon separator in
301
+ * JSON object fields
302
+ * - *:object_nl* [_String_|_nil_] String to use after a JSON object field value
303
+ * - *:array_nl* [_String_|_nil_] String to use after a JSON array value
304
+ * - *:nan* [_:null_|_:huge_|_:word_|_:raise_|_:auto_] how to dump Infinity and
305
+ * NaN. :null places a null, :huge places a huge number, :word places Infinity
306
+ * or NaN, :raise raises and exception, :auto uses default for each mode.
307
+ * - *:hash_class* [_Class_|_nil_] Class to use instead of Hash on load,
308
+ * :object_class can also be used
309
+ * - *:array_class* [_Class_|_nil_] Class to use instead of Array on load
310
+ * - *:omit_nil* [_true_|_false_] if true Hash and Object attributes with nil
311
+ * values are omitted
312
+ * - *:omit_null_byte* [_true_|_false_] if true null bytes in strings will be
313
+ * omitted when dumping
314
+ * - *:ignore* [_nil_|_Array_] either nil or an Array of classes to ignore when dumping
315
+ * - *:ignore_under* [_Boolean_] if true then attributes that start with _ are
316
+ * ignored when dumping in object or custom mode.
317
+ * - *:cache_keys* [_Boolean_] if true then hash keys are cached if less than 35 bytes.
318
+ * - *:cache_str* [_Fixnum_] maximum string value length to cache (strings less
319
+ * than this are cached)
320
+ * - *:integer_range* [_Range_] Dump integers outside range as strings.
321
+ * - *:trace* [_true,_|_false_] Trace all load and dump calls, default is false
322
+ * (trace is off)
323
+ * - *:safe* [_true,_|_false_] Safe mimic breaks JSON mimic to be safer, default
324
+ * is false (safe is off)
325
+ *
326
+ * Return [_Hash_] all current option settings.
327
+ */
328
+ static VALUE get_def_opts(VALUE self) {
329
+ VALUE opts = rb_hash_new();
330
+
331
+ if (0 == oj_default_options.dump_opts.indent_size) {
332
+ rb_hash_aset(opts, oj_indent_sym, INT2FIX(oj_default_options.indent));
333
+ } else {
334
+ rb_hash_aset(opts, oj_indent_sym, rb_str_new2(oj_default_options.dump_opts.indent_str));
335
+ }
336
+ rb_hash_aset(opts, sec_prec_sym, INT2FIX(oj_default_options.sec_prec));
337
+ rb_hash_aset(opts,
338
+ circular_sym,
339
+ (Yes == oj_default_options.circular) ? Qtrue : ((No == oj_default_options.circular) ? Qfalse : Qnil));
340
+ rb_hash_aset(
341
+ opts,
342
+ class_cache_sym,
343
+ (Yes == oj_default_options.class_cache) ? Qtrue : ((No == oj_default_options.class_cache) ? Qfalse : Qnil));
344
+ rb_hash_aset(
345
+ opts,
346
+ auto_define_sym,
347
+ (Yes == oj_default_options.auto_define) ? Qtrue : ((No == oj_default_options.auto_define) ? Qfalse : Qnil));
348
+ rb_hash_aset(opts,
349
+ symbol_keys_sym,
350
+ (Yes == oj_default_options.sym_key) ? Qtrue : ((No == oj_default_options.sym_key) ? Qfalse : Qnil));
351
+ rb_hash_aset(
352
+ opts,
353
+ bigdecimal_as_decimal_sym,
354
+ (Yes == oj_default_options.bigdec_as_num) ? Qtrue : ((No == oj_default_options.bigdec_as_num) ? Qfalse : Qnil));
355
+ rb_hash_aset(
356
+ opts,
357
+ oj_create_additions_sym,
358
+ (Yes == oj_default_options.create_ok) ? Qtrue : ((No == oj_default_options.create_ok) ? Qfalse : Qnil));
359
+ rb_hash_aset(opts,
360
+ use_to_json_sym,
361
+ (Yes == oj_default_options.to_json) ? Qtrue : ((No == oj_default_options.to_json) ? Qfalse : Qnil));
362
+ rb_hash_aset(opts,
363
+ use_to_hash_sym,
364
+ (Yes == oj_default_options.to_hash) ? Qtrue : ((No == oj_default_options.to_hash) ? Qfalse : Qnil));
365
+ rb_hash_aset(opts,
366
+ use_as_json_sym,
367
+ (Yes == oj_default_options.as_json) ? Qtrue : ((No == oj_default_options.as_json) ? Qfalse : Qnil));
368
+ rb_hash_aset(opts,
369
+ use_raw_json_sym,
370
+ (Yes == oj_default_options.raw_json) ? Qtrue : ((No == oj_default_options.raw_json) ? Qfalse : Qnil));
371
+ rb_hash_aset(opts,
372
+ nilnil_sym,
373
+ (Yes == oj_default_options.nilnil) ? Qtrue : ((No == oj_default_options.nilnil) ? Qfalse : Qnil));
374
+ rb_hash_aset(
375
+ opts,
376
+ empty_string_sym,
377
+ (Yes == oj_default_options.empty_string) ? Qtrue : ((No == oj_default_options.empty_string) ? Qfalse : Qnil));
378
+ rb_hash_aset(opts,
379
+ allow_gc_sym,
380
+ (Yes == oj_default_options.allow_gc) ? Qtrue : ((No == oj_default_options.allow_gc) ? Qfalse : Qnil));
381
+ rb_hash_aset(
382
+ opts,
383
+ oj_quirks_mode_sym,
384
+ (Yes == oj_default_options.quirks_mode) ? Qtrue : ((No == oj_default_options.quirks_mode) ? Qfalse : Qnil));
385
+ rb_hash_aset(
386
+ opts,
387
+ allow_invalid_unicode_sym,
388
+ (Yes == oj_default_options.allow_invalid) ? Qtrue : ((No == oj_default_options.allow_invalid) ? Qfalse : Qnil));
389
+ rb_hash_aset(
390
+ opts,
391
+ oj_allow_nan_sym,
392
+ (Yes == oj_default_options.allow_nan) ? Qtrue : ((No == oj_default_options.allow_nan) ? Qfalse : Qnil));
393
+ rb_hash_aset(opts,
394
+ oj_trace_sym,
395
+ (Yes == oj_default_options.trace) ? Qtrue : ((No == oj_default_options.trace) ? Qfalse : Qnil));
396
+ rb_hash_aset(opts,
397
+ oj_safe_sym,
398
+ (Yes == oj_default_options.safe) ? Qtrue : ((No == oj_default_options.safe) ? Qfalse : Qnil));
399
+ rb_hash_aset(opts, float_prec_sym, INT2FIX(oj_default_options.float_prec));
400
+ rb_hash_aset(opts, float_format_sym, rb_str_new_cstr(oj_default_options.float_fmt));
401
+ rb_hash_aset(opts, cache_str_sym, INT2FIX(oj_default_options.cache_str));
402
+ rb_hash_aset(
403
+ opts,
404
+ ignore_under_sym,
405
+ (Yes == oj_default_options.ignore_under) ? Qtrue : ((No == oj_default_options.ignore_under) ? Qfalse : Qnil));
406
+ rb_hash_aset(
407
+ opts,
408
+ cache_keys_sym,
409
+ (Yes == oj_default_options.cache_keys) ? Qtrue : ((No == oj_default_options.cache_keys) ? Qfalse : Qnil));
410
+
411
+ switch (oj_default_options.mode) {
412
+ case StrictMode: rb_hash_aset(opts, mode_sym, strict_sym); break;
413
+ case CompatMode: rb_hash_aset(opts, mode_sym, compat_sym); break;
414
+ case NullMode: rb_hash_aset(opts, mode_sym, null_sym); break;
415
+ case ObjectMode: rb_hash_aset(opts, mode_sym, object_sym); break;
416
+ case CustomMode: rb_hash_aset(opts, mode_sym, custom_sym); break;
417
+ case RailsMode: rb_hash_aset(opts, mode_sym, rails_sym); break;
418
+ case WabMode: rb_hash_aset(opts, mode_sym, wab_sym); break;
419
+ default: rb_hash_aset(opts, mode_sym, object_sym); break;
420
+ }
421
+
422
+ if (oj_default_options.int_range_max != 0 || oj_default_options.int_range_min != 0) {
423
+ VALUE range = rb_obj_alloc(rb_cRange);
424
+ VALUE min = LONG2FIX(oj_default_options.int_range_min);
425
+ VALUE max = LONG2FIX(oj_default_options.int_range_max);
426
+
427
+ rb_ivar_set(range, oj_begin_id, min);
428
+ rb_ivar_set(range, oj_end_id, max);
429
+ rb_hash_aset(opts, integer_range_sym, range);
430
+ } else {
431
+ rb_hash_aset(opts, integer_range_sym, Qnil);
432
+ }
433
+ switch (oj_default_options.escape_mode) {
434
+ case NLEsc: rb_hash_aset(opts, escape_mode_sym, newline_sym); break;
435
+ case JSONEsc: rb_hash_aset(opts, escape_mode_sym, json_sym); break;
436
+ case SlashEsc: rb_hash_aset(opts, escape_mode_sym, slash_sym); break;
437
+ case XSSEsc: rb_hash_aset(opts, escape_mode_sym, xss_safe_sym); break;
438
+ case ASCIIEsc: rb_hash_aset(opts, escape_mode_sym, ascii_sym); break;
439
+ case JXEsc: rb_hash_aset(opts, escape_mode_sym, unicode_xss_sym); break;
440
+ default: rb_hash_aset(opts, escape_mode_sym, json_sym); break;
441
+ }
442
+ switch (oj_default_options.time_format) {
443
+ case XmlTime: rb_hash_aset(opts, time_format_sym, xmlschema_sym); break;
444
+ case RubyTime: rb_hash_aset(opts, time_format_sym, ruby_sym); break;
445
+ case UnixZTime: rb_hash_aset(opts, time_format_sym, unix_zone_sym); break;
446
+ case UnixTime:
447
+ default: rb_hash_aset(opts, time_format_sym, unix_sym); break;
448
+ }
449
+ switch (oj_default_options.bigdec_load) {
450
+ case BigDec: rb_hash_aset(opts, bigdecimal_load_sym, bigdecimal_sym); break;
451
+ case FloatDec: rb_hash_aset(opts, bigdecimal_load_sym, float_sym); break;
452
+ case FastDec: rb_hash_aset(opts, bigdecimal_load_sym, fast_sym); break;
453
+ case AutoDec:
454
+ default: rb_hash_aset(opts, bigdecimal_load_sym, auto_sym); break;
455
+ }
456
+ rb_hash_aset(opts, compat_bigdecimal_sym, oj_default_options.compat_bigdec ? Qtrue : Qfalse);
457
+ rb_hash_aset(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(
461
+ opts,
462
+ oj_space_sym,
463
+ (0 == oj_default_options.dump_opts.after_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.after_sep));
464
+ rb_hash_aset(
465
+ opts,
466
+ oj_space_before_sym,
467
+ (0 == oj_default_options.dump_opts.before_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.before_sep));
468
+ rb_hash_aset(
469
+ opts,
470
+ oj_object_nl_sym,
471
+ (0 == oj_default_options.dump_opts.hash_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.hash_nl));
472
+ rb_hash_aset(
473
+ opts,
474
+ oj_array_nl_sym,
475
+ (0 == oj_default_options.dump_opts.array_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.array_nl));
476
+
477
+ switch (oj_default_options.dump_opts.nan_dump) {
478
+ case NullNan: rb_hash_aset(opts, nan_sym, null_sym); break;
479
+ case RaiseNan: rb_hash_aset(opts, nan_sym, raise_sym); break;
480
+ case WordNan: rb_hash_aset(opts, nan_sym, word_sym); break;
481
+ case HugeNan: rb_hash_aset(opts, nan_sym, huge_sym); break;
482
+ case AutoNan:
483
+ default: rb_hash_aset(opts, nan_sym, auto_sym); break;
484
+ }
485
+ rb_hash_aset(opts, omit_nil_sym, oj_default_options.dump_opts.omit_nil ? Qtrue : Qfalse);
486
+ rb_hash_aset(opts, omit_null_byte_sym, oj_default_options.dump_opts.omit_null_byte ? Qtrue : Qfalse);
487
+ rb_hash_aset(opts, oj_hash_class_sym, oj_default_options.hash_class);
488
+ rb_hash_aset(opts, oj_array_class_sym, oj_default_options.array_class);
489
+
490
+ if (NULL == oj_default_options.ignore) {
491
+ rb_hash_aset(opts, ignore_sym, Qnil);
492
+ } else {
493
+ VALUE *vp;
494
+ volatile VALUE a = rb_ary_new();
495
+
496
+ for (vp = oj_default_options.ignore; Qnil != *vp; vp++) {
497
+ rb_ary_push(a, *vp);
498
+ }
499
+ rb_hash_aset(opts, ignore_sym, a);
500
+ }
501
+ return opts;
502
+ }
503
+
504
+ /* Document-method: default_options=
505
+ * call-seq: default_options=(opts)
506
+ *
507
+ * Sets the default options for load and dump.
508
+ * - *opts* [_Hash_] options to change
509
+ * - *:indent* [_Fixnum_|_String_|_nil_] number of spaces to indent each element
510
+ * in a JSON document or the String to use for indentation.
511
+ * - :circular [_Boolean_|_nil_] support circular references while dumping.
512
+ * - *:auto_define* [_Boolean_|_nil_] automatically define classes if they do not exist.
513
+ * - *:symbol_keys* [_Boolean_|_nil_] convert hash keys to symbols.
514
+ * - *:class_cache* [_Boolean_|_nil_] cache classes for faster parsing.
515
+ * - *:escape* [_:newline_|_:json_|_:xss_safe_|_:ascii_|_unicode_xss_|_nil_]
516
+ * mode encodes all high-bit characters as escaped sequences if :ascii, :json
517
+ * is standand UTF-8 JSON encoding, :newline is the same as :json but newlines
518
+ * are not escaped, :unicode_xss allows unicode but escapes &, <, and >, and
519
+ * any \u20xx characters along with some others, and :xss_safe escapes &, <,
520
+ * and >, and some others.
521
+ * - *:bigdecimal_as_decimal* [_Boolean_|_nil_] dump BigDecimal as a decimal
522
+ * number or as a String.
523
+ * - *:bigdecimal_load* [_:bigdecimal_|_:float_|_:auto_|_nil_] load decimals as
524
+ * BigDecimal instead of as a Float. :auto pick the most precise for the number of digits.
525
+ * - *:compat_bigdecimal* [_true_|_false_] load decimals as BigDecimal instead
526
+ * of as a Float in compat mode.
527
+ * - *:mode* [_:object_|_:strict_|_:compat_|_:null_|_:custom_|_:rails_|_:wab_] load
528
+ * and dump mode to use for JSON :strict raises an exception when a non-supported
529
+ * Object is encountered. :compat attempts to extract variable values from an
530
+ * Object using to_json() or to_hash() then it walks the Object's variables if
531
+ * neither is found. The :object mode ignores to_hash() and to_json() methods
532
+ * and encodes variables using code internal to the Oj gem. The :null mode
533
+ * ignores non-supported Objects and replaces them with a null. The :custom
534
+ * mode honors all dump options. The :rails more mimics rails and Active behavior.
535
+ * - *:time_format* [_:unix_|_:xmlschema_|_:ruby_] time format when dumping in :compat
536
+ * mode :unix decimal number denoting the number of seconds since 1/1/1970,
537
+ * :unix_zone decimal number denoting the number of seconds since 1/1/1970
538
+ * plus the utc_offset in the exponent, :xmlschema date-time format taken
539
+ * from XML Schema as a String, :ruby Time.to_s formatted String.
540
+ * - *:create_id* [_String_|_nil_] create id for json compatible object encoding
541
+ * - *:create_additions* [_Boolean_|_nil_] if true allow creation of instances using create_id on load.
542
+ * - *:second_precision* [_Fixnum_|_nil_] number of digits after the decimal
543
+ * when dumping the seconds portion of time.
544
+ * - *:float_format* [_String_] the C printf format string for printing floats.
545
+ * Default follows the float_precision and will be changed if float_precision
546
+ * is changed. The string can be no more than 6 bytes.
547
+ * - *:float_precision* [_Fixnum_|_nil_] number of digits of precision when dumping floats, 0 indicates use Ruby.
548
+ * - *:use_to_json* [_Boolean_|_nil_] call to_json() methods on dump, default is false.
549
+ * - *:use_as_json* [_Boolean_|_nil_] call as_json() methods on dump, default is false.
550
+ * - *:use_to_hash* [_Boolean_|_nil_] call to_hash() methods on dump, default is false.
551
+ * - *:use_raw_json* [_Boolean_|_nil_] call raw_json() methods on dump, default is false.
552
+ * - *:nilnil* [_Boolean_|_nil_] if true a nil input to load will return nil and not raise an Exception.
553
+ * - *:allow_gc* [_Boolean_|_nil_] allow or prohibit GC during parsing, default is true (allow).
554
+ * - *:quirks_mode* [_Boolean_|_nil_] allow single JSON values instead of documents, default is true (allow).
555
+ * - *:allow_invalid_unicode* [_Boolean_|_nil_] allow invalid unicode, default is false (don't allow).
556
+ * - *:allow_nan* [_Boolean_|_nil_] allow Nan, Infinity, and -Infinity, default is true (allow).
557
+ * - *:space* [_String_|_nil_] String to use for the space after the colon in JSON object fields.
558
+ * - *:space_before* [_String_|_nil_] String to use before the colon separator in JSON object fields.
559
+ * - *:object_nl* [_String_|_nil_] String to use after a JSON object field value.
560
+ * - *:array_nl* [_String_|_nil_] String to use after a JSON array value
561
+ * - *:nan* [_:null_|_:huge_|_:word_|_:raise_] how to dump Infinity and NaN in null,
562
+ * strict, and compat mode. :null places a null, :huge places a huge number, :word
563
+ * places Infinity or NaN, :raise raises and exception, :auto uses default for each mode.
564
+ * - *:hash_class* [_Class_|_nil_] Class to use instead of Hash on load, :object_class can also be used.
565
+ * - *:array_class* [_Class_|_nil_] Class to use instead of Array on load.
566
+ * - *:omit_nil* [_true_|_false_] if true Hash and Object attributes with nil values are omitted.
567
+ * - *:ignore* [_nil_|Array] either nil or an Array of classes to ignore when dumping
568
+ * - *:ignore_under* [_Boolean_] if true then attributes that start with _ are
569
+ * ignored when dumping in object or custom mode.
570
+ * - *:cache_keys* [_Boolean_] if true then hash keys are cached
571
+ * - *:cache_str* [_Fixnum_] maximum string value length to cache (strings less than this are cached)
572
+ * - *:integer_range* [_Range_] Dump integers outside range as strings.
573
+ * - *:trace* [_Boolean_] turn trace on or off.
574
+ * - *:safe* [_Boolean_] turn safe mimic on or off.
575
+ */
576
+ static VALUE set_def_opts(VALUE self, VALUE opts) {
577
+ Check_Type(opts, T_HASH);
578
+ oj_parse_options(opts, &oj_default_options);
579
+
580
+ return Qnil;
581
+ }
582
+
583
+ bool oj_hash_has_key(VALUE hash, VALUE key) {
584
+ if (Qundef == rb_hash_lookup2(hash, key, Qundef)) {
585
+ return false;
586
+ }
587
+ return true;
588
+ }
589
+
590
+ bool set_yesno_options(VALUE key, VALUE value, Options copts) {
591
+ struct _yesNoOpt ynos[] = {{circular_sym, &copts->circular},
592
+ {auto_define_sym, &copts->auto_define},
593
+ {symbol_keys_sym, &copts->sym_key},
594
+ {class_cache_sym, &copts->class_cache},
595
+ {bigdecimal_as_decimal_sym, &copts->bigdec_as_num},
596
+ {use_to_hash_sym, &copts->to_hash},
597
+ {use_to_json_sym, &copts->to_json},
598
+ {use_as_json_sym, &copts->as_json},
599
+ {use_raw_json_sym, &copts->raw_json},
600
+ {nilnil_sym, &copts->nilnil},
601
+ {allow_blank_sym, &copts->nilnil}, // same as nilnil
602
+ {empty_string_sym, &copts->empty_string},
603
+ {allow_gc_sym, &copts->allow_gc},
604
+ {oj_quirks_mode_sym, &copts->quirks_mode},
605
+ {allow_invalid_unicode_sym, &copts->allow_invalid},
606
+ {oj_allow_nan_sym, &copts->allow_nan},
607
+ {oj_trace_sym, &copts->trace},
608
+ {oj_safe_sym, &copts->safe},
609
+ {ignore_under_sym, &copts->ignore_under},
610
+ {oj_create_additions_sym, &copts->create_ok},
611
+ {cache_keys_sym, &copts->cache_keys},
612
+ {Qnil, 0}};
613
+ YesNoOpt o;
614
+
615
+ for (o = ynos; 0 != o->attr; o++) {
616
+ if (key == o->sym) {
617
+ if (Qnil == value) {
618
+ *o->attr = NotSet;
619
+ } else if (Qtrue == value) {
620
+ *o->attr = Yes;
621
+ } else if (Qfalse == value) {
622
+ *o->attr = No;
623
+ } else {
624
+ rb_raise(rb_eArgError, "%s must be true, false, or nil.", rb_id2name(key));
625
+ }
626
+ return true;
627
+ }
628
+ }
629
+ return false;
630
+ }
631
+
632
+ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
633
+ Options copts = (Options)opts;
634
+ size_t len;
635
+
636
+ if (set_yesno_options(k, v, copts)) {
637
+ return ST_CONTINUE;
638
+ }
639
+ if (oj_indent_sym == k) {
640
+ switch (rb_type(v)) {
641
+ case T_NIL:
642
+ copts->dump_opts.indent_size = 0;
643
+ *copts->dump_opts.indent_str = '\0';
644
+ copts->indent = 0;
645
+ break;
646
+ case T_FIXNUM:
647
+ copts->dump_opts.indent_size = 0;
648
+ *copts->dump_opts.indent_str = '\0';
649
+ copts->indent = FIX2INT(v);
650
+ break;
651
+ case T_STRING:
652
+ if (sizeof(copts->dump_opts.indent_str) <= (len = RSTRING_LEN(v))) {
653
+ rb_raise(rb_eArgError,
654
+ "indent string is limited to %lu characters.",
655
+ (unsigned long)sizeof(copts->dump_opts.indent_str));
656
+ }
657
+ strcpy(copts->dump_opts.indent_str, StringValuePtr(v));
658
+ copts->dump_opts.indent_size = (uint8_t)len;
659
+ copts->indent = 0;
660
+ break;
661
+ default: rb_raise(rb_eTypeError, "indent must be a Fixnum, String, or nil."); break;
662
+ }
663
+ } else if (float_prec_sym == k) {
664
+ int n;
665
+
666
+ if (rb_cInteger != rb_obj_class(v)) {
667
+ rb_raise(rb_eArgError, ":float_precision must be a Integer.");
668
+ }
669
+ n = FIX2INT(v);
670
+ if (0 >= n) {
671
+ *copts->float_fmt = '\0';
672
+ copts->float_prec = 0;
673
+ } else {
674
+ if (20 < n) {
675
+ n = 20;
676
+ }
677
+ sprintf(copts->float_fmt, "%%0.%dg", n);
678
+ copts->float_prec = n;
679
+ }
680
+ } else if (cache_str_sym == k || cache_string_sym == k) {
681
+ int n;
682
+
683
+ if (rb_cInteger != rb_obj_class(v)) {
684
+ rb_raise(rb_eArgError, ":cache_str must be a Integer.");
685
+ }
686
+ n = FIX2INT(v);
687
+ if (0 >= n) {
688
+ copts->cache_str = 0;
689
+ } else {
690
+ if (32 < n) {
691
+ n = 32;
692
+ }
693
+ copts->cache_str = (char)n;
694
+ }
695
+ } else if (sec_prec_sym == k) {
696
+ int n;
697
+
698
+ if (rb_cInteger != rb_obj_class(v)) {
699
+ rb_raise(rb_eArgError, ":second_precision must be a Integer.");
700
+ }
701
+ n = NUM2INT(v);
702
+ if (0 > n) {
703
+ n = 0;
704
+ copts->sec_prec_set = false;
705
+ } else if (9 < n) {
706
+ n = 9;
707
+ copts->sec_prec_set = true;
708
+ } else {
709
+ copts->sec_prec_set = true;
710
+ }
711
+ copts->sec_prec = n;
712
+ } else if (mode_sym == k) {
713
+ if (wab_sym == v) {
714
+ copts->mode = WabMode;
715
+ } else if (object_sym == v) {
716
+ copts->mode = ObjectMode;
717
+ } else if (strict_sym == v) {
718
+ copts->mode = StrictMode;
719
+ } else if (compat_sym == v || json_sym == v) {
720
+ copts->mode = CompatMode;
721
+ } else if (null_sym == v) {
722
+ copts->mode = NullMode;
723
+ } else if (custom_sym == v) {
724
+ copts->mode = CustomMode;
725
+ } else if (rails_sym == v) {
726
+ copts->mode = RailsMode;
727
+ } else {
728
+ rb_raise(rb_eArgError, ":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
729
+ }
730
+ } else if (time_format_sym == k) {
731
+ if (unix_sym == v) {
732
+ copts->time_format = UnixTime;
733
+ } else if (unix_zone_sym == v) {
734
+ copts->time_format = UnixZTime;
735
+ } else if (xmlschema_sym == v) {
736
+ copts->time_format = XmlTime;
737
+ } else if (ruby_sym == v) {
738
+ copts->time_format = RubyTime;
739
+ } else {
740
+ rb_raise(rb_eArgError, ":time_format must be :unix, :unix_zone, :xmlschema, or :ruby.");
741
+ }
742
+ } else if (escape_mode_sym == k) {
743
+ if (newline_sym == v) {
744
+ copts->escape_mode = NLEsc;
745
+ } else if (json_sym == v) {
746
+ copts->escape_mode = JSONEsc;
747
+ } else if (slash_sym == v) {
748
+ copts->escape_mode = SlashEsc;
749
+ } else if (xss_safe_sym == v) {
750
+ copts->escape_mode = XSSEsc;
751
+ } else if (ascii_sym == v) {
752
+ copts->escape_mode = ASCIIEsc;
753
+ } else if (unicode_xss_sym == v) {
754
+ copts->escape_mode = JXEsc;
755
+ } else {
756
+ rb_raise(rb_eArgError, ":encoding must be :newline, :json, :xss_safe, :unicode_xss, or :ascii.");
757
+ }
758
+ } else if (bigdecimal_load_sym == k) {
759
+ if (Qnil == v) {
760
+ return ST_CONTINUE;
761
+ }
762
+
763
+ if (bigdecimal_sym == v || Qtrue == v) {
764
+ copts->bigdec_load = BigDec;
765
+ } else if (float_sym == v) {
766
+ copts->bigdec_load = FloatDec;
767
+ } else if (fast_sym == v) {
768
+ copts->bigdec_load = FastDec;
769
+ } else if (auto_sym == v || Qfalse == v) {
770
+ copts->bigdec_load = AutoDec;
771
+ } else {
772
+ rb_raise(rb_eArgError, ":bigdecimal_load must be :bigdecimal, :float, or :auto.");
773
+ }
774
+ } else if (compat_bigdecimal_sym == k) {
775
+ if (Qnil == v) {
776
+ return ST_CONTINUE;
777
+ }
778
+ copts->compat_bigdec = (Qtrue == v);
779
+ } else if (oj_decimal_class_sym == k) {
780
+ if (rb_cFloat == v) {
781
+ copts->compat_bigdec = false;
782
+ } else if (oj_bigdecimal_class == v) {
783
+ copts->compat_bigdec = true;
784
+ } else {
785
+ rb_raise(rb_eArgError, ":decimal_class must be BigDecimal or Float.");
786
+ }
787
+ } else if (create_id_sym == k) {
788
+ if (Qnil == v) {
789
+ if (oj_json_class != oj_default_options.create_id && NULL != copts->create_id) {
790
+ OJ_R_FREE((char *)oj_default_options.create_id);
791
+ }
792
+ copts->create_id = NULL;
793
+ copts->create_id_len = 0;
794
+ } else if (T_STRING == rb_type(v)) {
795
+ const char *str = StringValuePtr(v);
796
+
797
+ len = RSTRING_LEN(v);
798
+ if (len != copts->create_id_len || 0 != strcmp(copts->create_id, str)) {
799
+ copts->create_id = OJ_R_ALLOC_N(char, len + 1);
800
+ strcpy((char *)copts->create_id, str);
801
+ copts->create_id_len = len;
802
+ }
803
+ } else {
804
+ rb_raise(rb_eArgError, ":create_id must be string.");
805
+ }
806
+ } else if (oj_space_sym == k) {
807
+ if (Qnil == v) {
808
+ copts->dump_opts.after_size = 0;
809
+ *copts->dump_opts.after_sep = '\0';
810
+ } else {
811
+ rb_check_type(v, T_STRING);
812
+ if (sizeof(copts->dump_opts.after_sep) <= (len = RSTRING_LEN(v))) {
813
+ rb_raise(rb_eArgError,
814
+ "space string is limited to %lu characters.",
815
+ (unsigned long)sizeof(copts->dump_opts.after_sep));
816
+ }
817
+ strcpy(copts->dump_opts.after_sep, StringValuePtr(v));
818
+ copts->dump_opts.after_size = (uint8_t)len;
819
+ }
820
+ } else if (oj_space_before_sym == k) {
821
+ if (Qnil == v) {
822
+ copts->dump_opts.before_size = 0;
823
+ *copts->dump_opts.before_sep = '\0';
824
+ } else {
825
+ rb_check_type(v, T_STRING);
826
+ if (sizeof(copts->dump_opts.before_sep) <= (len = RSTRING_LEN(v))) {
827
+ rb_raise(rb_eArgError,
828
+ "sapce_before string is limited to %lu characters.",
829
+ (unsigned long)sizeof(copts->dump_opts.before_sep));
830
+ }
831
+ strcpy(copts->dump_opts.before_sep, StringValuePtr(v));
832
+ copts->dump_opts.before_size = (uint8_t)len;
833
+ }
834
+ } else if (oj_object_nl_sym == k) {
835
+ if (Qnil == v) {
836
+ copts->dump_opts.hash_size = 0;
837
+ *copts->dump_opts.hash_nl = '\0';
838
+ } else {
839
+ rb_check_type(v, T_STRING);
840
+ if (sizeof(copts->dump_opts.hash_nl) <= (len = RSTRING_LEN(v))) {
841
+ rb_raise(rb_eArgError,
842
+ "object_nl string is limited to %lu characters.",
843
+ (unsigned long)sizeof(copts->dump_opts.hash_nl));
844
+ }
845
+ strcpy(copts->dump_opts.hash_nl, StringValuePtr(v));
846
+ copts->dump_opts.hash_size = (uint8_t)len;
847
+ }
848
+ } else if (oj_array_nl_sym == k) {
849
+ if (Qnil == v) {
850
+ copts->dump_opts.array_size = 0;
851
+ *copts->dump_opts.array_nl = '\0';
852
+ } else {
853
+ rb_check_type(v, T_STRING);
854
+ if (sizeof(copts->dump_opts.array_nl) <= (len = RSTRING_LEN(v))) {
855
+ rb_raise(rb_eArgError,
856
+ "array_nl string is limited to %lu characters.",
857
+ (unsigned long)sizeof(copts->dump_opts.array_nl));
858
+ }
859
+ strcpy(copts->dump_opts.array_nl, StringValuePtr(v));
860
+ copts->dump_opts.array_size = (uint8_t)len;
861
+ }
862
+ } else if (nan_sym == k) {
863
+ if (Qnil == v) {
864
+ return ST_CONTINUE;
865
+ }
866
+ if (null_sym == v) {
867
+ copts->dump_opts.nan_dump = NullNan;
868
+ } else if (huge_sym == v) {
869
+ copts->dump_opts.nan_dump = HugeNan;
870
+ } else if (word_sym == v) {
871
+ copts->dump_opts.nan_dump = WordNan;
872
+ } else if (raise_sym == v) {
873
+ copts->dump_opts.nan_dump = RaiseNan;
874
+ } else if (auto_sym == v) {
875
+ copts->dump_opts.nan_dump = AutoNan;
876
+ } else {
877
+ rb_raise(rb_eArgError, ":nan must be :null, :huge, :word, :raise, or :auto.");
878
+ }
879
+ } else if (omit_nil_sym == k) {
880
+ if (Qnil == v) {
881
+ return ST_CONTINUE;
882
+ }
883
+ if (Qtrue == v) {
884
+ copts->dump_opts.omit_nil = true;
885
+ } else if (Qfalse == v) {
886
+ copts->dump_opts.omit_nil = false;
887
+ } else {
888
+ rb_raise(rb_eArgError, ":omit_nil must be true or false.");
889
+ }
890
+ } else if (omit_null_byte_sym == k) {
891
+ if (Qnil == v) {
892
+ return ST_CONTINUE;
893
+ }
894
+ if (Qtrue == v) {
895
+ copts->dump_opts.omit_null_byte = true;
896
+ } else if (Qfalse == v) {
897
+ copts->dump_opts.omit_null_byte = false;
898
+ } else {
899
+ rb_raise(rb_eArgError, ":omit_null_byte must be true or false.");
900
+ }
901
+ } else if (oj_ascii_only_sym == k) {
902
+ // This is here only for backwards compatibility with the original Oj.
903
+ if (Qtrue == v) {
904
+ copts->escape_mode = ASCIIEsc;
905
+ } else if (Qfalse == v) {
906
+ copts->escape_mode = JSONEsc;
907
+ }
908
+ } else if (oj_hash_class_sym == k) {
909
+ if (Qnil == v) {
910
+ copts->hash_class = Qnil;
911
+ } else {
912
+ rb_check_type(v, T_CLASS);
913
+ copts->hash_class = v;
914
+ }
915
+ } else if (oj_object_class_sym == k) {
916
+ if (Qnil == v) {
917
+ copts->hash_class = Qnil;
918
+ } else {
919
+ rb_check_type(v, T_CLASS);
920
+ copts->hash_class = v;
921
+ }
922
+ } else if (oj_array_class_sym == k) {
923
+ if (Qnil == v) {
924
+ copts->array_class = Qnil;
925
+ } else {
926
+ rb_check_type(v, T_CLASS);
927
+ copts->array_class = v;
928
+ }
929
+ } else if (ignore_sym == k) {
930
+ OJ_R_FREE(copts->ignore);
931
+ copts->ignore = NULL;
932
+ if (Qnil != v) {
933
+ size_t cnt;
934
+
935
+ rb_check_type(v, T_ARRAY);
936
+ cnt = RARRAY_LEN(v);
937
+ if (0 < cnt) {
938
+ size_t i;
939
+
940
+ copts->ignore = OJ_R_ALLOC_N(VALUE, cnt + 1);
941
+ for (i = 0; i < cnt; i++) {
942
+ copts->ignore[i] = RARRAY_AREF(v, i);
943
+ }
944
+ copts->ignore[i] = Qnil;
945
+ }
946
+ }
947
+ } else if (integer_range_sym == k) {
948
+ if (Qnil == v) {
949
+ return ST_CONTINUE;
950
+ }
951
+ if (rb_obj_class(v) == rb_cRange) {
952
+ VALUE min = rb_funcall(v, oj_begin_id, 0);
953
+ VALUE max = rb_funcall(v, oj_end_id, 0);
954
+
955
+ if (TYPE(min) != T_FIXNUM || TYPE(max) != T_FIXNUM) {
956
+ rb_raise(rb_eArgError, ":integer_range range bounds is not Fixnum.");
957
+ }
958
+
959
+ copts->int_range_min = FIX2LONG(min);
960
+ copts->int_range_max = FIX2LONG(max);
961
+ } else if (Qfalse != v) {
962
+ rb_raise(rb_eArgError, ":integer_range must be a range of Fixnum.");
963
+ }
964
+ } else if (symbol_keys_sym == k || oj_symbolize_names_sym == k) {
965
+ if (Qnil == v) {
966
+ return ST_CONTINUE;
967
+ }
968
+ copts->sym_key = (Qtrue == v) ? Yes : No;
969
+
970
+ } else if (oj_max_nesting_sym == k) {
971
+ if (Qtrue == v) {
972
+ copts->dump_opts.max_depth = 100;
973
+ } else if (Qfalse == v || Qnil == v) {
974
+ copts->dump_opts.max_depth = MAX_DEPTH;
975
+ } else if (T_FIXNUM == rb_type(v)) {
976
+ copts->dump_opts.max_depth = NUM2INT(v);
977
+ if (0 >= copts->dump_opts.max_depth) {
978
+ copts->dump_opts.max_depth = MAX_DEPTH;
979
+ }
980
+ }
981
+ } else if (float_format_sym == k) {
982
+ rb_check_type(v, T_STRING);
983
+ if (6 < RSTRING_LEN(v)) {
984
+ rb_raise(rb_eArgError, ":float_format must be 6 bytes or less.");
985
+ }
986
+ strncpy(copts->float_fmt, RSTRING_PTR(v), (size_t)RSTRING_LEN(v));
987
+ copts->float_fmt[RSTRING_LEN(v)] = '\0';
988
+ }
989
+ return ST_CONTINUE;
990
+ }
991
+
992
+ void oj_parse_options(VALUE ropts, Options copts) {
993
+ if (T_HASH != rb_type(ropts)) {
994
+ return;
995
+ }
996
+ rb_hash_foreach(ropts, parse_options_cb, (VALUE)copts);
997
+ oj_parse_opt_match_string(&copts->str_rx, ropts);
998
+
999
+ copts->dump_opts.use = (0 < copts->dump_opts.indent_size || 0 < copts->dump_opts.after_size ||
1000
+ 0 < copts->dump_opts.before_size || 0 < copts->dump_opts.hash_size ||
1001
+ 0 < copts->dump_opts.array_size);
1002
+ return;
1003
+ }
1004
+
1005
+ static int match_string_cb(VALUE key, VALUE value, VALUE rx) {
1006
+ RxClass rc = (RxClass)rx;
1007
+
1008
+ if (T_CLASS != rb_type(value)) {
1009
+ rb_raise(rb_eArgError, "for :match_string, the hash values must be a Class.");
1010
+ }
1011
+ switch (rb_type(key)) {
1012
+ case T_REGEXP: oj_rxclass_rappend(rc, key, value); break;
1013
+ case T_STRING:
1014
+ if (0 != oj_rxclass_append(rc, StringValuePtr(key), value)) {
1015
+ rb_raise(rb_eArgError, "%s", rc->err);
1016
+ }
1017
+ break;
1018
+ default: rb_raise(rb_eArgError, "for :match_string, keys must either a String or RegExp."); break;
1019
+ }
1020
+ return ST_CONTINUE;
1021
+ }
1022
+
1023
+ void oj_parse_opt_match_string(RxClass rc, VALUE ropts) {
1024
+ VALUE v;
1025
+
1026
+ if (Qnil != (v = rb_hash_lookup(ropts, match_string_sym))) {
1027
+ rb_check_type(v, T_HASH);
1028
+ // Zero out rc. Pattern are not appended but override.
1029
+ rc->head = NULL;
1030
+ rc->tail = NULL;
1031
+ *rc->err = '\0';
1032
+ rb_hash_foreach(v, match_string_cb, (VALUE)rc);
1033
+ }
1034
+ }
1035
+
1036
+ /* Document-method: load
1037
+ * call-seq: load(json, options={}) { _|_obj, start, len_|_ }
1038
+ *
1039
+ * Parses a JSON document String into a Object, Hash, Array, String, Fixnum,
1040
+ * Float, true, false, or nil according to the default mode or the mode
1041
+ * specified. Raises an exception if the JSON is malformed or the classes
1042
+ * specified are not valid. If the string input is not a valid JSON document (an
1043
+ * empty string is not a valid JSON document) an exception is raised.
1044
+ *
1045
+ * When used with a document that has multiple JSON elements the block, if
1046
+ * any, will be yielded to. If no block then the last element read will be
1047
+ * returned.
1048
+ *
1049
+ * This parser operates on string and will attempt to load files into memory if
1050
+ * a file object is passed as the first argument. A stream input will be parsed
1051
+ * using a stream parser but others use the slightly faster string parser.
1052
+ *
1053
+ * A block can be provided with a single argument. That argument will be the
1054
+ * parsed JSON document. This is useful when parsing a string that includes
1055
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1056
+ * object, the position in the string or stream of the start of the JSON for
1057
+ * that object, and the length of the JSON for that object plus trailing
1058
+ * whitespace.
1059
+ *
1060
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read()
1061
+ * - *options* [_Hash_] load options (same as default_options)
1062
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1063
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1064
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1065
+ *
1066
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1067
+ */
1068
+ static VALUE load(int argc, VALUE *argv, VALUE self) {
1069
+ Mode mode = oj_default_options.mode;
1070
+
1071
+ if (1 > argc) {
1072
+ rb_raise(rb_eArgError, "Wrong number of arguments to load().");
1073
+ }
1074
+ if (2 <= argc) {
1075
+ VALUE ropts = argv[1];
1076
+ VALUE v;
1077
+
1078
+ if (Qnil != ropts || CompatMode != mode) {
1079
+ Check_Type(ropts, T_HASH);
1080
+ if (Qnil != (v = rb_hash_lookup(ropts, mode_sym))) {
1081
+ if (object_sym == v) {
1082
+ mode = ObjectMode;
1083
+ } else if (strict_sym == v) {
1084
+ mode = StrictMode;
1085
+ } else if (compat_sym == v || json_sym == v) {
1086
+ mode = CompatMode;
1087
+ } else if (null_sym == v) {
1088
+ mode = NullMode;
1089
+ } else if (custom_sym == v) {
1090
+ mode = CustomMode;
1091
+ } else if (rails_sym == v) {
1092
+ mode = RailsMode;
1093
+ } else if (wab_sym == v) {
1094
+ mode = WabMode;
1095
+ } else {
1096
+ rb_raise(rb_eArgError,
1097
+ ":mode must be :object, :strict, :compat, :null, :custom, :rails, or "
1098
+ ":wab.");
1099
+ }
1100
+ }
1101
+ }
1102
+ }
1103
+ switch (mode) {
1104
+ case StrictMode:
1105
+ case NullMode: return oj_strict_parse(argc, argv, self);
1106
+ case CompatMode:
1107
+ case RailsMode: return oj_compat_parse(argc, argv, self);
1108
+ case CustomMode: return oj_custom_parse(argc, argv, self);
1109
+ case WabMode: return oj_wab_parse(argc, argv, self);
1110
+ case ObjectMode:
1111
+ default: break;
1112
+ }
1113
+ return oj_object_parse(argc, argv, self);
1114
+ }
1115
+
1116
+ /* Document-method: load_file
1117
+ * call-seq: load_file(path, options={}) { _|_obj, start, len_|_ }
1118
+ *
1119
+ * Parses a JSON document String into a Object, Hash, Array, String, Fixnum,
1120
+ * Float, true, false, or nil according to the default mode or the mode
1121
+ * specified. Raises an exception if the JSON is malformed or the classes
1122
+ * specified are not valid. If the string input is not a valid JSON document (an
1123
+ * empty string is not a valid JSON document) an exception is raised.
1124
+ *
1125
+ * When used with a document that has multiple JSON elements the block, if
1126
+ * any, will be yielded to. If no block then the last element read will be
1127
+ * returned.
1128
+ *
1129
+ * If the input file is not a valid JSON document (an empty file is not a valid
1130
+ * JSON document) an exception is raised.
1131
+ *
1132
+ * This is a stream based parser which allows a large or huge file to be loaded
1133
+ * without pulling the whole file into memory.
1134
+ *
1135
+ * A block can be provided with a single argument. That argument will be the
1136
+ * parsed JSON document. This is useful when parsing a string that includes
1137
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1138
+ * object, the position in the string or stream of the start of the JSON for
1139
+ * that object, and the length of the JSON for that object plus trailing
1140
+ * whitespace.
1141
+ *
1142
+ * - *path* [_String_] to a file containing a JSON document
1143
+ * - *options* [_Hash_] load options (same as default_options)
1144
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1145
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1146
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1147
+ *
1148
+ * Returns [_Object_|_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1149
+ */
1150
+ static VALUE load_file(int argc, VALUE *argv, VALUE self) {
1151
+ char *path;
1152
+ int fd;
1153
+ Mode mode = oj_default_options.mode;
1154
+ struct _parseInfo pi;
1155
+
1156
+ if (1 > argc) {
1157
+ rb_raise(rb_eArgError, "Wrong number of arguments to load().");
1158
+ }
1159
+ path = StringValuePtr(*argv);
1160
+ parse_info_init(&pi);
1161
+ pi.options = oj_default_options;
1162
+ pi.handler = Qnil;
1163
+ pi.err_class = Qnil;
1164
+ pi.max_depth = 0;
1165
+ if (2 <= argc) {
1166
+ VALUE ropts = argv[1];
1167
+ VALUE v;
1168
+
1169
+ Check_Type(ropts, T_HASH);
1170
+ if (Qnil != (v = rb_hash_lookup(ropts, mode_sym))) {
1171
+ if (object_sym == v) {
1172
+ mode = ObjectMode;
1173
+ } else if (strict_sym == v) {
1174
+ mode = StrictMode;
1175
+ } else if (compat_sym == v || json_sym == v) {
1176
+ mode = CompatMode;
1177
+ } else if (null_sym == v) {
1178
+ mode = NullMode;
1179
+ } else if (custom_sym == v) {
1180
+ mode = CustomMode;
1181
+ } else if (rails_sym == v) {
1182
+ mode = RailsMode;
1183
+ } else if (wab_sym == v) {
1184
+ mode = WabMode;
1185
+ } else {
1186
+ rb_raise(rb_eArgError, ":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
1187
+ }
1188
+ }
1189
+ }
1190
+ #ifdef _WIN32
1191
+ {
1192
+ WCHAR *wide_path;
1193
+ wide_path = rb_w32_mbstr_to_wstr(CP_UTF8, path, -1, NULL);
1194
+ fd = rb_w32_wopen(wide_path, O_RDONLY);
1195
+ OJ_FREE(wide_path);
1196
+ }
1197
+ #else
1198
+ fd = open(path, O_RDONLY);
1199
+ #endif
1200
+ if (0 == fd) {
1201
+ rb_raise(rb_eIOError, "%s", strerror(errno));
1202
+ }
1203
+ switch (mode) {
1204
+ case StrictMode:
1205
+ case NullMode: oj_set_strict_callbacks(&pi); return oj_pi_sparse(argc, argv, &pi, fd);
1206
+ case CustomMode: oj_set_custom_callbacks(&pi); return oj_pi_sparse(argc, argv, &pi, fd);
1207
+ case CompatMode:
1208
+ case RailsMode: oj_set_compat_callbacks(&pi); return oj_pi_sparse(argc, argv, &pi, fd);
1209
+ case WabMode: oj_set_wab_callbacks(&pi); return oj_pi_sparse(argc, argv, &pi, fd);
1210
+ case ObjectMode:
1211
+ default: break;
1212
+ }
1213
+ oj_set_object_callbacks(&pi);
1214
+
1215
+ return oj_pi_sparse(argc, argv, &pi, fd);
1216
+ }
1217
+
1218
+ /* Document-method: safe_load
1219
+ * call-seq: safe_load(doc)
1220
+ *
1221
+ * Loads a JSON document in strict mode with :auto_define and :symbol_keys
1222
+ * turned off. This function should be safe to use with JSON received on an
1223
+ * unprotected public interface.
1224
+ *
1225
+ * - *doc* [_String__|_IO_] JSON String or IO to load.
1226
+ *
1227
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Bignum_|_BigDecimal_|_nil_|_True_|_False_]
1228
+ */
1229
+ static VALUE safe_load(VALUE self, VALUE doc) {
1230
+ struct _parseInfo pi;
1231
+ VALUE args[1];
1232
+
1233
+ parse_info_init(&pi);
1234
+ pi.err_class = Qnil;
1235
+ pi.max_depth = 0;
1236
+ pi.options = oj_default_options;
1237
+ pi.options.auto_define = No;
1238
+ pi.options.sym_key = No;
1239
+ pi.options.mode = StrictMode;
1240
+ oj_set_strict_callbacks(&pi);
1241
+ *args = doc;
1242
+
1243
+ return oj_pi_parse(1, args, &pi, 0, 0, 1);
1244
+ }
1245
+
1246
+ /* Document-method: saj_parse
1247
+ * call-seq: saj_parse(handler, io)
1248
+ *
1249
+ * Parses an IO stream or file containing a JSON document. Raises an exception
1250
+ * if the JSON is malformed. This is a callback parser that calls the methods in
1251
+ * the handler if they exist. A sample is the Oj::Saj class which can be used as
1252
+ * a base class for the handler.
1253
+ *
1254
+ * - *handler* [_Oj::Saj_] responds to Oj::Saj methods
1255
+ * - *io* [_IO_|_String_] IO Object to read from
1256
+ */
1257
+
1258
+ /* Document-method: sc_parse
1259
+ * call-seq: sc_parse(handler, io)
1260
+ *
1261
+ * Parses an IO stream or file containing a JSON document. Raises an exception
1262
+ * if the JSON is malformed. This is a callback parser (Simple Callback Parser)
1263
+ * that calls the methods in the handler if they exist. A sample is the
1264
+ * Oj::ScHandler class which can be used as a base class for the handler. This
1265
+ * callback parser is slightly more efficient than the Saj callback parser and
1266
+ * requires less argument checking.
1267
+ *
1268
+ * - *handler* [_Oj_::ScHandler_] responds to Oj::ScHandler methods
1269
+ * - *io* [_IO__|_String_] IO Object to read from
1270
+ */
1271
+
1272
+ struct dump_arg {
1273
+ struct _out *out;
1274
+ struct _options *copts;
1275
+ int argc;
1276
+ VALUE *argv;
1277
+ };
1278
+
1279
+ static VALUE dump_body(VALUE a) {
1280
+ volatile struct dump_arg *arg = (void *)a;
1281
+ VALUE rstr;
1282
+
1283
+ oj_dump_obj_to_json_using_params(*arg->argv, arg->copts, arg->out, arg->argc - 1, arg->argv + 1);
1284
+ if (0 == arg->out->buf) {
1285
+ rb_raise(rb_eNoMemError, "Not enough memory.");
1286
+ }
1287
+ rstr = rb_utf8_str_new_cstr(arg->out->buf);
1288
+
1289
+ return rstr;
1290
+ }
1291
+
1292
+ static VALUE dump_ensure(VALUE a) {
1293
+ volatile struct dump_arg *arg = (void *)a;
1294
+
1295
+ oj_out_free(arg->out);
1296
+
1297
+ return Qnil;
1298
+ }
1299
+
1300
+ /* Document-method: dump
1301
+ * call-seq: dump(obj, options={})
1302
+ *
1303
+ * Dumps an Object (obj) to a string.
1304
+ * - *obj* [_Object_] Object to serialize as an JSON document String
1305
+ * - *options* [_Hash_] same as default_options
1306
+ */
1307
+ static VALUE dump(int argc, VALUE *argv, VALUE self) {
1308
+ struct dump_arg arg;
1309
+ struct _out out;
1310
+ struct _options copts = oj_default_options;
1311
+
1312
+ if (1 > argc) {
1313
+ rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
1314
+ }
1315
+ if (CompatMode == copts.mode) {
1316
+ copts.dump_opts.nan_dump = WordNan;
1317
+ }
1318
+ if (2 == argc) {
1319
+ oj_parse_options(argv[1], &copts);
1320
+ }
1321
+ if (CompatMode == copts.mode && copts.escape_mode != ASCIIEsc) {
1322
+ copts.escape_mode = JSONEsc;
1323
+ }
1324
+ arg.out = &out;
1325
+ arg.copts = &copts;
1326
+ arg.argc = argc;
1327
+ arg.argv = argv;
1328
+
1329
+ oj_out_init(arg.out);
1330
+
1331
+ arg.out->omit_nil = copts.dump_opts.omit_nil;
1332
+ arg.out->omit_null_byte = copts.dump_opts.omit_null_byte;
1333
+
1334
+ return rb_ensure(dump_body, (VALUE)&arg, dump_ensure, (VALUE)&arg);
1335
+ }
1336
+
1337
+ /* Document-method: to_json
1338
+ * call-seq: to_json(obj, options)
1339
+ *
1340
+ * Dumps an Object (obj) to a string. If the object has a to_json method that
1341
+ * will be called. The mode is set to :compat.
1342
+ * - *obj* [_Object_] Object to serialize as an JSON document String
1343
+ * - *options* [_Hash_]
1344
+ * - *:max_nesting* [_Fixnum_|_boolean_] It true nesting is limited to 100.
1345
+ * If a Fixnum nesting is set to the provided value. The option to detect
1346
+ * circular references is available but is not compatible with the json gem.,
1347
+ * default is false or unlimited.
1348
+ * - *:allow_nan* [_boolean_] If true non JSON compliant words such as Nan and
1349
+ * Infinity will be used as appropriate, default is true.
1350
+ * - *:quirks_mode* [_boolean_] Allow single JSON values instead of documents, default is true (allow).
1351
+ * - *:indent* [_String_|_nil_] String to use for indentation, overriding the indent option if not nil.
1352
+ * - *:space* [_String_|_nil_] String to use for the space after the colon in JSON object fields.
1353
+ * - *:space_before* [_String_|_nil_] String to use before the colon separator in JSON object fields.
1354
+ * - *:object_nl* [_String_|_nil_] String to use after a JSON object field value.
1355
+ * - *:array_nl* [_String_|_nil_] String to use after a JSON array value.
1356
+ * - *:trace* [_Boolean_] If true trace is turned on.
1357
+ *
1358
+ * Returns [_String_] the encoded JSON.
1359
+ */
1360
+ static VALUE to_json(int argc, VALUE *argv, VALUE self) {
1361
+ struct _out out;
1362
+ struct _options copts = oj_default_options;
1363
+ VALUE rstr;
1364
+
1365
+ if (1 > argc) {
1366
+ rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
1367
+ }
1368
+ copts.escape_mode = JXEsc;
1369
+ copts.dump_opts.nan_dump = RaiseNan;
1370
+ if (2 == argc) {
1371
+ oj_parse_mimic_dump_options(argv[1], &copts);
1372
+ }
1373
+ copts.mode = CompatMode;
1374
+ copts.to_json = Yes;
1375
+
1376
+ oj_out_init(&out);
1377
+
1378
+ out.omit_nil = copts.dump_opts.omit_nil;
1379
+ out.omit_null_byte = copts.dump_opts.omit_null_byte;
1380
+ // For obj.to_json or generate nan is not allowed but if called from dump
1381
+ // it is.
1382
+ oj_dump_obj_to_json_using_params(*argv, &copts, &out, argc - 1, argv + 1);
1383
+
1384
+ if (0 == out.buf) {
1385
+ rb_raise(rb_eNoMemError, "Not enough memory.");
1386
+ }
1387
+ rstr = rb_utf8_str_new_cstr(out.buf);
1388
+
1389
+ oj_out_free(&out);
1390
+
1391
+ return rstr;
1392
+ }
1393
+
1394
+ /* Document-method: to_file
1395
+ * call-seq: to_file(file_path, obj, options={})
1396
+ *
1397
+ * Dumps an Object to the specified file.
1398
+ * - *file* [_String_] _path file path to write the JSON document to
1399
+ * - *obj* [_Object_] Object to serialize as an JSON document String
1400
+ * - *options* [_Hash_] formatting options
1401
+ * - *:indent* [_Fixnum_] format expected
1402
+ * - *:circular* [_Boolean_] allow circular references, default: false
1403
+ */
1404
+ static VALUE to_file(int argc, VALUE *argv, VALUE self) {
1405
+ struct _options copts = oj_default_options;
1406
+
1407
+ if (3 == argc) {
1408
+ oj_parse_options(argv[2], &copts);
1409
+ }
1410
+ oj_write_obj_to_file(argv[1], StringValuePtr(*argv), &copts);
1411
+
1412
+ return Qnil;
1413
+ }
1414
+
1415
+ /* Document-method: to_stream
1416
+ * call-seq: to_stream(io, obj, options={})
1417
+ *
1418
+ * Dumps an Object to the specified IO stream.
1419
+ * - *io* [_IO_] IO stream to write the JSON document to
1420
+ * - *obj* [_Object_] Object to serialize as an JSON document String
1421
+ * - *options* [_Hash_] formatting options
1422
+ * - *:indent* [_Fixnum_] format expected
1423
+ * - *:circular* [_Boolean_] allow circular references, default: false
1424
+ */
1425
+ static VALUE to_stream(int argc, VALUE *argv, VALUE self) {
1426
+ struct _options copts = oj_default_options;
1427
+
1428
+ if (3 == argc) {
1429
+ oj_parse_options(argv[2], &copts);
1430
+ }
1431
+ oj_write_obj_to_stream(argv[1], *argv, &copts);
1432
+
1433
+ return Qnil;
1434
+ }
1435
+
1436
+ /* Document-method: register_odd
1437
+ * call-seq: register_odd(clas, create_object, create_method, *members)
1438
+ *
1439
+ * Registers a class as special. This is useful for working around subclasses of
1440
+ * primitive types as is done with ActiveSupport classes. The use of this
1441
+ * function should be limited to just classes that can not be handled in the
1442
+ * normal way. It is not intended as a hook for changing the output of all
1443
+ * classes as it is not optimized for large numbers of classes.
1444
+ *
1445
+ * - *clas* [_Class__|_Module_] Class or Module to be made special
1446
+ * - *create_object* [_Object_] object to call the create method on
1447
+ * - *create_method* [_Symbol_] method on the clas that will create a new instance
1448
+ * of the clas when given all the member values in the order specified.
1449
+ * - *members* [_Symbol__|_String_] methods used to get the member values from
1450
+ * instances of the clas.
1451
+ */
1452
+ static VALUE register_odd(int argc, VALUE *argv, VALUE self) {
1453
+ if (3 > argc) {
1454
+ rb_raise(rb_eArgError, "incorrect number of arguments.");
1455
+ }
1456
+ switch (rb_type(*argv)) {
1457
+ case T_CLASS:
1458
+ case T_MODULE: break;
1459
+ default: rb_raise(rb_eTypeError, "expected a class or module."); break;
1460
+ }
1461
+ Check_Type(argv[2], T_SYMBOL);
1462
+ if (MAX_ODD_ARGS < argc - 2) {
1463
+ rb_raise(rb_eArgError, "too many members.");
1464
+ }
1465
+ oj_reg_odd(argv[0], argv[1], argv[2], argc - 3, argv + 3, false);
1466
+
1467
+ return Qnil;
1468
+ }
1469
+
1470
+ /* Document-method: register_odd_raw
1471
+ * call-seq: register_odd_raw(clas, create_object, create_method, dump_method)
1472
+ *
1473
+ * Registers a class as special and expect the output to be a string that can be
1474
+ * included in the dumped JSON directly. This is useful for working around
1475
+ * subclasses of primitive types as is done with ActiveSupport classes. The use
1476
+ * of this function should be limited to just classes that can not be handled in
1477
+ * the normal way. It is not intended as a hook for changing the output of all
1478
+ * classes as it is not optimized for large numbers of classes. Be careful with
1479
+ * this option as the JSON may be incorrect if invalid JSON is returned.
1480
+ *
1481
+ * - *clas* [_Class_|_Module_] Class or Module to be made special
1482
+ * - *create_object* [_Object_] object to call the create method on
1483
+ * - *create_method* [_Symbol_] method on the clas that will create a new instance
1484
+ * of the clas when given all the member values in the order specified.
1485
+ * - *dump_method* [_Symbol_|_String_] method to call on the object being
1486
+ * serialized to generate the raw JSON.
1487
+ */
1488
+ static VALUE register_odd_raw(int argc, VALUE *argv, VALUE self) {
1489
+ if (3 > argc) {
1490
+ rb_raise(rb_eArgError, "incorrect number of arguments.");
1491
+ }
1492
+ switch (rb_type(*argv)) {
1493
+ case T_CLASS:
1494
+ case T_MODULE: break;
1495
+ default: rb_raise(rb_eTypeError, "expected a class or module."); break;
1496
+ }
1497
+ Check_Type(argv[2], T_SYMBOL);
1498
+ if (MAX_ODD_ARGS < argc - 2) {
1499
+ rb_raise(rb_eArgError, "too many members.");
1500
+ }
1501
+ oj_reg_odd(argv[0], argv[1], argv[2], 1, argv + 3, true);
1502
+
1503
+ return Qnil;
1504
+ }
1505
+
1506
+ ////////////////////////////////////////////////////////////////////////////////
1507
+ // RDoc entries must be in the same file as the rb_define_method and must be
1508
+ // directly above the C method function. The extern declaration is enough to
1509
+ // get it to work.
1510
+ ////////////////////////////////////////////////////////////////////////////////
1511
+
1512
+ /* Document-method: strict_load
1513
+ * call-seq: strict_load(json, options) { _|_obj, start, len_|_ }
1514
+ *
1515
+ * Parses a JSON document String into an Hash, Array, String, Fixnum, Float,
1516
+ * true, false, or nil. It parses using a mode that is strict in that it maps
1517
+ * each primitive JSON type to a similar Ruby type. The :create_id is not
1518
+ * honored in this mode. Note that a Ruby Hash is used to represent the JSON
1519
+ * Object type. These two are not the same since the JSON Object type can have
1520
+ * repeating entries with the same key and Ruby Hash can not.
1521
+ *
1522
+ * When used with a document that has multiple JSON elements the block, if
1523
+ * any, will be yielded to. If no block then the last element read will be
1524
+ * returned.
1525
+ *
1526
+ * Raises an exception if the JSON is malformed or the classes specified are not
1527
+ * valid. If the input is not a valid JSON document (an empty string is not a
1528
+ * valid JSON document) an exception is raised.
1529
+ *
1530
+ * A block can be provided with a single argument. That argument will be the
1531
+ * parsed JSON document. This is useful when parsing a string that includes
1532
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1533
+ * object, the position in the string or stream of the start of the JSON for
1534
+ * that object, and the length of the JSON for that object plus trailing
1535
+ * whitespace.
1536
+ *
1537
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read().
1538
+ * - *options* [_Hash_] load options (same as default_options).
1539
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1540
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1541
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1542
+ *
1543
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1544
+ */
1545
+ extern VALUE oj_strict_parse(int argc, VALUE *argv, VALUE self);
1546
+
1547
+ /* Document-method: compat_load
1548
+ * call-seq: compat_load(json, options) { _|_obj, start, len_|_ }
1549
+ *
1550
+ * Parses a JSON document String into an Object, Hash, Array, String, Fixnum,
1551
+ * Float, true, false, or nil. It parses using a mode that is generally
1552
+ * compatible with other Ruby JSON parsers in that it will create objects based
1553
+ * on the :create_id value. It is not compatible in every way to every other
1554
+ * parser though as each parser has it's own variations.
1555
+ *
1556
+ * When used with a document that has multiple JSON elements the block, if
1557
+ * any, will be yielded to. If no block then the last element read will be
1558
+ * returned.
1559
+ *
1560
+ * Raises an exception if the JSON is malformed or the classes specified are not
1561
+ * valid. If the input is not a valid JSON document (an empty string is not a
1562
+ * valid JSON document) an exception is raised.
1563
+ *
1564
+ * A block can be provided with a single argument. That argument will be the
1565
+ * parsed JSON document. This is useful when parsing a string that includes
1566
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1567
+ * object, the position in the string or stream of the start of the JSON for
1568
+ * that object, and the length of the JSON for that object plus trailing
1569
+ * whitespace.
1570
+ *
1571
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read().
1572
+ * - *options* [_Hash_] load options (same as default_options).
1573
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1574
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1575
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1576
+ *
1577
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1578
+ */
1579
+ extern VALUE oj_compat_parse(int argc, VALUE *argv, VALUE self);
1580
+
1581
+ /* Document-method: object_load
1582
+ * call-seq: object_load(json, options) { _|_obj, start, len_|_ }
1583
+ *
1584
+ * Parses a JSON document String into an Object, Hash, Array, String, Fixnum,
1585
+ * Float, true, false, or nil. In the :object mode the JSON should have been
1586
+ * generated by Oj.dump(). The parser will reconstitute the original marshalled
1587
+ * or dumped Object. The :auto_define and :circular options have meaning with
1588
+ * this parsing mode.
1589
+ *
1590
+ * Raises an exception if the JSON is malformed or the classes specified are not
1591
+ * valid. If the input is not a valid JSON document (an empty string is not a
1592
+ * valid JSON document) an exception is raised.
1593
+ *
1594
+ * A block can be provided with a single argument. That argument will be the
1595
+ * parsed JSON document. This is useful when parsing a string that includes
1596
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1597
+ * object, the position in the string or stream of the start of the JSON for
1598
+ * that object, and the length of the JSON for that object plus trailing
1599
+ * whitespace.
1600
+ *
1601
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read().
1602
+ * - *options* [_Hash_] load options (same as default_options).
1603
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1604
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1605
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1606
+ *
1607
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1608
+ */
1609
+ extern VALUE oj_object_parse(int argc, VALUE *argv, VALUE self);
1610
+
1611
+ /* Document-method: wab_load
1612
+ * call-seq: wab_load(json, options) { _|_obj, start, len_|_ }
1613
+ *
1614
+ * Parses a JSON document String into an Hash, Array, String, Fixnum, Float,
1615
+ * true, false, or nil. It parses using a mode that is :wab in that it maps
1616
+ * each primitive JSON type to a similar Ruby type. The :create_id is not
1617
+ * honored in this mode. Note that a Ruby Hash is used to represent the JSON
1618
+ * Object type. These two are not the same since the JSON Object type can have
1619
+ * repeating entries with the same key and Ruby Hash can not.
1620
+ *
1621
+ * When used with a document that has multiple JSON elements the block, if
1622
+ * any, will be yielded to. If no block then the last element read will be
1623
+ * returned.
1624
+ *
1625
+ * Raises an exception if the JSON is malformed or the classes specified are not
1626
+ * valid. If the input is not a valid JSON document (an empty string is not a
1627
+ * valid JSON document) an exception is raised.
1628
+ *
1629
+ * A block can be provided with a single argument. That argument will be the
1630
+ * parsed JSON document. This is useful when parsing a string that includes
1631
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1632
+ * object, the position in the string or stream of the start of the JSON for
1633
+ * that object, and the length of the JSON for that object plus trailing
1634
+ * whitespace.
1635
+ *
1636
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read().
1637
+ * - *options* [_Hash_] load options (same as default_options).
1638
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1639
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1640
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1641
+ *
1642
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1643
+ */
1644
+ extern VALUE oj_wab_parse(int argc, VALUE *argv, VALUE self);
1645
+
1646
+ /* Document-method: add_to_json
1647
+ * call-seq: add_to_json(*args)
1648
+ *
1649
+ * Override simple to_s dump behavior in :compat mode to instead use an
1650
+ * optimized dump that includes the classname and attributes so that the
1651
+ * object can be re-created on load. The format is the same as the json gem
1652
+ * but does not use the ruby methods for encoding.
1653
+ *
1654
+ * The classes supported for optimization are: Array, BigDecimal, Complex,
1655
+ * Date, DateTime, Exception, Hash, Integer, OpenStruct, Range, Rational,
1656
+ * Regexp, Struct, and Time. Providing no classes will result in all those
1657
+ * classes being optimized.q
1658
+ *
1659
+ * - *args( [_Class_] zero or more classes to optimize.
1660
+ */
1661
+ extern VALUE oj_add_to_json(int argc, VALUE *argv, VALUE self);
1662
+
1663
+ /* @!method remove_to_json(*args)
1664
+ *
1665
+ * Reverts back to the to_s dump behavior in :compat mode to instead use an
1666
+ * optimized dump that includes the classname and attributes so that the
1667
+ * object can be re-created on load. The format is the same as the json gem
1668
+ * but does not use the ruby methods for encoding.
1669
+ *
1670
+ * The classes supported for optimization are: Array, BigDecimal, Complex,
1671
+ * Date, DateTime, Exception, Hash, Integer, OpenStruct, Range, Rational,
1672
+ * Regexp, Struct, and Time. Providing no classes will result in all those
1673
+ * classes being reverted from the optimized mode.
1674
+ *
1675
+ * - *args* [_Class_] zero or more classes to optimize.
1676
+ */
1677
+ extern VALUE oj_remove_to_json(int argc, VALUE *argv, VALUE self);
1678
+
1679
+ /* Document-method: mimic_JSON
1680
+ * call-seq: mimic_JSON()
1681
+ *
1682
+ * Creates the JSON module with methods and classes to mimic the JSON gem. After
1683
+ * this method is invoked calls that expect the JSON module will use Oj instead
1684
+ * and be faster than the original JSON. Most options that could be passed to
1685
+ * the JSON methods are supported. The calls to set parser or generator will not
1686
+ * raise an Exception but will not have any effect. The method can also be
1687
+ * called after the json gem is loaded. The necessary methods on the json gem
1688
+ * will be replaced with Oj methods.
1689
+ *
1690
+ * Note that this also sets the default options of :mode to :compat and
1691
+ * :encoding to :unicode_xss.
1692
+ *
1693
+ * Returns [_Module_] the JSON module.
1694
+ */
1695
+ extern VALUE oj_define_mimic_json(int argc, VALUE *argv, VALUE self);
1696
+
1697
+ /* Document-method: generate
1698
+ * call-seq: generate(obj, opts=nil)
1699
+ *
1700
+ * Encode obj as a JSON String. The obj argument must be a Hash, Array, or
1701
+ * respond to to_h or to_json. Options other than those listed such as
1702
+ * +:allow_nan+ or +:max_nesting+ are ignored. Calling this method will call
1703
+ * Oj.mimic_JSON if it is not already called.
1704
+ *
1705
+ * - *obj* [_Object__|_Hash_|_Array_] object to convert to a JSON String
1706
+ * - *opts* [_Hash_] options
1707
+ * - *:indent* [_String_] String to use for indentation.
1708
+ * - *:space* [_String_] String placed after a , or : delimiter
1709
+ * - *:space_before* [_String_] String placed before a : delimiter
1710
+ * - *:object_nl* [_String_] String placed after a JSON object
1711
+ * - *:array_nl* [_String_] String placed after a JSON array
1712
+ * - *:ascii_only* [_Boolean_] if not nil or false then use only ascii characters
1713
+ * in the output. Note JSON.generate does support this even if it is not documented.
1714
+ *
1715
+ * Returns [_String_]generated JSON.
1716
+ */
1717
+ extern VALUE oj_mimic_generate(int argc, VALUE *argv, VALUE self);
1718
+
1719
+ /* Document-module: Oj.optimize_rails()
1720
+ *
1721
+ * Sets the Oj as the Rails encoder and decoder. Oj::Rails.optimize is also
1722
+ * called.
1723
+ */
1724
+ extern VALUE oj_optimize_rails(VALUE self);
1725
+
1726
+ /*
1727
+ extern void oj_hash_test();
1728
+ static VALUE
1729
+ hash_test(VALUE self) {
1730
+ oj_hash_test();
1731
+ return Qnil;
1732
+ }
1733
+ */
1734
+ /*
1735
+ extern void oj_hash_sizes();
1736
+ static VALUE
1737
+ hash_test(VALUE self) {
1738
+ oj_hash_sizes();
1739
+ return Qnil;
1740
+ }
1741
+ */
1742
+
1743
+ static VALUE protect_require(VALUE x) {
1744
+ rb_require("time");
1745
+ rb_require("bigdecimal");
1746
+ return Qnil;
1747
+ }
1748
+
1749
+ extern void print_all_odds(const char *label);
1750
+
1751
+ static VALUE debug_odd(VALUE self, VALUE label) {
1752
+ print_all_odds(RSTRING_PTR(label));
1753
+ return Qnil;
1754
+ }
1755
+
1756
+ static VALUE mem_report(VALUE self) {
1757
+ oj_mem_report();
1758
+ return Qnil;
1759
+ }
1760
+
1761
+ /* Document-module: Oj
1762
+ *
1763
+ * Optimized JSON (Oj), as the name implies was written to provide speed
1764
+ * optimized JSON handling.
1765
+ *
1766
+ * Oj uses modes to control how object are encoded and decoded. In addition
1767
+ * global and options to methods allow additional behavior modifications. The
1768
+ * modes are:
1769
+ *
1770
+ * - *:strict* mode will only allow the 7 basic JSON types to be serialized.
1771
+ * Any other Object will raise an Exception.
1772
+ *
1773
+ * - *:null* mode is similar to the :strict mode except any Object that is not
1774
+ * one of the JSON base types is replaced by a JSON null.
1775
+ *
1776
+ * - *:object* mode will dump any Object as a JSON Object with keys that match
1777
+ * the Ruby Object's variable names without the '@' character. This is the
1778
+ * highest performance mode.
1779
+ *
1780
+ * - *:compat* or *:json* mode is the compatible mode for the json gem. It mimics
1781
+ * the json gem including the options, defaults, and restrictions.
1782
+ *
1783
+ * - *:rails* is the compatibility mode for Rails or Active support.
1784
+ *
1785
+ * - *:custom* is the most configurable mode.
1786
+ *
1787
+ * - *:wab* specifically for WAB data exchange.
1788
+ */
1789
+ void Init_oj_windows(void) {
1790
+ int err = 0;
1791
+
1792
+ #if HAVE_RB_EXT_RACTOR_SAFE
1793
+ rb_ext_ractor_safe(true);
1794
+ #endif
1795
+ Oj = rb_define_module("Oj");
1796
+ rb_gc_register_address(&Oj);
1797
+
1798
+ oj_cstack_class = rb_define_class_under(Oj, "CStack", rb_cObject);
1799
+ rb_gc_register_address(&oj_cstack_class);
1800
+
1801
+ rb_undef_alloc_func(oj_cstack_class);
1802
+
1803
+ oj_string_writer_init();
1804
+ oj_stream_writer_init();
1805
+
1806
+ rb_require("date");
1807
+ // On Rubinius the require fails but can be done from a ruby file.
1808
+ rb_protect(protect_require, Qnil, &err);
1809
+ rb_require("stringio");
1810
+ oj_utf8_encoding_index = rb_enc_find_index("UTF-8");
1811
+ oj_utf8_encoding = rb_enc_from_index(oj_utf8_encoding_index);
1812
+
1813
+ // rb_define_module_function(Oj, "hash_test", hash_test, 0);
1814
+ rb_define_module_function(Oj, "debug_odd", debug_odd, 1);
1815
+
1816
+ rb_define_module_function(Oj, "default_options", get_def_opts, 0);
1817
+ rb_define_module_function(Oj, "default_options=", set_def_opts, 1);
1818
+
1819
+ rb_define_module_function(Oj, "mimic_JSON", oj_define_mimic_json, -1);
1820
+ rb_define_module_function(Oj, "load", load, -1);
1821
+ rb_define_module_function(Oj, "load_file", load_file, -1);
1822
+ rb_define_module_function(Oj, "safe_load", safe_load, 1);
1823
+ rb_define_module_function(Oj, "strict_load", oj_strict_parse, -1);
1824
+ rb_define_module_function(Oj, "compat_load", oj_compat_parse, -1);
1825
+ rb_define_module_function(Oj, "object_load", oj_object_parse, -1);
1826
+ rb_define_module_function(Oj, "wab_load", oj_wab_parse, -1);
1827
+
1828
+ rb_define_module_function(Oj, "dump", dump, -1);
1829
+
1830
+ rb_define_module_function(Oj, "to_file", to_file, -1);
1831
+ rb_define_module_function(Oj, "to_stream", to_stream, -1);
1832
+ // JSON gem compatibility
1833
+ rb_define_module_function(Oj, "to_json", to_json, -1);
1834
+ rb_define_module_function(Oj, "generate", oj_mimic_generate, -1);
1835
+ rb_define_module_function(Oj, "fast_generate", oj_mimic_generate, -1);
1836
+
1837
+ rb_define_module_function(Oj, "add_to_json", oj_add_to_json, -1);
1838
+ rb_define_module_function(Oj, "remove_to_json", oj_remove_to_json, -1);
1839
+
1840
+ rb_define_module_function(Oj, "register_odd", register_odd, -1);
1841
+ rb_define_module_function(Oj, "register_odd_raw", register_odd_raw, -1);
1842
+
1843
+ rb_define_module_function(Oj, "saj_parse", oj_saj_parse, -1);
1844
+ rb_define_module_function(Oj, "sc_parse", oj_sc_parse, -1);
1845
+
1846
+ rb_define_module_function(Oj, "optimize_rails", oj_optimize_rails, 0);
1847
+
1848
+ rb_define_module_function(Oj, "mem_report", mem_report, 0);
1849
+
1850
+ oj_add_value_id = rb_intern("add_value");
1851
+ oj_array_append_id = rb_intern("array_append");
1852
+ oj_array_end_id = rb_intern("array_end");
1853
+ oj_array_start_id = rb_intern("array_start");
1854
+ oj_as_json_id = rb_intern("as_json");
1855
+ oj_begin_id = rb_intern("begin");
1856
+ oj_bigdecimal_id = rb_intern("BigDecimal");
1857
+ oj_end_id = rb_intern("end");
1858
+ oj_eofq_id = rb_intern("eof?");
1859
+ oj_error_id = rb_intern("error");
1860
+ oj_exclude_end_id = rb_intern("exclude_end?");
1861
+ oj_file_id = rb_intern("file?");
1862
+ oj_fileno_id = rb_intern("fileno");
1863
+ oj_ftype_id = rb_intern("ftype");
1864
+ oj_hash_end_id = rb_intern("hash_end");
1865
+ oj_hash_key_id = rb_intern("hash_key");
1866
+ oj_hash_set_id = rb_intern("hash_set");
1867
+ oj_hash_start_id = rb_intern("hash_start");
1868
+ oj_iconv_id = rb_intern("iconv");
1869
+ oj_json_create_id = rb_intern("json_create");
1870
+ oj_length_id = rb_intern("length");
1871
+ oj_new_id = rb_intern("new");
1872
+ oj_parse_id = rb_intern("parse");
1873
+ oj_plus_id = rb_intern("+");
1874
+ oj_pos_id = rb_intern("pos");
1875
+ oj_raw_json_id = rb_intern("raw_json");
1876
+ oj_read_id = rb_intern("read");
1877
+ oj_readpartial_id = rb_intern("readpartial");
1878
+ oj_replace_id = rb_intern("replace");
1879
+ oj_stat_id = rb_intern("stat");
1880
+ oj_string_id = rb_intern("string");
1881
+ oj_to_h_id = rb_intern("to_h");
1882
+ oj_to_hash_id = rb_intern("to_hash");
1883
+ oj_to_json_id = rb_intern("to_json");
1884
+ oj_to_s_id = rb_intern("to_s");
1885
+ oj_to_sym_id = rb_intern("to_sym");
1886
+ oj_to_time_id = rb_intern("to_time");
1887
+ oj_tv_nsec_id = rb_intern("tv_nsec");
1888
+ oj_tv_sec_id = rb_intern("tv_sec");
1889
+ oj_tv_usec_id = rb_intern("tv_usec");
1890
+ oj_utc_id = rb_intern("utc");
1891
+ oj_utc_offset_id = rb_intern("utc_offset");
1892
+ oj_utcq_id = rb_intern("utc?");
1893
+ oj_write_id = rb_intern("write");
1894
+
1895
+ rb_require("oj_windows/bag");
1896
+ rb_require("oj_windows/error");
1897
+ rb_require("oj_windows/mimic");
1898
+ rb_require("oj_windows/saj");
1899
+ rb_require("oj_windows/schandler");
1900
+
1901
+ oj_bag_class = rb_const_get_at(Oj, rb_intern("Bag"));
1902
+ rb_gc_register_mark_object(oj_bag_class);
1903
+ oj_bigdecimal_class = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
1904
+ rb_gc_register_mark_object(oj_bigdecimal_class);
1905
+ oj_date_class = rb_const_get(rb_cObject, rb_intern("Date"));
1906
+ rb_gc_register_mark_object(oj_date_class);
1907
+ oj_datetime_class = rb_const_get(rb_cObject, rb_intern("DateTime"));
1908
+ rb_gc_register_mark_object(oj_datetime_class);
1909
+ oj_enumerable_class = rb_const_get(rb_cObject, rb_intern("Enumerable"));
1910
+ rb_gc_register_mark_object(oj_enumerable_class);
1911
+ oj_parse_error_class = rb_const_get_at(Oj, rb_intern("ParseError"));
1912
+ rb_gc_register_mark_object(oj_parse_error_class);
1913
+ oj_stringio_class = rb_const_get(rb_cObject, rb_intern("StringIO"));
1914
+ rb_gc_register_mark_object(oj_stringio_class);
1915
+ oj_struct_class = rb_const_get(rb_cObject, rb_intern("Struct"));
1916
+ rb_gc_register_mark_object(oj_struct_class);
1917
+ oj_json_parser_error_class = rb_eEncodingError; // replaced if mimic is called
1918
+ oj_json_generator_error_class = rb_eEncodingError; // replaced if mimic is called
1919
+
1920
+ allow_blank_sym = ID2SYM(rb_intern("allow_blank"));
1921
+ rb_gc_register_address(&allow_blank_sym);
1922
+ allow_gc_sym = ID2SYM(rb_intern("allow_gc"));
1923
+ rb_gc_register_address(&allow_gc_sym);
1924
+ allow_invalid_unicode_sym = ID2SYM(rb_intern("allow_invalid_unicode"));
1925
+ rb_gc_register_address(&allow_invalid_unicode_sym);
1926
+ ascii_sym = ID2SYM(rb_intern("ascii"));
1927
+ rb_gc_register_address(&ascii_sym);
1928
+ auto_define_sym = ID2SYM(rb_intern("auto_define"));
1929
+ rb_gc_register_address(&auto_define_sym);
1930
+ auto_sym = ID2SYM(rb_intern("auto"));
1931
+ rb_gc_register_address(&auto_sym);
1932
+ bigdecimal_as_decimal_sym = ID2SYM(rb_intern("bigdecimal_as_decimal"));
1933
+ rb_gc_register_address(&bigdecimal_as_decimal_sym);
1934
+ bigdecimal_load_sym = ID2SYM(rb_intern("bigdecimal_load"));
1935
+ rb_gc_register_address(&bigdecimal_load_sym);
1936
+ bigdecimal_sym = ID2SYM(rb_intern("bigdecimal"));
1937
+ rb_gc_register_address(&bigdecimal_sym);
1938
+ cache_keys_sym = ID2SYM(rb_intern("cache_keys"));
1939
+ rb_gc_register_address(&cache_keys_sym);
1940
+ cache_str_sym = ID2SYM(rb_intern("cache_str"));
1941
+ rb_gc_register_address(&cache_str_sym);
1942
+ cache_string_sym = ID2SYM(rb_intern("cache_string"));
1943
+ rb_gc_register_address(&cache_string_sym);
1944
+ circular_sym = ID2SYM(rb_intern("circular"));
1945
+ rb_gc_register_address(&circular_sym);
1946
+ class_cache_sym = ID2SYM(rb_intern("class_cache"));
1947
+ rb_gc_register_address(&class_cache_sym);
1948
+ compat_bigdecimal_sym = ID2SYM(rb_intern("compat_bigdecimal"));
1949
+ rb_gc_register_address(&compat_bigdecimal_sym);
1950
+ compat_sym = ID2SYM(rb_intern("compat"));
1951
+ rb_gc_register_address(&compat_sym);
1952
+ create_id_sym = ID2SYM(rb_intern("create_id"));
1953
+ rb_gc_register_address(&create_id_sym);
1954
+ custom_sym = ID2SYM(rb_intern("custom"));
1955
+ rb_gc_register_address(&custom_sym);
1956
+ empty_string_sym = ID2SYM(rb_intern("empty_string"));
1957
+ rb_gc_register_address(&empty_string_sym);
1958
+ escape_mode_sym = ID2SYM(rb_intern("escape_mode"));
1959
+ rb_gc_register_address(&escape_mode_sym);
1960
+ integer_range_sym = ID2SYM(rb_intern("integer_range"));
1961
+ rb_gc_register_address(&integer_range_sym);
1962
+ fast_sym = ID2SYM(rb_intern("fast"));
1963
+ rb_gc_register_address(&fast_sym);
1964
+ float_format_sym = ID2SYM(rb_intern("float_format"));
1965
+ rb_gc_register_address(&float_format_sym);
1966
+ float_prec_sym = ID2SYM(rb_intern("float_precision"));
1967
+ rb_gc_register_address(&float_prec_sym);
1968
+ float_sym = ID2SYM(rb_intern("float"));
1969
+ rb_gc_register_address(&float_sym);
1970
+ huge_sym = ID2SYM(rb_intern("huge"));
1971
+ rb_gc_register_address(&huge_sym);
1972
+ ignore_sym = ID2SYM(rb_intern("ignore"));
1973
+ rb_gc_register_address(&ignore_sym);
1974
+ ignore_under_sym = ID2SYM(rb_intern("ignore_under"));
1975
+ rb_gc_register_address(&ignore_under_sym);
1976
+ json_sym = ID2SYM(rb_intern("json"));
1977
+ rb_gc_register_address(&json_sym);
1978
+ match_string_sym = ID2SYM(rb_intern("match_string"));
1979
+ rb_gc_register_address(&match_string_sym);
1980
+ mode_sym = ID2SYM(rb_intern("mode"));
1981
+ rb_gc_register_address(&mode_sym);
1982
+ nan_sym = ID2SYM(rb_intern("nan"));
1983
+ rb_gc_register_address(&nan_sym);
1984
+ newline_sym = ID2SYM(rb_intern("newline"));
1985
+ rb_gc_register_address(&newline_sym);
1986
+ nilnil_sym = ID2SYM(rb_intern("nilnil"));
1987
+ rb_gc_register_address(&nilnil_sym);
1988
+ null_sym = ID2SYM(rb_intern("null"));
1989
+ rb_gc_register_address(&null_sym);
1990
+ object_sym = ID2SYM(rb_intern("object"));
1991
+ rb_gc_register_address(&object_sym);
1992
+ oj_allow_nan_sym = ID2SYM(rb_intern("allow_nan"));
1993
+ rb_gc_register_address(&oj_allow_nan_sym);
1994
+ oj_array_class_sym = ID2SYM(rb_intern("array_class"));
1995
+ rb_gc_register_address(&oj_array_class_sym);
1996
+ oj_array_nl_sym = ID2SYM(rb_intern("array_nl"));
1997
+ rb_gc_register_address(&oj_array_nl_sym);
1998
+ oj_ascii_only_sym = ID2SYM(rb_intern("ascii_only"));
1999
+ rb_gc_register_address(&oj_ascii_only_sym);
2000
+ oj_create_additions_sym = ID2SYM(rb_intern("create_additions"));
2001
+ rb_gc_register_address(&oj_create_additions_sym);
2002
+ oj_decimal_class_sym = ID2SYM(rb_intern("decimal_class"));
2003
+ rb_gc_register_address(&oj_decimal_class_sym);
2004
+ oj_hash_class_sym = ID2SYM(rb_intern("hash_class"));
2005
+ rb_gc_register_address(&oj_hash_class_sym);
2006
+ oj_indent_sym = ID2SYM(rb_intern("indent"));
2007
+ rb_gc_register_address(&oj_indent_sym);
2008
+ oj_max_nesting_sym = ID2SYM(rb_intern("max_nesting"));
2009
+ rb_gc_register_address(&oj_max_nesting_sym);
2010
+ oj_object_class_sym = ID2SYM(rb_intern("object_class"));
2011
+ rb_gc_register_address(&oj_object_class_sym);
2012
+ oj_object_nl_sym = ID2SYM(rb_intern("object_nl"));
2013
+ rb_gc_register_address(&oj_object_nl_sym);
2014
+ oj_quirks_mode_sym = ID2SYM(rb_intern("quirks_mode"));
2015
+ rb_gc_register_address(&oj_quirks_mode_sym);
2016
+ oj_safe_sym = ID2SYM(rb_intern("safe"));
2017
+ rb_gc_register_address(&oj_safe_sym);
2018
+ omit_null_byte_sym = ID2SYM(rb_intern("omit_null_byte"));
2019
+ rb_gc_register_address(&omit_null_byte_sym);
2020
+ oj_space_before_sym = ID2SYM(rb_intern("space_before"));
2021
+ rb_gc_register_address(&oj_space_before_sym);
2022
+ oj_space_sym = ID2SYM(rb_intern("space"));
2023
+ rb_gc_register_address(&oj_space_sym);
2024
+ oj_trace_sym = ID2SYM(rb_intern("trace"));
2025
+ rb_gc_register_address(&oj_trace_sym);
2026
+ omit_nil_sym = ID2SYM(rb_intern("omit_nil"));
2027
+ rb_gc_register_address(&omit_nil_sym);
2028
+ rails_sym = ID2SYM(rb_intern("rails"));
2029
+ rb_gc_register_address(&rails_sym);
2030
+ raise_sym = ID2SYM(rb_intern("raise"));
2031
+ rb_gc_register_address(&raise_sym);
2032
+ ruby_sym = ID2SYM(rb_intern("ruby"));
2033
+ rb_gc_register_address(&ruby_sym);
2034
+ sec_prec_sym = ID2SYM(rb_intern("second_precision"));
2035
+ rb_gc_register_address(&sec_prec_sym);
2036
+ slash_sym = ID2SYM(rb_intern("slash"));
2037
+ rb_gc_register_address(&slash_sym);
2038
+ strict_sym = ID2SYM(rb_intern("strict"));
2039
+ rb_gc_register_address(&strict_sym);
2040
+ symbol_keys_sym = ID2SYM(rb_intern("symbol_keys"));
2041
+ rb_gc_register_address(&symbol_keys_sym);
2042
+ oj_symbolize_names_sym = ID2SYM(rb_intern("symbolize_names"));
2043
+ rb_gc_register_address(&oj_symbolize_names_sym);
2044
+ time_format_sym = ID2SYM(rb_intern("time_format"));
2045
+ rb_gc_register_address(&time_format_sym);
2046
+ unicode_xss_sym = ID2SYM(rb_intern("unicode_xss"));
2047
+ rb_gc_register_address(&unicode_xss_sym);
2048
+ unix_sym = ID2SYM(rb_intern("unix"));
2049
+ rb_gc_register_address(&unix_sym);
2050
+ unix_zone_sym = ID2SYM(rb_intern("unix_zone"));
2051
+ rb_gc_register_address(&unix_zone_sym);
2052
+ use_as_json_sym = ID2SYM(rb_intern("use_as_json"));
2053
+ rb_gc_register_address(&use_as_json_sym);
2054
+ use_raw_json_sym = ID2SYM(rb_intern("use_raw_json"));
2055
+ rb_gc_register_address(&use_raw_json_sym);
2056
+ use_to_hash_sym = ID2SYM(rb_intern("use_to_hash"));
2057
+ rb_gc_register_address(&use_to_hash_sym);
2058
+ use_to_json_sym = ID2SYM(rb_intern("use_to_json"));
2059
+ rb_gc_register_address(&use_to_json_sym);
2060
+ wab_sym = ID2SYM(rb_intern("wab"));
2061
+ rb_gc_register_address(&wab_sym);
2062
+ word_sym = ID2SYM(rb_intern("word"));
2063
+ rb_gc_register_address(&word_sym);
2064
+ xmlschema_sym = ID2SYM(rb_intern("xmlschema"));
2065
+ rb_gc_register_address(&xmlschema_sym);
2066
+ xss_safe_sym = ID2SYM(rb_intern("xss_safe"));
2067
+ rb_gc_register_address(&xss_safe_sym);
2068
+
2069
+ oj_slash_string = rb_str_new2("/");
2070
+ rb_gc_register_address(&oj_slash_string);
2071
+ OBJ_FREEZE(oj_slash_string);
2072
+
2073
+ oj_default_options.mode = ObjectMode;
2074
+
2075
+ oj_hash_init();
2076
+ oj_odd_init();
2077
+ oj_mimic_rails_init();
2078
+
2079
+ #if defined(_WIN32)
2080
+ InitializeCriticalSection(&oj_cache_mutex);
2081
+ #elif HAVE_PTHREAD_MUTEX_INIT
2082
+ if (0 != (err = pthread_mutex_init(&oj_cache_mutex, 0))) {
2083
+ rb_raise(rb_eException, "failed to initialize a mutex. %s", strerror(err));
2084
+ }
2085
+ #else
2086
+ oj_cache_mutex = rb_mutex_new();
2087
+ rb_gc_register_address(&oj_cache_mutex);
2088
+ #endif
2089
+ oj_init_doc();
2090
+
2091
+ oj_parser_init();
2092
+ oj_scanner_init();
2093
+
2094
+ #ifdef HAVE_SIMD_NEON
2095
+ initialize_neon();
2096
+ #endif /* HAVE_SIMD_NEON */
2097
+ }