oj 3.7.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +96 -0
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +107 -0
  6. data/ext/oj/cache8.h +48 -0
  7. data/ext/oj/circarray.c +68 -0
  8. data/ext/oj/circarray.h +23 -0
  9. data/ext/oj/code.c +235 -0
  10. data/ext/oj/code.h +42 -0
  11. data/ext/oj/compat.c +299 -0
  12. data/ext/oj/custom.c +1188 -0
  13. data/ext/oj/dump.c +1232 -0
  14. data/ext/oj/dump.h +94 -0
  15. data/ext/oj/dump_compat.c +973 -0
  16. data/ext/oj/dump_leaf.c +252 -0
  17. data/ext/oj/dump_object.c +837 -0
  18. data/ext/oj/dump_strict.c +433 -0
  19. data/ext/oj/encode.h +45 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +47 -0
  23. data/ext/oj/fast.c +1771 -0
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +873 -0
  28. data/ext/oj/object.c +771 -0
  29. data/ext/oj/odd.c +231 -0
  30. data/ext/oj/odd.h +44 -0
  31. data/ext/oj/oj.c +1694 -0
  32. data/ext/oj/oj.h +381 -0
  33. data/ext/oj/parse.c +1085 -0
  34. data/ext/oj/parse.h +111 -0
  35. data/ext/oj/rails.c +1485 -0
  36. data/ext/oj/rails.h +21 -0
  37. data/ext/oj/reader.c +231 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +102 -0
  40. data/ext/oj/resolve.h +14 -0
  41. data/ext/oj/rxclass.c +147 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +714 -0
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +910 -0
  46. data/ext/oj/stream_writer.c +363 -0
  47. data/ext/oj/strict.c +212 -0
  48. data/ext/oj/string_writer.c +512 -0
  49. data/ext/oj/trace.c +79 -0
  50. data/ext/oj/trace.h +28 -0
  51. data/ext/oj/util.c +136 -0
  52. data/ext/oj/util.h +19 -0
  53. data/ext/oj/val_stack.c +118 -0
  54. data/ext/oj/val_stack.h +185 -0
  55. data/ext/oj/wab.c +631 -0
  56. data/lib/oj.rb +21 -0
  57. data/lib/oj/active_support_helper.rb +41 -0
  58. data/lib/oj/bag.rb +88 -0
  59. data/lib/oj/easy_hash.rb +52 -0
  60. data/lib/oj/error.rb +22 -0
  61. data/lib/oj/json.rb +176 -0
  62. data/lib/oj/mimic.rb +267 -0
  63. data/lib/oj/saj.rb +66 -0
  64. data/lib/oj/schandler.rb +142 -0
  65. data/lib/oj/state.rb +131 -0
  66. data/lib/oj/version.rb +5 -0
  67. data/pages/Advanced.md +22 -0
  68. data/pages/Compatibility.md +25 -0
  69. data/pages/Custom.md +23 -0
  70. data/pages/Encoding.md +65 -0
  71. data/pages/JsonGem.md +79 -0
  72. data/pages/Modes.md +154 -0
  73. data/pages/Options.md +266 -0
  74. data/pages/Rails.md +116 -0
  75. data/pages/Security.md +20 -0
  76. data/pages/WAB.md +13 -0
  77. data/test/_test_active.rb +76 -0
  78. data/test/_test_active_mimic.rb +96 -0
  79. data/test/_test_mimic_rails.rb +126 -0
  80. data/test/activerecord/result_test.rb +27 -0
  81. data/test/activesupport4/decoding_test.rb +108 -0
  82. data/test/activesupport4/encoding_test.rb +531 -0
  83. data/test/activesupport4/test_helper.rb +41 -0
  84. data/test/activesupport5/decoding_test.rb +125 -0
  85. data/test/activesupport5/encoding_test.rb +485 -0
  86. data/test/activesupport5/encoding_test_cases.rb +90 -0
  87. data/test/activesupport5/test_helper.rb +50 -0
  88. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  89. data/test/big.rb +15 -0
  90. data/test/files.rb +29 -0
  91. data/test/foo.rb +33 -0
  92. data/test/helper.rb +26 -0
  93. data/test/isolated/shared.rb +308 -0
  94. data/test/isolated/test_mimic_after.rb +13 -0
  95. data/test/isolated/test_mimic_alone.rb +12 -0
  96. data/test/isolated/test_mimic_as_json.rb +45 -0
  97. data/test/isolated/test_mimic_before.rb +13 -0
  98. data/test/isolated/test_mimic_define.rb +28 -0
  99. data/test/isolated/test_mimic_rails_after.rb +22 -0
  100. data/test/isolated/test_mimic_rails_before.rb +21 -0
  101. data/test/isolated/test_mimic_redefine.rb +15 -0
  102. data/test/json_gem/json_addition_test.rb +216 -0
  103. data/test/json_gem/json_common_interface_test.rb +148 -0
  104. data/test/json_gem/json_encoding_test.rb +107 -0
  105. data/test/json_gem/json_ext_parser_test.rb +20 -0
  106. data/test/json_gem/json_fixtures_test.rb +35 -0
  107. data/test/json_gem/json_generator_test.rb +383 -0
  108. data/test/json_gem/json_generic_object_test.rb +90 -0
  109. data/test/json_gem/json_parser_test.rb +470 -0
  110. data/test/json_gem/json_string_matching_test.rb +42 -0
  111. data/test/json_gem/test_helper.rb +18 -0
  112. data/test/mem.rb +35 -0
  113. data/test/perf.rb +107 -0
  114. data/test/perf_compat.rb +130 -0
  115. data/test/perf_fast.rb +164 -0
  116. data/test/perf_file.rb +64 -0
  117. data/test/perf_object.rb +138 -0
  118. data/test/perf_saj.rb +109 -0
  119. data/test/perf_scp.rb +151 -0
  120. data/test/perf_simple.rb +287 -0
  121. data/test/perf_strict.rb +145 -0
  122. data/test/perf_wab.rb +131 -0
  123. data/test/sample.rb +54 -0
  124. data/test/sample/change.rb +14 -0
  125. data/test/sample/dir.rb +19 -0
  126. data/test/sample/doc.rb +36 -0
  127. data/test/sample/file.rb +48 -0
  128. data/test/sample/group.rb +16 -0
  129. data/test/sample/hasprops.rb +16 -0
  130. data/test/sample/layer.rb +12 -0
  131. data/test/sample/line.rb +20 -0
  132. data/test/sample/oval.rb +10 -0
  133. data/test/sample/rect.rb +10 -0
  134. data/test/sample/shape.rb +35 -0
  135. data/test/sample/text.rb +20 -0
  136. data/test/sample_json.rb +37 -0
  137. data/test/test_compat.rb +509 -0
  138. data/test/test_custom.rb +406 -0
  139. data/test/test_debian.rb +53 -0
  140. data/test/test_fast.rb +470 -0
  141. data/test/test_file.rb +239 -0
  142. data/test/test_gc.rb +49 -0
  143. data/test/test_hash.rb +29 -0
  144. data/test/test_integer_range.rb +73 -0
  145. data/test/test_null.rb +376 -0
  146. data/test/test_object.rb +1018 -0
  147. data/test/test_saj.rb +186 -0
  148. data/test/test_scp.rb +433 -0
  149. data/test/test_strict.rb +410 -0
  150. data/test/test_various.rb +739 -0
  151. data/test/test_wab.rb +307 -0
  152. data/test/test_writer.rb +380 -0
  153. data/test/tests.rb +24 -0
  154. data/test/tests_mimic.rb +14 -0
  155. data/test/tests_mimic_addition.rb +7 -0
  156. metadata +359 -0
@@ -0,0 +1,231 @@
1
+ /* odd.c
2
+ * Copyright (c) 2011, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #include <string.h>
7
+
8
+ #include "odd.h"
9
+
10
+ static struct _odd _odds[4]; // bump up if new initial Odd classes are added
11
+ static struct _odd *odds = _odds;
12
+ static long odd_cnt = 0;
13
+ static ID sec_id;
14
+ static ID sec_fraction_id;
15
+ static ID to_f_id;
16
+ static ID numerator_id;
17
+ static ID denominator_id;
18
+ static ID rational_id;
19
+ static VALUE rational_class;
20
+
21
+ static void
22
+ set_class(Odd odd, const char *classname) {
23
+ const char **np;
24
+ ID *idp;
25
+
26
+ odd->classname = classname;
27
+ odd->clen = strlen(classname);
28
+ odd->clas = rb_const_get(rb_cObject, rb_intern(classname));
29
+ odd->create_obj = odd->clas;
30
+ odd->create_op = rb_intern("new");
31
+ odd->is_module = (T_MODULE == rb_type(odd->clas));
32
+ odd->raw = 0;
33
+ for (np = odd->attr_names, idp = odd->attrs; 0 != *np; np++, idp++) {
34
+ *idp = rb_intern(*np);
35
+ }
36
+ *idp = 0;
37
+ }
38
+
39
+ static VALUE
40
+ get_datetime_secs(VALUE obj) {
41
+ volatile VALUE rsecs = rb_funcall(obj, sec_id, 0);
42
+ volatile VALUE rfrac = rb_funcall(obj, sec_fraction_id, 0);
43
+ long sec = NUM2LONG(rsecs);
44
+ long long num = rb_num2ll(rb_funcall(rfrac, numerator_id, 0));
45
+ long long den = rb_num2ll(rb_funcall(rfrac, denominator_id, 0));
46
+
47
+ num += sec * den;
48
+
49
+ return rb_funcall(rb_cObject, rational_id, 2, rb_ll2inum(num), rb_ll2inum(den));
50
+ }
51
+
52
+ void
53
+ oj_odd_init() {
54
+ Odd odd;
55
+ const char **np;
56
+
57
+ sec_id = rb_intern("sec");
58
+ sec_fraction_id = rb_intern("sec_fraction");
59
+ to_f_id = rb_intern("to_f");
60
+ numerator_id = rb_intern("numerator");
61
+ denominator_id = rb_intern("denominator");
62
+ rational_id = rb_intern("Rational");
63
+ rational_class = rb_const_get(rb_cObject, rational_id);
64
+
65
+ memset(_odds, 0, sizeof(_odds));
66
+ odd = odds;
67
+ // Rational
68
+ np = odd->attr_names;
69
+ *np++ = "numerator";
70
+ *np++ = "denominator";
71
+ *np = 0;
72
+ set_class(odd, "Rational");
73
+ odd->create_obj = rb_cObject;
74
+ odd->create_op = rational_id;
75
+ odd->attr_cnt = 2;
76
+ // Date
77
+ odd++;
78
+ np = odd->attr_names;
79
+ *np++ = "year";
80
+ *np++ = "month";
81
+ *np++ = "day";
82
+ *np++ = "start";
83
+ *np++ = 0;
84
+ set_class(odd, "Date");
85
+ odd->attr_cnt = 4;
86
+ // DateTime
87
+ odd++;
88
+ np = odd->attr_names;
89
+ *np++ = "year";
90
+ *np++ = "month";
91
+ *np++ = "day";
92
+ *np++ = "hour";
93
+ *np++ = "min";
94
+ *np++ = "sec";
95
+ *np++ = "offset";
96
+ *np++ = "start";
97
+ *np++ = 0;
98
+ set_class(odd, "DateTime");
99
+ odd->attr_cnt = 8;
100
+ odd->attrFuncs[5] = get_datetime_secs;
101
+ // Range
102
+ odd++;
103
+ np = odd->attr_names;
104
+ *np++ = "begin";
105
+ *np++ = "end";
106
+ *np++ = "exclude_end?";
107
+ *np++ = 0;
108
+ set_class(odd, "Range");
109
+ odd->attr_cnt = 3;
110
+
111
+ odd_cnt = odd - odds + 1;
112
+ }
113
+
114
+ Odd
115
+ oj_get_odd(VALUE clas) {
116
+ Odd odd;
117
+ const char *classname = NULL;
118
+
119
+ for (odd = odds + odd_cnt - 1; odds <= odd; odd--) {
120
+ if (clas == odd->clas) {
121
+ return odd;
122
+ }
123
+ if (odd->is_module) {
124
+ if (NULL == classname) {
125
+ classname = rb_class2name(clas);
126
+ }
127
+ if (0 == strncmp(odd->classname, classname, odd->clen) &&
128
+ ':' == classname[odd->clen]) {
129
+ return odd;
130
+ }
131
+ }
132
+ }
133
+ return NULL;
134
+ }
135
+
136
+ Odd
137
+ oj_get_oddc(const char *classname, size_t len) {
138
+ Odd odd;
139
+
140
+ for (odd = odds + odd_cnt - 1; odds <= odd; odd--) {
141
+ if (len == odd->clen && 0 == strncmp(classname, odd->classname, len)) {
142
+ return odd;
143
+ }
144
+ if (odd->is_module &&
145
+ 0 == strncmp(odd->classname, classname, odd->clen) &&
146
+ ':' == classname[odd->clen]) {
147
+ return odd;
148
+ }
149
+ }
150
+ return 0;
151
+ }
152
+
153
+ OddArgs
154
+ oj_odd_alloc_args(Odd odd) {
155
+ OddArgs oa = ALLOC_N(struct _oddArgs, 1);
156
+ VALUE *a;
157
+ int i;
158
+
159
+ oa->odd = odd;
160
+ for (i = odd->attr_cnt, a = oa->args; 0 < i; i--, a++) {
161
+ *a = Qnil;
162
+ }
163
+ return oa;
164
+ }
165
+
166
+ void
167
+ oj_odd_free(OddArgs args) {
168
+ xfree(args);
169
+ }
170
+
171
+ int
172
+ oj_odd_set_arg(OddArgs args, const char *key, size_t klen, VALUE value) {
173
+ const char **np;
174
+ VALUE *vp;
175
+ int i;
176
+
177
+ for (i = args->odd->attr_cnt, np = args->odd->attr_names, vp = args->args; 0 < i; i--, np++, vp++) {
178
+ if (0 == strncmp(key, *np, klen) && '\0' == *((*np) + klen)) {
179
+ *vp = value;
180
+ return 0;
181
+ }
182
+ }
183
+ return -1;
184
+ }
185
+
186
+ void
187
+ oj_reg_odd(VALUE clas, VALUE create_object, VALUE create_method, int mcnt, VALUE *members, bool raw) {
188
+ Odd odd;
189
+ const char **np;
190
+ ID *ap;
191
+ AttrGetFunc *fp;
192
+
193
+ if (_odds == odds) {
194
+ odds = ALLOC_N(struct _odd, odd_cnt + 1);
195
+
196
+ memcpy(odds, _odds, sizeof(struct _odd) * odd_cnt);
197
+ } else {
198
+ REALLOC_N(odds, struct _odd, odd_cnt + 1);
199
+ }
200
+ odd = odds + odd_cnt;
201
+ odd->clas = clas;
202
+ if (NULL == (odd->classname = strdup(rb_class2name(clas)))) {
203
+ rb_raise(rb_eNoMemError, "for attribute name.");
204
+ }
205
+ odd->clen = strlen(odd->classname);
206
+ odd->create_obj = create_object;
207
+ odd->create_op = SYM2ID(create_method);
208
+ odd->attr_cnt = mcnt;
209
+ odd->is_module = (T_MODULE == rb_type(clas));
210
+ odd->raw = raw;
211
+ for (ap = odd->attrs, np = odd->attr_names, fp = odd->attrFuncs; 0 < mcnt; mcnt--, ap++, np++, members++, fp++) {
212
+ *fp = 0;
213
+ switch (rb_type(*members)) {
214
+ case T_STRING:
215
+ if (NULL == (*np = strdup(rb_string_value_ptr(members)))) {
216
+ rb_raise(rb_eNoMemError, "for attribute name.");
217
+ }
218
+ break;
219
+ case T_SYMBOL:
220
+ *np = rb_id2name(SYM2ID(*members));
221
+ break;
222
+ default:
223
+ rb_raise(rb_eArgError, "registered member identifiers must be Strings or Symbols.");
224
+ break;
225
+ }
226
+ *ap = rb_intern(*np);
227
+ }
228
+ *np = 0;
229
+ *ap = 0;
230
+ odd_cnt++;
231
+ }
@@ -0,0 +1,44 @@
1
+ /* odd.h
2
+ * Copyright (c) 2011, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #ifndef OJ_ODD_H
7
+ #define OJ_ODD_H
8
+
9
+ #include <stdbool.h>
10
+
11
+ #include "ruby.h"
12
+
13
+ #define MAX_ODD_ARGS 10
14
+
15
+ typedef VALUE (*AttrGetFunc)(VALUE obj);
16
+
17
+ typedef struct _odd {
18
+ const char *classname;
19
+ size_t clen;
20
+ VALUE clas; // Ruby class or module
21
+ VALUE create_obj;
22
+ ID create_op;
23
+ int attr_cnt;
24
+ bool is_module;
25
+ bool raw;
26
+ const char *attr_names[MAX_ODD_ARGS]; // NULL terminated attr names
27
+ ID attrs[MAX_ODD_ARGS]; // 0 terminated attr IDs
28
+ AttrGetFunc attrFuncs[MAX_ODD_ARGS];
29
+ } *Odd;
30
+
31
+ typedef struct _oddArgs {
32
+ Odd odd;
33
+ VALUE args[MAX_ODD_ARGS];
34
+ } *OddArgs;
35
+
36
+ extern void oj_odd_init(void);
37
+ extern Odd oj_get_odd(VALUE clas);
38
+ extern Odd oj_get_oddc(const char *classname, size_t len);
39
+ extern OddArgs oj_odd_alloc_args(Odd odd);
40
+ extern void oj_odd_free(OddArgs args);
41
+ extern int oj_odd_set_arg(OddArgs args, const char *key, size_t klen, VALUE value);
42
+ extern void oj_reg_odd(VALUE clas, VALUE create_object, VALUE create_method, int mcnt, VALUE *members, bool raw);
43
+
44
+ #endif /* OJ_ODD_H */
@@ -0,0 +1,1694 @@
1
+ /* oj.c
2
+ * Copyright (c) 2012, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #include <stdlib.h>
7
+ #include <errno.h>
8
+ #include <stdio.h>
9
+ #include <string.h>
10
+ #include <sys/types.h>
11
+ #include <unistd.h>
12
+ #include <fcntl.h>
13
+
14
+ #include "oj.h"
15
+ #include "parse.h"
16
+ #include "hash.h"
17
+ #include "odd.h"
18
+ #include "dump.h"
19
+ #include "rails.h"
20
+ #include "encode.h"
21
+
22
+ typedef struct _yesNoOpt {
23
+ VALUE sym;
24
+ char *attr;
25
+ } *YesNoOpt;
26
+
27
+ void Init_oj();
28
+
29
+ VALUE Oj = Qnil;
30
+
31
+ ID oj_add_value_id;
32
+ ID oj_array_append_id;
33
+ ID oj_array_end_id;
34
+ ID oj_array_start_id;
35
+ ID oj_as_json_id;
36
+ ID oj_begin_id;
37
+ ID oj_bigdecimal_id;
38
+ ID oj_end_id;
39
+ ID oj_exclude_end_id;
40
+ ID oj_error_id;
41
+ ID oj_file_id;
42
+ ID oj_fileno_id;
43
+ ID oj_ftype_id;
44
+ ID oj_has_key_id;
45
+ ID oj_hash_end_id;
46
+ ID oj_hash_key_id;
47
+ ID oj_hash_set_id;
48
+ ID oj_hash_start_id;
49
+ ID oj_iconv_id;
50
+ ID oj_instance_variables_id;
51
+ ID oj_json_create_id;
52
+ ID oj_length_id;
53
+ ID oj_new_id;
54
+ ID oj_parse_id;
55
+ ID oj_pos_id;
56
+ ID oj_read_id;
57
+ ID oj_readpartial_id;
58
+ ID oj_replace_id;
59
+ ID oj_stat_id;
60
+ ID oj_string_id;
61
+ ID oj_to_h_id;
62
+ ID oj_to_hash_id;
63
+ ID oj_to_json_id;
64
+ ID oj_to_s_id;
65
+ ID oj_to_sym_id;
66
+ ID oj_to_time_id;
67
+ ID oj_tv_nsec_id;
68
+ ID oj_tv_sec_id;
69
+ ID oj_tv_usec_id;
70
+ ID oj_utc_id;
71
+ ID oj_utc_offset_id;
72
+ ID oj_utcq_id;
73
+ ID oj_write_id;
74
+
75
+
76
+ VALUE oj_bag_class;
77
+ VALUE oj_bigdecimal_class;
78
+ VALUE oj_cstack_class;
79
+ VALUE oj_date_class;
80
+ VALUE oj_datetime_class;
81
+ VALUE oj_enumerable_class;
82
+ VALUE oj_parse_error_class;
83
+ VALUE oj_stream_writer_class;
84
+ VALUE oj_string_writer_class;
85
+ VALUE oj_stringio_class;
86
+ VALUE oj_struct_class;
87
+
88
+ VALUE oj_slash_string;
89
+
90
+ VALUE oj_allow_nan_sym;
91
+ VALUE oj_array_class_sym;
92
+ VALUE oj_create_additions_sym;
93
+ VALUE oj_hash_class_sym;
94
+ VALUE oj_indent_sym;
95
+ VALUE oj_object_class_sym;
96
+ VALUE oj_quirks_mode_sym;
97
+ VALUE oj_trace_sym;
98
+
99
+ static VALUE allow_blank_sym;
100
+ static VALUE allow_gc_sym;
101
+ static VALUE allow_invalid_unicode_sym;
102
+ static VALUE ascii_sym;
103
+ static VALUE auto_define_sym;
104
+ static VALUE auto_sym;
105
+ static VALUE bigdecimal_as_decimal_sym;
106
+ static VALUE bigdecimal_load_sym;
107
+ static VALUE bigdecimal_sym;
108
+ static VALUE circular_sym;
109
+ static VALUE class_cache_sym;
110
+ static VALUE compat_sym;
111
+ static VALUE create_id_sym;
112
+ static VALUE custom_sym;
113
+ static VALUE empty_string_sym;
114
+ static VALUE escape_mode_sym;
115
+ static VALUE integer_range_sym;
116
+ static VALUE float_prec_sym;
117
+ static VALUE float_sym;
118
+ static VALUE huge_sym;
119
+ static VALUE ignore_sym;
120
+ static VALUE json_sym;
121
+ static VALUE match_string_sym;
122
+ static VALUE mode_sym;
123
+ static VALUE nan_sym;
124
+ static VALUE newline_sym;
125
+ static VALUE nilnil_sym;
126
+ static VALUE null_sym;
127
+ static VALUE object_sym;
128
+ static VALUE omit_nil_sym;
129
+ static VALUE rails_sym;
130
+ static VALUE raise_sym;
131
+ static VALUE ruby_sym;
132
+ static VALUE sec_prec_sym;
133
+ static VALUE strict_sym;
134
+ static VALUE symbol_keys_sym;
135
+ static VALUE time_format_sym;
136
+ static VALUE unicode_xss_sym;
137
+ static VALUE unix_sym;
138
+ static VALUE unix_zone_sym;
139
+ static VALUE use_as_json_sym;
140
+ static VALUE use_to_hash_sym;
141
+ static VALUE use_to_json_sym;
142
+ static VALUE wab_sym;
143
+ static VALUE word_sym;
144
+ static VALUE xmlschema_sym;
145
+ static VALUE xss_safe_sym;
146
+
147
+ rb_encoding *oj_utf8_encoding = 0;
148
+
149
+ #if HAVE_LIBPTHREAD
150
+ pthread_mutex_t oj_cache_mutex;
151
+ #else
152
+ VALUE oj_cache_mutex = Qnil;
153
+ #endif
154
+
155
+ const char oj_json_class[] = "json_class";
156
+
157
+ struct _options oj_default_options = {
158
+ 0, // indent
159
+ No, // circular
160
+ No, // auto_define
161
+ No, // sym_key
162
+ JSONEsc, // escape_mode
163
+ ObjectMode, // mode
164
+ Yes, // class_cache
165
+ UnixTime, // time_format
166
+ NotSet, // bigdec_as_num
167
+ AutoDec, // bigdec_load
168
+ No, // to_hash
169
+ No, // to_json
170
+ No, // as_json
171
+ No, // nilnil
172
+ Yes, // empty_string
173
+ Yes, // allow_gc
174
+ Yes, // quirks_mode
175
+ No, // allow_invalid
176
+ No, // create_ok
177
+ Yes, // allow_nan
178
+ No, // trace
179
+ 0, // integer_range_min
180
+ 0, // integer_range_max
181
+ oj_json_class, // create_id
182
+ 10, // create_id_len
183
+ 9, // sec_prec
184
+ 16, // float_prec
185
+ "%0.15g", // float_fmt
186
+ Qnil, // hash_class
187
+ Qnil, // array_class
188
+ { // dump_opts
189
+ false, //use
190
+ "", // indent
191
+ "", // before_sep
192
+ "", // after_sep
193
+ "", // hash_nl
194
+ "", // array_nl
195
+ 0, // indent_size
196
+ 0, // before_size
197
+ 0, // after_size
198
+ 0, // hash_size
199
+ 0, // array_size
200
+ AutoNan,// nan_dump
201
+ false, // omit_nil
202
+ MAX_DEPTH, // max_depth
203
+ },
204
+ { // str_rx
205
+ NULL, // head
206
+ NULL, // tail
207
+ { '\0' }, // err
208
+ },
209
+ NULL, // ignore
210
+ };
211
+
212
+ /* Document-method: default_options()
213
+ * call-seq: default_options()
214
+ *
215
+ * Returns the default load and dump options as a Hash. The options are
216
+ * - *:indent* [_Fixnum_|_String_|_nil_] number of spaces to indent each element in an JSON document, zero or nil is no newline between JSON elements, negative indicates no newline between top level JSON elements in a stream, a String indicates the string should be used for indentation
217
+ * - *:circular* [_Boolean_|_nil_] support circular references while dumping
218
+ * - *:auto_define* [_Boolean_|_nil_] automatically define classes if they do not exist
219
+ * - *:symbol_keys* [_Boolean_|_nil_] use symbols instead of strings for hash keys
220
+ * - *:escape_mode* [_:newline_|_:json_|_:xss_safe_|_:ascii_|_unicode_xss_|_nil_] determines the characters to escape
221
+ * - *:class_cache* [_Boolean_|_nil_] cache classes for faster parsing (if dynamically modifying classes or reloading classes then don't use this)
222
+ * - *:mode* [_:object_|_:strict_|_:compat_|_:null_|_:custom_|_:rails_|_:wab_] load and dump modes to use for JSON
223
+ * - *:time_format* [_:unix_|_:unix_zone_|_:xmlschema_|_:ruby_] time format when dumping
224
+ * - *:bigdecimal_as_decimal* [_Boolean_|_nil_] dump BigDecimal as a decimal number or as a String
225
+ * - *:bigdecimal_load* [_:bigdecimal_|_:float_|_:auto_] load decimals as BigDecimal instead of as a Float. :auto pick the most precise for the number of digits.
226
+ * - *:create_id* [_String_|_nil_] create id for json compatible object encoding, default is 'json_class'
227
+ * - *:create_additions* [_Boolean_|_nil_] if true allow creation of instances using create_id on load.
228
+ * - *:second_precision* [_Fixnum_|_nil_] number of digits after the decimal when dumping the seconds portion of time
229
+ * - *:float_precision* [_Fixnum_|_nil_] number of digits of precision when dumping floats, 0 indicates use Ruby
230
+ * - *:use_to_json* [_Boolean_|_nil_] call to_json() methods on dump, default is false
231
+ * - *:use_as_json* [_Boolean_|_nil_] call as_json() methods on dump, default is false
232
+ * - *:nilnil* [_Boolean_|_nil_] if true a nil input to load will return nil and not raise an Exception
233
+ * - *:empty_string* [_Boolean_|_nil_] if true an empty input will not raise an Exception
234
+ * - *:allow_gc* [_Boolean_|_nil_] allow or prohibit GC during parsing, default is true (allow)
235
+ * - *:quirks_mode* [_true,_|_false_|_nil_] Allow single JSON values instead of documents, default is true (allow)
236
+ * - *:allow_invalid_unicode* [_true,_|_false_|_nil_] Allow invalid unicode, default is false (don't allow)
237
+ * - *:allow_nan* [_true,_|_false_|_nil_] Allow Nan, Infinity, and -Infinity to be parsed, default is true (allow)
238
+ * - *:indent_str* [_String_|_nil_] String to use for indentation, overriding the indent option is not nil
239
+ * - *:space* [_String_|_nil_] String to use for the space after the colon in JSON object fields
240
+ * - *:space_before* [_String_|_nil_] String to use before the colon separator in JSON object fields
241
+ * - *:object_nl* [_String_|_nil_] String to use after a JSON object field value
242
+ * - *:array_nl* [_String_|_nil_] String to use after a JSON array value
243
+ * - *:nan* [_:null_|_:huge_|_:word_|_:raise_|_:auto_] how to dump Infinity and NaN. :null places a null, :huge places a huge number, :word places Infinity or NaN, :raise raises and exception, :auto uses default for each mode.
244
+ * - *:hash_class* [_Class_|_nil_] Class to use instead of Hash on load, :object_class can also be used
245
+ * - *:array_class* [_Class_|_nil_] Class to use instead of Array on load
246
+ * - *:omit_nil* [_true_|_false_] if true Hash and Object attributes with nil values are omitted
247
+ * - *:ignore* [_nil_|Array] either nil or an Array of classes to ignore when dumping
248
+ * - *:integer_range* [_Range_] Dump integers outside range as strings.
249
+ * - *:trace* [_true,_|_false_] Trace all load and dump calls, default is false (trace is off)
250
+ *
251
+ * Return [_Hash_] all current option settings.
252
+ */
253
+ static VALUE
254
+ get_def_opts(VALUE self) {
255
+ VALUE opts = rb_hash_new();
256
+
257
+ if (0 == oj_default_options.dump_opts.indent_size) {
258
+ rb_hash_aset(opts, oj_indent_sym, INT2FIX(oj_default_options.indent));
259
+ } else {
260
+ rb_hash_aset(opts, oj_indent_sym, rb_str_new2(oj_default_options.dump_opts.indent_str));
261
+ }
262
+ rb_hash_aset(opts, sec_prec_sym, INT2FIX(oj_default_options.sec_prec));
263
+ rb_hash_aset(opts, circular_sym, (Yes == oj_default_options.circular) ? Qtrue : ((No == oj_default_options.circular) ? Qfalse : Qnil));
264
+ rb_hash_aset(opts, class_cache_sym, (Yes == oj_default_options.class_cache) ? Qtrue : ((No == oj_default_options.class_cache) ? Qfalse : Qnil));
265
+ rb_hash_aset(opts, auto_define_sym, (Yes == oj_default_options.auto_define) ? Qtrue : ((No == oj_default_options.auto_define) ? Qfalse : Qnil));
266
+ rb_hash_aset(opts, symbol_keys_sym, (Yes == oj_default_options.sym_key) ? Qtrue : ((No == oj_default_options.sym_key) ? Qfalse : Qnil));
267
+ rb_hash_aset(opts, bigdecimal_as_decimal_sym, (Yes == oj_default_options.bigdec_as_num) ? Qtrue : ((No == oj_default_options.bigdec_as_num) ? Qfalse : Qnil));
268
+ rb_hash_aset(opts, oj_create_additions_sym, (Yes == oj_default_options.create_ok) ? Qtrue : ((No == oj_default_options.create_ok) ? Qfalse : Qnil));
269
+ rb_hash_aset(opts, use_to_json_sym, (Yes == oj_default_options.to_json) ? Qtrue : ((No == oj_default_options.to_json) ? Qfalse : Qnil));
270
+ rb_hash_aset(opts, use_to_hash_sym, (Yes == oj_default_options.to_hash) ? Qtrue : ((No == oj_default_options.to_hash) ? Qfalse : Qnil));
271
+ rb_hash_aset(opts, use_as_json_sym, (Yes == oj_default_options.as_json) ? Qtrue : ((No == oj_default_options.as_json) ? Qfalse : Qnil));
272
+ rb_hash_aset(opts, nilnil_sym, (Yes == oj_default_options.nilnil) ? Qtrue : ((No == oj_default_options.nilnil) ? Qfalse : Qnil));
273
+ rb_hash_aset(opts, empty_string_sym, (Yes == oj_default_options.empty_string) ? Qtrue : ((No == oj_default_options.empty_string) ? Qfalse : Qnil));
274
+ rb_hash_aset(opts, allow_gc_sym, (Yes == oj_default_options.allow_gc) ? Qtrue : ((No == oj_default_options.allow_gc) ? Qfalse : Qnil));
275
+ rb_hash_aset(opts, oj_quirks_mode_sym, (Yes == oj_default_options.quirks_mode) ? Qtrue : ((No == oj_default_options.quirks_mode) ? Qfalse : Qnil));
276
+ rb_hash_aset(opts, allow_invalid_unicode_sym, (Yes == oj_default_options.allow_invalid) ? Qtrue : ((No == oj_default_options.allow_invalid) ? Qfalse : Qnil));
277
+ rb_hash_aset(opts, oj_allow_nan_sym, (Yes == oj_default_options.allow_nan) ? Qtrue : ((No == oj_default_options.allow_nan) ? Qfalse : Qnil));
278
+ rb_hash_aset(opts, oj_trace_sym, (Yes == oj_default_options.trace) ? Qtrue : ((No == oj_default_options.trace) ? Qfalse : Qnil));
279
+ rb_hash_aset(opts, float_prec_sym, INT2FIX(oj_default_options.float_prec));
280
+ switch (oj_default_options.mode) {
281
+ case StrictMode: rb_hash_aset(opts, mode_sym, strict_sym); break;
282
+ case CompatMode: rb_hash_aset(opts, mode_sym, compat_sym); break;
283
+ case NullMode: rb_hash_aset(opts, mode_sym, null_sym); break;
284
+ case ObjectMode: rb_hash_aset(opts, mode_sym, object_sym); break;
285
+ case CustomMode: rb_hash_aset(opts, mode_sym, custom_sym); break;
286
+ case RailsMode: rb_hash_aset(opts, mode_sym, rails_sym); break;
287
+ case WabMode: rb_hash_aset(opts, mode_sym, wab_sym); break;
288
+ default: rb_hash_aset(opts, mode_sym, object_sym); break;
289
+ }
290
+
291
+ if (oj_default_options.integer_range_max != 0 || oj_default_options.integer_range_min != 0) {
292
+ VALUE range = rb_obj_alloc(rb_cRange);
293
+ VALUE min = LONG2FIX(oj_default_options.integer_range_min);
294
+ VALUE max = LONG2FIX(oj_default_options.integer_range_max);
295
+ rb_ivar_set(range, oj_begin_id, min);
296
+ rb_ivar_set(range, oj_end_id, max);
297
+ rb_hash_aset(opts, integer_range_sym, range);
298
+ }
299
+ else {
300
+ rb_hash_aset(opts, integer_range_sym, Qnil);
301
+ }
302
+ switch (oj_default_options.escape_mode) {
303
+ case NLEsc: rb_hash_aset(opts, escape_mode_sym, newline_sym); break;
304
+ case JSONEsc: rb_hash_aset(opts, escape_mode_sym, json_sym); break;
305
+ case XSSEsc: rb_hash_aset(opts, escape_mode_sym, xss_safe_sym); break;
306
+ case ASCIIEsc: rb_hash_aset(opts, escape_mode_sym, ascii_sym); break;
307
+ case JXEsc: rb_hash_aset(opts, escape_mode_sym, unicode_xss_sym); break;
308
+ default: rb_hash_aset(opts, escape_mode_sym, json_sym); break;
309
+ }
310
+ switch (oj_default_options.time_format) {
311
+ case XmlTime: rb_hash_aset(opts, time_format_sym, xmlschema_sym); break;
312
+ case RubyTime: rb_hash_aset(opts, time_format_sym, ruby_sym); break;
313
+ case UnixZTime: rb_hash_aset(opts, time_format_sym, unix_zone_sym); break;
314
+ case UnixTime:
315
+ default: rb_hash_aset(opts, time_format_sym, unix_sym); break;
316
+ }
317
+ switch (oj_default_options.bigdec_load) {
318
+ case BigDec: rb_hash_aset(opts, bigdecimal_load_sym, bigdecimal_sym);break;
319
+ case FloatDec: rb_hash_aset(opts, bigdecimal_load_sym, float_sym); break;
320
+ case AutoDec:
321
+ default: rb_hash_aset(opts, bigdecimal_load_sym, auto_sym); break;
322
+ }
323
+ rb_hash_aset(opts, create_id_sym, (0 == oj_default_options.create_id) ? Qnil : rb_str_new2(oj_default_options.create_id));
324
+ rb_hash_aset(opts, oj_space_sym, (0 == oj_default_options.dump_opts.after_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.after_sep));
325
+ rb_hash_aset(opts, oj_space_before_sym, (0 == oj_default_options.dump_opts.before_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.before_sep));
326
+ rb_hash_aset(opts, oj_object_nl_sym, (0 == oj_default_options.dump_opts.hash_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.hash_nl));
327
+ rb_hash_aset(opts, oj_array_nl_sym, (0 == oj_default_options.dump_opts.array_size) ? Qnil : rb_str_new2(oj_default_options.dump_opts.array_nl));
328
+
329
+ switch (oj_default_options.dump_opts.nan_dump) {
330
+ case NullNan: rb_hash_aset(opts, nan_sym, null_sym); break;
331
+ case RaiseNan: rb_hash_aset(opts, nan_sym, raise_sym); break;
332
+ case WordNan: rb_hash_aset(opts, nan_sym, word_sym); break;
333
+ case HugeNan: rb_hash_aset(opts, nan_sym, huge_sym); break;
334
+ case AutoNan:
335
+ default: rb_hash_aset(opts, nan_sym, auto_sym); break;
336
+ }
337
+ rb_hash_aset(opts, omit_nil_sym, oj_default_options.dump_opts.omit_nil ? Qtrue : Qfalse);
338
+ rb_hash_aset(opts, oj_hash_class_sym, oj_default_options.hash_class);
339
+ rb_hash_aset(opts, oj_array_class_sym, oj_default_options.array_class);
340
+
341
+ if (NULL == oj_default_options.ignore) {
342
+ rb_hash_aset(opts, ignore_sym, Qnil);
343
+ } else {
344
+ VALUE *vp;
345
+ volatile VALUE a = rb_ary_new();
346
+
347
+ for (vp = oj_default_options.ignore; Qnil != *vp; vp++) {
348
+ rb_ary_push(a, *vp);
349
+ }
350
+ rb_hash_aset(opts, ignore_sym, a);
351
+ }
352
+ return opts;
353
+ }
354
+
355
+ /* Document-method: default_options=
356
+ * call-seq: default_options=(opts)
357
+ *
358
+ * Sets the default options for load and dump.
359
+ * - *opts* [_Hash_] options to change
360
+ * - *:indent* [_Fixnum_|_String_|_nil_] number of spaces to indent each element in a JSON document or the String to use for identation.
361
+ * - :circular [_Boolean_|_nil_] support circular references while dumping.
362
+ * - *:auto_define* [_Boolean_|_nil_] automatically define classes if they do not exist.
363
+ * - *:symbol_keys* [_Boolean_|_nil_] convert hash keys to symbols.
364
+ * - *:class_cache* [_Boolean_|_nil_] cache classes for faster parsing.
365
+ * - *:escape* [_:newline_|_:json_|_:xss_safe_|_:ascii_|_unicode_xss_|_nil_] mode encodes all high-bit characters as escaped sequences if :ascii, :json is standand UTF-8 JSON encoding, :newline is the same as :json but newlines are not escaped, :unicode_xss allows unicode but escapes &, <, and >, and any \u20xx characters along with some others, and :xss_safe escapes &, <, and >, and some others.
366
+ * - *:bigdecimal_as_decimal* [_Boolean_|_nil_] dump BigDecimal as a decimal number or as a String.
367
+ * - *:bigdecimal_load* [_:bigdecimal_|_:float_|_:auto_|_nil_] load decimals as BigDecimal instead of as a Float. :auto pick the most precise for the number of digits.
368
+ * - *:mode* [_:object_|_:strict_|_:compat_|_:null_|_:custom_|_:rails_|_:wab_] load and dump mode to use for JSON :strict raises an exception when a non-supported Object is encountered. :compat attempts to extract variable values from an Object using to_json() or to_hash() then it walks the Object's variables if neither is found. The :object mode ignores to_hash() and to_json() methods and encodes variables using code internal to the Oj gem. The :null mode ignores non-supported Objects and replaces them with a null. The :custom mode honors all dump options. The :rails more mimics rails and Active behavior.
369
+ * - *:time_format* [_:unix_|_:xmlschema_|_:ruby_] time format when dumping in :compat mode :unix decimal number denoting the number of seconds since 1/1/1970, :unix_zone decimal number denoting the number of seconds since 1/1/1970 plus the utc_offset in the exponent, :xmlschema date-time format taken from XML Schema as a String, :ruby Time.to_s formatted String.
370
+ * - *:create_id* [_String_|_nil_] create id for json compatible object encoding
371
+ * - *:create_additions* [_Boolean_|_nil_] if true allow creation of instances using create_id on load.
372
+ * - *:second_precision* [_Fixnum_|_nil_] number of digits after the decimal when dumping the seconds portion of time.
373
+ * - *:float_precision* [_Fixnum_|_nil_] number of digits of precision when dumping floats, 0 indicates use Ruby.
374
+ * - *:use_to_json* [_Boolean_|_nil_] call to_json() methods on dump, default is false.
375
+ * - *:use_as_json* [_Boolean_|_nil_] call as_json() methods on dump, default is false.
376
+ * - *:use_to_hash* [_Boolean_|_nil_] call to_hash() methods on dump, default is false.
377
+ * - *:nilnil* [_Boolean_|_nil_] if true a nil input to load will return nil and not raise an Exception.
378
+ * - *:allow_gc* [_Boolean_|_nil_] allow or prohibit GC during parsing, default is true (allow).
379
+ * - *:quirks_mode* [_Boolean_|_nil_] allow single JSON values instead of documents, default is true (allow).
380
+ * - *:allow_invalid_unicode* [_Boolean_|_nil_] allow invalid unicode, default is false (don't allow).
381
+ * - *:allow_nan* [_Boolean_|_nil_] allow Nan, Infinity, and -Infinity, default is true (allow).
382
+ * - *:space* [_String_|_nil_] String to use for the space after the colon in JSON object fields.
383
+ * - *:space_before* [_String_|_nil_] String to use before the colon separator in JSON object fields.
384
+ * - *:object_nl* [_String_|_nil_] String to use after a JSON object field value.
385
+ * - *:array_nl* [_String_|_nil_] String to use after a JSON array value
386
+ * - *:nan* [_:null_|_:huge_|_:word_|_:raise_] how to dump Infinity and NaN in null, strict, and compat mode. :null places a null, :huge places a huge number, :word places Infinity or NaN, :raise raises and exception, :auto uses default for each mode.
387
+ * - *:hash_class* [_Class_|_nil_] Class to use instead of Hash on load, :object_class can also be used.
388
+ * - *:array_class* [_Class_|_nil_] Class to use instead of Array on load.
389
+ * - *:omit_nil* [_true_|_false_] if true Hash and Object attributes with nil values are omitted.
390
+ * - *:ignore* [_nil_|Array] either nil or an Array of classes to ignore when dumping
391
+ * - *:integer_range* [_Range_] Dump integers outside range as strings.
392
+ * - *:trace* [_Boolean_] turn trace on or off.
393
+ */
394
+ static VALUE
395
+ set_def_opts(VALUE self, VALUE opts) {
396
+ Check_Type(opts, T_HASH);
397
+ oj_parse_options(opts, &oj_default_options);
398
+
399
+ return Qnil;
400
+ }
401
+
402
+ void
403
+ oj_parse_options(VALUE ropts, Options copts) {
404
+ struct _yesNoOpt ynos[] = {
405
+ { circular_sym, &copts->circular },
406
+ { auto_define_sym, &copts->auto_define },
407
+ { symbol_keys_sym, &copts->sym_key },
408
+ { class_cache_sym, &copts->class_cache },
409
+ { bigdecimal_as_decimal_sym, &copts->bigdec_as_num },
410
+ { use_to_hash_sym, &copts->to_hash },
411
+ { use_to_json_sym, &copts->to_json },
412
+ { use_as_json_sym, &copts->as_json },
413
+ { nilnil_sym, &copts->nilnil },
414
+ { allow_blank_sym, &copts->nilnil }, // same as nilnil
415
+ { empty_string_sym, &copts->empty_string },
416
+ { allow_gc_sym, &copts->allow_gc },
417
+ { oj_quirks_mode_sym, &copts->quirks_mode },
418
+ { allow_invalid_unicode_sym, &copts->allow_invalid },
419
+ { oj_allow_nan_sym, &copts->allow_nan },
420
+ { oj_trace_sym, &copts->trace },
421
+ { oj_create_additions_sym, &copts->create_ok },
422
+ { Qnil, 0 }
423
+ };
424
+ YesNoOpt o;
425
+ volatile VALUE v;
426
+ size_t len;
427
+
428
+ if (T_HASH != rb_type(ropts)) {
429
+ return;
430
+ }
431
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_indent_sym)) {
432
+ v = rb_hash_lookup(ropts, oj_indent_sym);
433
+ switch (rb_type(v)) {
434
+ case T_NIL:
435
+ copts->dump_opts.indent_size = 0;
436
+ *copts->dump_opts.indent_str = '\0';
437
+ copts->indent = 0;
438
+ break;
439
+ case T_FIXNUM:
440
+ copts->dump_opts.indent_size = 0;
441
+ *copts->dump_opts.indent_str = '\0';
442
+ copts->indent = FIX2INT(v);
443
+ break;
444
+ case T_STRING:
445
+ if (sizeof(copts->dump_opts.indent_str) <= (len = RSTRING_LEN(v))) {
446
+ rb_raise(rb_eArgError, "indent string is limited to %lu characters.", (unsigned long)sizeof(copts->dump_opts.indent_str));
447
+ }
448
+ strcpy(copts->dump_opts.indent_str, StringValuePtr(v));
449
+ copts->dump_opts.indent_size = (uint8_t)len;
450
+ copts->indent = 0;
451
+ break;
452
+ default:
453
+ rb_raise(rb_eTypeError, "indent must be a Fixnum, String, or nil.");
454
+ break;
455
+ }
456
+ }
457
+ if (Qnil != (v = rb_hash_lookup(ropts, float_prec_sym))) {
458
+ int n;
459
+
460
+ #ifdef RUBY_INTEGER_UNIFICATION
461
+ if (rb_cInteger != rb_obj_class(v)) {
462
+ rb_raise(rb_eArgError, ":float_precision must be a Integer.");
463
+ }
464
+ #else
465
+ if (T_FIXNUM != rb_type(v)) {
466
+ rb_raise(rb_eArgError, ":float_precision must be a Fixnum.");
467
+ }
468
+ #endif
469
+ n = FIX2INT(v);
470
+ if (0 >= n) {
471
+ *copts->float_fmt = '\0';
472
+ copts->float_prec = 0;
473
+ } else {
474
+ if (20 < n) {
475
+ n = 20;
476
+ }
477
+ sprintf(copts->float_fmt, "%%0.%dg", n);
478
+ copts->float_prec = n;
479
+ }
480
+ }
481
+ if (Qnil != (v = rb_hash_lookup(ropts, sec_prec_sym))) {
482
+ int n;
483
+
484
+ #ifdef RUBY_INTEGER_UNIFICATION
485
+ if (rb_cInteger != rb_obj_class(v)) {
486
+ rb_raise(rb_eArgError, ":second_precision must be a Integer.");
487
+ }
488
+ #else
489
+ if (T_FIXNUM != rb_type(v)) {
490
+ rb_raise(rb_eArgError, ":second_precision must be a Fixnum.");
491
+ }
492
+ #endif
493
+ n = NUM2INT(v);
494
+ if (0 > n) {
495
+ n = 0;
496
+ } else if (9 < n) {
497
+ n = 9;
498
+ }
499
+ copts->sec_prec = n;
500
+ }
501
+ if (Qnil != (v = rb_hash_lookup(ropts, mode_sym))) {
502
+ if (wab_sym == v) {
503
+ copts->mode = WabMode;
504
+ } else if (object_sym == v) {
505
+ copts->mode = ObjectMode;
506
+ } else if (strict_sym == v) {
507
+ copts->mode = StrictMode;
508
+ } else if (compat_sym == v || json_sym == v) {
509
+ copts->mode = CompatMode;
510
+ } else if (null_sym == v) {
511
+ copts->mode = NullMode;
512
+ } else if (custom_sym == v) {
513
+ copts->mode = CustomMode;
514
+ } else if (rails_sym == v) {
515
+ copts->mode = RailsMode;
516
+ } else {
517
+ rb_raise(rb_eArgError, ":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
518
+ }
519
+ }
520
+ if (Qnil != (v = rb_hash_lookup(ropts, time_format_sym))) {
521
+ if (unix_sym == v) {
522
+ copts->time_format = UnixTime;
523
+ } else if (unix_zone_sym == v) {
524
+ copts->time_format = UnixZTime;
525
+ } else if (xmlschema_sym == v) {
526
+ copts->time_format = XmlTime;
527
+ } else if (ruby_sym == v) {
528
+ copts->time_format = RubyTime;
529
+ } else {
530
+ rb_raise(rb_eArgError, ":time_format must be :unix, :unix_zone, :xmlschema, or :ruby.");
531
+ }
532
+ }
533
+ if (Qnil != (v = rb_hash_lookup(ropts, escape_mode_sym))) {
534
+ if (newline_sym == v) {
535
+ copts->escape_mode = NLEsc;
536
+ } else if (json_sym == v) {
537
+ copts->escape_mode = JSONEsc;
538
+ } else if (xss_safe_sym == v) {
539
+ copts->escape_mode = XSSEsc;
540
+ } else if (ascii_sym == v) {
541
+ copts->escape_mode = ASCIIEsc;
542
+ } else if (unicode_xss_sym == v) {
543
+ copts->escape_mode = JXEsc;
544
+ } else {
545
+ rb_raise(rb_eArgError, ":encoding must be :newline, :json, :xss_safe, :unicode_xss, or :ascii.");
546
+ }
547
+ }
548
+ if (Qnil != (v = rb_hash_lookup(ropts, bigdecimal_load_sym))) {
549
+ if (bigdecimal_sym == v || Qtrue == v) {
550
+ copts->bigdec_load = BigDec;
551
+ } else if (float_sym == v) {
552
+ copts->bigdec_load = FloatDec;
553
+ } else if (auto_sym == v || Qfalse == v) {
554
+ copts->bigdec_load = AutoDec;
555
+ } else {
556
+ rb_raise(rb_eArgError, ":bigdecimal_load must be :bigdecimal, :float, or :auto.");
557
+ }
558
+ }
559
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, create_id_sym)) {
560
+ v = rb_hash_lookup(ropts, create_id_sym);
561
+ if (Qnil == v) {
562
+ if (oj_json_class != oj_default_options.create_id && NULL != copts->create_id) {
563
+ xfree((char*)oj_default_options.create_id);
564
+ }
565
+ copts->create_id = NULL;
566
+ copts->create_id_len = 0;
567
+ } else if (T_STRING == rb_type(v)) {
568
+ const char *str = StringValuePtr(v);
569
+
570
+ len = RSTRING_LEN(v);
571
+ if (len != copts->create_id_len ||
572
+ 0 != strcmp(copts->create_id, str)) {
573
+ copts->create_id = ALLOC_N(char, len + 1);
574
+ strcpy((char*)copts->create_id, str);
575
+ copts->create_id_len = len;
576
+ }
577
+ } else {
578
+ rb_raise(rb_eArgError, ":create_id must be string.");
579
+ }
580
+ }
581
+ for (o = ynos; 0 != o->attr; o++) {
582
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, o->sym)) {
583
+ v = rb_hash_lookup(ropts, o->sym);
584
+ if (Qnil == v) {
585
+ *o->attr = NotSet;
586
+ } else if (Qtrue == v) {
587
+ *o->attr = Yes;
588
+ } else if (Qfalse == v) {
589
+ *o->attr = No;
590
+ } else {
591
+ rb_raise(rb_eArgError, "%s must be true, false, or nil.", rb_id2name(SYM2ID(o->sym)));
592
+ }
593
+ }
594
+ }
595
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_space_sym)) {
596
+ if (Qnil == (v = rb_hash_lookup(ropts, oj_space_sym))) {
597
+ copts->dump_opts.after_size = 0;
598
+ *copts->dump_opts.after_sep = '\0';
599
+ } else {
600
+ rb_check_type(v, T_STRING);
601
+ if (sizeof(copts->dump_opts.after_sep) <= (len = RSTRING_LEN(v))) {
602
+ rb_raise(rb_eArgError, "space string is limited to %lu characters.", (unsigned long)sizeof(copts->dump_opts.after_sep));
603
+ }
604
+ strcpy(copts->dump_opts.after_sep, StringValuePtr(v));
605
+ copts->dump_opts.after_size = (uint8_t)len;
606
+ }
607
+ }
608
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_space_before_sym)) {
609
+ if (Qnil == (v = rb_hash_lookup(ropts, oj_space_before_sym))) {
610
+ copts->dump_opts.before_size = 0;
611
+ *copts->dump_opts.before_sep = '\0';
612
+ } else {
613
+ rb_check_type(v, T_STRING);
614
+ if (sizeof(copts->dump_opts.before_sep) <= (len = RSTRING_LEN(v))) {
615
+ rb_raise(rb_eArgError, "sapce_before string is limited to %lu characters.", (unsigned long)sizeof(copts->dump_opts.before_sep));
616
+ }
617
+ strcpy(copts->dump_opts.before_sep, StringValuePtr(v));
618
+ copts->dump_opts.before_size = (uint8_t)len;
619
+ }
620
+ }
621
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_object_nl_sym)) {
622
+ if (Qnil == (v = rb_hash_lookup(ropts, oj_object_nl_sym))) {
623
+ copts->dump_opts.hash_size = 0;
624
+ *copts->dump_opts.hash_nl = '\0';
625
+ } else {
626
+ rb_check_type(v, T_STRING);
627
+ if (sizeof(copts->dump_opts.hash_nl) <= (len = RSTRING_LEN(v))) {
628
+ rb_raise(rb_eArgError, "object_nl string is limited to %lu characters.", (unsigned long)sizeof(copts->dump_opts.hash_nl));
629
+ }
630
+ strcpy(copts->dump_opts.hash_nl, StringValuePtr(v));
631
+ copts->dump_opts.hash_size = (uint8_t)len;
632
+ }
633
+ }
634
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_array_nl_sym)) {
635
+ if (Qnil == (v = rb_hash_lookup(ropts, oj_array_nl_sym))) {
636
+ copts->dump_opts.array_size = 0;
637
+ *copts->dump_opts.array_nl = '\0';
638
+ } else {
639
+ rb_check_type(v, T_STRING);
640
+ if (sizeof(copts->dump_opts.array_nl) <= (len = RSTRING_LEN(v))) {
641
+ rb_raise(rb_eArgError, "array_nl string is limited to %lu characters.", (unsigned long)sizeof(copts->dump_opts.array_nl));
642
+ }
643
+ strcpy(copts->dump_opts.array_nl, StringValuePtr(v));
644
+ copts->dump_opts.array_size = (uint8_t)len;
645
+ }
646
+ }
647
+ if (Qnil != (v = rb_hash_lookup(ropts, nan_sym))) {
648
+ if (null_sym == v) {
649
+ copts->dump_opts.nan_dump = NullNan;
650
+ } else if (huge_sym == v) {
651
+ copts->dump_opts.nan_dump = HugeNan;
652
+ } else if (word_sym == v) {
653
+ copts->dump_opts.nan_dump = WordNan;
654
+ } else if (raise_sym == v) {
655
+ copts->dump_opts.nan_dump = RaiseNan;
656
+ } else if (auto_sym == v) {
657
+ copts->dump_opts.nan_dump = AutoNan;
658
+ } else {
659
+ rb_raise(rb_eArgError, ":nan must be :null, :huge, :word, :raise, or :auto.");
660
+ }
661
+ }
662
+ copts->dump_opts.use = (0 < copts->dump_opts.indent_size ||
663
+ 0 < copts->dump_opts.after_size ||
664
+ 0 < copts->dump_opts.before_size ||
665
+ 0 < copts->dump_opts.hash_size ||
666
+ 0 < copts->dump_opts.array_size);
667
+ if (Qnil != (v = rb_hash_lookup(ropts, omit_nil_sym))) {
668
+ if (Qtrue == v) {
669
+ copts->dump_opts.omit_nil = true;
670
+ } else if (Qfalse == v) {
671
+ copts->dump_opts.omit_nil = false;
672
+ } else {
673
+ rb_raise(rb_eArgError, ":omit_nil must be true or false.");
674
+ }
675
+ }
676
+ // This is here only for backwards compatibility with the original Oj.
677
+ v = rb_hash_lookup(ropts, oj_ascii_only_sym);
678
+ if (Qtrue == v) {
679
+ copts->escape_mode = ASCIIEsc;
680
+ } else if (Qfalse == v) {
681
+ copts->escape_mode = JSONEsc;
682
+ }
683
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_hash_class_sym)) {
684
+ if (Qnil == (v = rb_hash_lookup(ropts, oj_hash_class_sym))) {
685
+ copts->hash_class = Qnil;
686
+ } else {
687
+ rb_check_type(v, T_CLASS);
688
+ copts->hash_class = v;
689
+ }
690
+ }
691
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_object_class_sym)) {
692
+ if (Qnil == (v = rb_hash_lookup(ropts, oj_object_class_sym))) {
693
+ copts->hash_class = Qnil;
694
+ } else {
695
+ rb_check_type(v, T_CLASS);
696
+ copts->hash_class = v;
697
+ }
698
+ }
699
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_array_class_sym)) {
700
+ if (Qnil == (v = rb_hash_lookup(ropts, oj_array_class_sym))) {
701
+ copts->array_class = Qnil;
702
+ } else {
703
+ rb_check_type(v, T_CLASS);
704
+ copts->array_class = v;
705
+ }
706
+ }
707
+ oj_parse_opt_match_string(&copts->str_rx, ropts);
708
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, ignore_sym)) {
709
+ xfree(copts->ignore);
710
+ copts->ignore = NULL;
711
+ if (Qnil != (v = rb_hash_lookup(ropts, ignore_sym))) {
712
+ int cnt;
713
+
714
+ rb_check_type(v, T_ARRAY);
715
+ cnt = (int)RARRAY_LEN(v);
716
+ if (0 < cnt) {
717
+ int i;
718
+
719
+ copts->ignore = ALLOC_N(VALUE, cnt + 1);
720
+ for (i = 0; i < cnt; i++) {
721
+ copts->ignore[i] = rb_ary_entry(v, i);
722
+ }
723
+ copts->ignore[i] = Qnil;
724
+ }
725
+ }
726
+ }
727
+ if (Qnil != (v = rb_hash_lookup(ropts, integer_range_sym))) {
728
+ if (TYPE(v) == T_STRUCT && rb_obj_class(v) == rb_cRange) {
729
+ VALUE min = rb_funcall(v, oj_begin_id, 0);
730
+ VALUE max = rb_funcall(v, oj_end_id, 0);
731
+
732
+ if (TYPE(min) != T_FIXNUM || TYPE(max) != T_FIXNUM) {
733
+ rb_raise(rb_eArgError, ":integer_range range bounds is not Fixnum.");
734
+ }
735
+
736
+ copts->integer_range_min = FIX2LONG(min);
737
+ copts->integer_range_max = FIX2LONG(max);
738
+ } else if (Qfalse != v) {
739
+ rb_raise(rb_eArgError, ":integer_range must be a range of Fixnum.");
740
+ }
741
+ }
742
+ }
743
+
744
+ static int
745
+ match_string_cb(VALUE key, VALUE value, RxClass rc) {
746
+ if (T_CLASS != rb_type(value)) {
747
+ rb_raise(rb_eArgError, "for :match_string, the hash values must be a Class.");
748
+ }
749
+ switch (rb_type(key)) {
750
+ case T_REGEXP:
751
+ oj_rxclass_rappend(rc, key, value);
752
+ break;
753
+ case T_STRING:
754
+ if (0 != oj_rxclass_append(rc, StringValuePtr(key), value)) {
755
+ rb_raise(rb_eArgError, "%s", rc->err);
756
+ }
757
+ break;
758
+ default:
759
+ rb_raise(rb_eArgError, "for :match_string, keys must either a String or RegExp.");
760
+ break;
761
+ }
762
+ return ST_CONTINUE;
763
+ }
764
+
765
+ void
766
+ oj_parse_opt_match_string(RxClass rc, VALUE ropts) {
767
+ VALUE v;
768
+
769
+ if (Qnil != (v = rb_hash_lookup(ropts, match_string_sym))) {
770
+ rb_check_type(v, T_HASH);
771
+ // Zero out rc. Pattern are not appended but override.
772
+ rc->head = NULL;
773
+ rc->tail = NULL;
774
+ *rc->err = '\0';
775
+ rb_hash_foreach(v, match_string_cb, (VALUE)rc);
776
+ }
777
+ }
778
+
779
+ /* Document-method: load
780
+ * call-seq: load(json, options) { _|_obj, start, len_|_ }
781
+ *
782
+ * Parses a JSON document String into a Object, Hash, Array, String, Fixnum,
783
+ * Float, true, false, or nil according to the default mode or the mode
784
+ * specified. Raises an exception if the JSON is malformed or the classes
785
+ * specified are not valid. If the string input is not a valid JSON document (an
786
+ * empty string is not a valid JSON document) an exception is raised.
787
+ *
788
+ * When used with a document that has multiple JSON elements the block, if
789
+ * any, will be yielded to. If no block then the last element read will be
790
+ * returned.
791
+ *
792
+ * This parser operates on string and will attempt to load files into memory if
793
+ * a file object is passed as the first argument. A stream input will be parsed
794
+ * using a stream parser but others use the slightly faster string parser.
795
+ *
796
+ * A block can be provided with a single argument. That argument will be the
797
+ * parsed JSON document. This is useful when parsing a string that includes
798
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
799
+ * object, the position in the string or stream of the start of the JSON for
800
+ * that object, and the length of the JSON for that object plus trailing
801
+ * whitespace.
802
+ *
803
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read()
804
+ * - *options* [_Hash_] load options (same as default_options)
805
+ * - -
806
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
807
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
808
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
809
+ *
810
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
811
+ */
812
+ static VALUE
813
+ load(int argc, VALUE *argv, VALUE self) {
814
+ Mode mode = oj_default_options.mode;
815
+
816
+ if (1 > argc) {
817
+ rb_raise(rb_eArgError, "Wrong number of arguments to load().");
818
+ }
819
+ if (2 <= argc) {
820
+ VALUE ropts = argv[1];
821
+ VALUE v;
822
+
823
+ if (Qnil != ropts || CompatMode != mode) {
824
+ Check_Type(ropts, T_HASH);
825
+ if (Qnil != (v = rb_hash_lookup(ropts, mode_sym))) {
826
+ if (object_sym == v) {
827
+ mode = ObjectMode;
828
+ } else if (strict_sym == v) {
829
+ mode = StrictMode;
830
+ } else if (compat_sym == v || json_sym == v) {
831
+ mode = CompatMode;
832
+ } else if (null_sym == v) {
833
+ mode = NullMode;
834
+ } else if (custom_sym == v) {
835
+ mode = CustomMode;
836
+ } else if (rails_sym == v) {
837
+ mode = RailsMode;
838
+ } else if (wab_sym == v) {
839
+ mode = WabMode;
840
+ } else {
841
+ rb_raise(rb_eArgError, ":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
842
+ }
843
+ }
844
+ }
845
+ }
846
+ switch (mode) {
847
+ case StrictMode:
848
+ case NullMode:
849
+ return oj_strict_parse(argc, argv, self);
850
+ case CompatMode:
851
+ case RailsMode:
852
+ return oj_compat_parse(argc, argv, self);
853
+ case CustomMode:
854
+ return oj_custom_parse(argc, argv, self);
855
+ case WabMode:
856
+ return oj_wab_parse(argc, argv, self);
857
+ case ObjectMode:
858
+ default:
859
+ break;
860
+ }
861
+ return oj_object_parse(argc, argv, self);
862
+ }
863
+
864
+ /* Document-method: load_file
865
+ * call-seq: load_file(path, options) { _|_obj, start, len_|_ }
866
+ *
867
+ * Parses a JSON document String into a Object, Hash, Array, String, Fixnum,
868
+ * Float, true, false, or nil according to the default mode or the mode
869
+ * specified. Raises an exception if the JSON is malformed or the classes
870
+ * specified are not valid. If the string input is not a valid JSON document (an
871
+ * empty string is not a valid JSON document) an exception is raised.
872
+ *
873
+ * When used with a document that has multiple JSON elements the block, if
874
+ * any, will be yielded to. If no block then the last element read will be
875
+ * returned.
876
+ *
877
+ * If the input file is not a valid JSON document (an empty file is not a valid
878
+ * JSON document) an exception is raised.
879
+ *
880
+ * This is a stream based parser which allows a large or huge file to be loaded
881
+ * without pulling the whole file into memory.
882
+ *
883
+ * A block can be provided with a single argument. That argument will be the
884
+ * parsed JSON document. This is useful when parsing a string that includes
885
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
886
+ * object, the position in the string or stream of the start of the JSON for
887
+ * that object, and the length of the JSON for that object plus trailing
888
+ * whitespace.
889
+ *
890
+ * - *path* [_String_] to a file containing a JSON document
891
+ * - *options* [_Hash_] load options (same as default_options)
892
+ * - -
893
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
894
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
895
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
896
+ *
897
+ * Returns [_Object_|_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
898
+ */
899
+ static VALUE
900
+ load_file(int argc, VALUE *argv, VALUE self) {
901
+ char *path;
902
+ int fd;
903
+ Mode mode = oj_default_options.mode;
904
+ struct _parseInfo pi;
905
+
906
+ if (1 > argc) {
907
+ rb_raise(rb_eArgError, "Wrong number of arguments to load().");
908
+ }
909
+ Check_Type(*argv, T_STRING);
910
+ parse_info_init(&pi);
911
+ pi.options = oj_default_options;
912
+ pi.handler = Qnil;
913
+ pi.err_class = Qnil;
914
+ pi.max_depth = 0;
915
+ if (2 <= argc) {
916
+ VALUE ropts = argv[1];
917
+ VALUE v;
918
+
919
+ Check_Type(ropts, T_HASH);
920
+ if (Qnil != (v = rb_hash_lookup(ropts, mode_sym))) {
921
+ if (object_sym == v) {
922
+ mode = ObjectMode;
923
+ } else if (strict_sym == v) {
924
+ mode = StrictMode;
925
+ } else if (compat_sym == v || json_sym == v) {
926
+ mode = CompatMode;
927
+ } else if (null_sym == v) {
928
+ mode = NullMode;
929
+ } else if (custom_sym == v) {
930
+ mode = CustomMode;
931
+ } else if (rails_sym == v) {
932
+ mode = RailsMode;
933
+ } else if (wab_sym == v) {
934
+ mode = WabMode;
935
+ } else {
936
+ rb_raise(rb_eArgError, ":mode must be :object, :strict, :compat, :null, :custom, :rails, or :wab.");
937
+ }
938
+ }
939
+ }
940
+ path = StringValuePtr(*argv);
941
+ if (0 == (fd = open(path, O_RDONLY))) {
942
+ rb_raise(rb_eIOError, "%s", strerror(errno));
943
+ }
944
+ switch (mode) {
945
+ case StrictMode:
946
+ oj_set_strict_callbacks(&pi);
947
+ return oj_pi_sparse(argc, argv, &pi, fd);
948
+ case NullMode:
949
+ case CompatMode:
950
+ case CustomMode:
951
+ case RailsMode:
952
+ oj_set_compat_callbacks(&pi);
953
+ return oj_pi_sparse(argc, argv, &pi, fd);
954
+ case WabMode:
955
+ oj_set_wab_callbacks(&pi);
956
+ return oj_pi_sparse(argc, argv, &pi, fd);
957
+ case ObjectMode:
958
+ default:
959
+ break;
960
+ }
961
+ oj_set_object_callbacks(&pi);
962
+
963
+ return oj_pi_sparse(argc, argv, &pi, fd);
964
+ }
965
+
966
+ /* Document-method: safe_load
967
+ * call-seq: safe_load(doc)
968
+ *
969
+ * Loads a JSON document in strict mode with :auto_define and :symbol_keys
970
+ * turned off. This function should be safe to use with JSON received on an
971
+ * unprotected public interface.
972
+ *
973
+ * - *doc* [_String__|_IO_] JSON String or IO to load.
974
+ *
975
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Bignum_|_BigDecimal_|_nil_|_True_|_False_]
976
+ */
977
+ static VALUE
978
+ safe_load(VALUE self, VALUE doc) {
979
+ struct _parseInfo pi;
980
+ VALUE args[1];
981
+
982
+ parse_info_init(&pi);
983
+ pi.err_class = Qnil;
984
+ pi.max_depth = 0;
985
+ pi.options = oj_default_options;
986
+ pi.options.auto_define = No;
987
+ pi.options.sym_key = No;
988
+ pi.options.mode = StrictMode;
989
+ oj_set_strict_callbacks(&pi);
990
+ *args = doc;
991
+
992
+ return oj_pi_parse(1, args, &pi, 0, 0, 1);
993
+ }
994
+
995
+ /* Document-method: saj_parse
996
+ * call-seq: saj_parse(handler, io)
997
+ *
998
+ * Parses an IO stream or file containing a JSON document. Raises an exception
999
+ * if the JSON is malformed. This is a callback parser that calls the methods in
1000
+ * the handler if they exist. A sample is the Oj::Saj class which can be used as
1001
+ * a base class for the handler.
1002
+ *
1003
+ * - *handler* [_Oj::Saj_] responds to Oj::Saj methods
1004
+ * - *io* [_IO_|_String_] IO Object to read from
1005
+ */
1006
+
1007
+ /* Document-method: sc_parse
1008
+ * call-seq: sc_parse(handler, io)
1009
+ *
1010
+ * Parses an IO stream or file containing a JSON document. Raises an exception
1011
+ * if the JSON is malformed. This is a callback parser (Simple Callback Parser)
1012
+ * that calls the methods in the handler if they exist. A sample is the
1013
+ * Oj::ScHandler class which can be used as a base class for the handler. This
1014
+ * callback parser is slightly more efficient than the Saj callback parser and
1015
+ * requires less argument checking.
1016
+ *
1017
+ * - *handler* [_Oj_::ScHandler_] responds to Oj::ScHandler methods
1018
+ * - *io* [_IO__|_String_] IO Object to read from
1019
+ */
1020
+
1021
+ /* Document-method: dump
1022
+ * call-seq: dump(obj, options)
1023
+ *
1024
+ * Dumps an Object (obj) to a string.
1025
+ * - *obj* [_Object_] Object to serialize as an JSON document String
1026
+ * - *options* [_Hash_] same as default_options
1027
+ */
1028
+ static VALUE
1029
+ dump(int argc, VALUE *argv, VALUE self) {
1030
+ char buf[4096];
1031
+ struct _out out;
1032
+ struct _options copts = oj_default_options;
1033
+ VALUE rstr;
1034
+
1035
+ if (1 > argc) {
1036
+ rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
1037
+ }
1038
+ if (CompatMode == copts.mode) {
1039
+ copts.dump_opts.nan_dump = WordNan;
1040
+ }
1041
+ if (2 == argc) {
1042
+ oj_parse_options(argv[1], &copts);
1043
+ }
1044
+ out.buf = buf;
1045
+ out.end = buf + sizeof(buf) - 10;
1046
+ out.allocated = false;
1047
+ out.omit_nil = copts.dump_opts.omit_nil;
1048
+ out.caller = CALLER_DUMP;
1049
+ oj_dump_obj_to_json_using_params(*argv, &copts, &out, argc - 1,argv + 1);
1050
+ if (0 == out.buf) {
1051
+ rb_raise(rb_eNoMemError, "Not enough memory.");
1052
+ }
1053
+ rstr = rb_str_new2(out.buf);
1054
+ rstr = oj_encode(rstr);
1055
+ if (out.allocated) {
1056
+ xfree(out.buf);
1057
+ }
1058
+ return rstr;
1059
+ }
1060
+
1061
+ /* Document-method: to_json
1062
+ * call-seq: to_json(obj, options)
1063
+ *
1064
+ * Dumps an Object (obj) to a string. If the object has a to_json method that
1065
+ * will be called. The mode is set to :compat.
1066
+ * - *obj* [_Object_] Object to serialize as an JSON document String
1067
+ * - *options* [_Hash_]
1068
+ * - *:max_nesting* [_boolean_] It true nesting is limited to 100. The option to detect circular references is available but is not compatible with the json gem., default is false
1069
+ * - *:allow_nan* [_boolean_] If true non JSON compliant words such as Nan and Infinity will be used as appropriate, default is true.
1070
+ * - *:quirks_mode* [_boolean_] Allow single JSON values instead of documents, default is true (allow).
1071
+ * - *:indent* [_String_|_nil_] String to use for indentation, overriding the indent option if not nil.
1072
+ * - *:space* [_String_|_nil_] String to use for the space after the colon in JSON object fields.
1073
+ * - *:space_before* [_String_|_nil_] String to use before the colon separator in JSON object fields.
1074
+ * - *:object_nl* [_String_|_nil_] String to use after a JSON object field value.
1075
+ * - *:array_nl* [_String_|_nil_] String to use after a JSON array value.
1076
+ * - *:trace* [_Boolean_] If true trace is turned on.
1077
+ *
1078
+ * Returns [_String_] the encoded JSON.
1079
+ */
1080
+ static VALUE
1081
+ to_json(int argc, VALUE *argv, VALUE self) {
1082
+ char buf[4096];
1083
+ struct _out out;
1084
+ struct _options copts = oj_default_options;
1085
+ VALUE rstr;
1086
+
1087
+ if (1 > argc) {
1088
+ rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
1089
+ }
1090
+ copts.escape_mode = JXEsc;
1091
+ copts.dump_opts.nan_dump = RaiseNan;
1092
+ if (2 == argc) {
1093
+ oj_parse_mimic_dump_options(argv[1], &copts);
1094
+ }
1095
+ copts.mode = CompatMode;
1096
+ copts.to_json = Yes;
1097
+ out.buf = buf;
1098
+ out.end = buf + sizeof(buf) - 10;
1099
+ out.allocated = false;
1100
+ out.omit_nil = copts.dump_opts.omit_nil;
1101
+ // For obj.to_json or generate nan is not allowed but if called from dump
1102
+ // it is.
1103
+ oj_dump_obj_to_json_using_params(*argv, &copts, &out, argc - 1, argv + 1);
1104
+
1105
+ if (0 == out.buf) {
1106
+ rb_raise(rb_eNoMemError, "Not enough memory.");
1107
+ }
1108
+ rstr = rb_str_new2(out.buf);
1109
+ rstr = oj_encode(rstr);
1110
+ if (out.allocated) {
1111
+ xfree(out.buf);
1112
+ }
1113
+ return rstr;
1114
+ }
1115
+
1116
+ /* Document-method: to_file
1117
+ * call-seq: to_file(file_path, obj, options)
1118
+ *
1119
+ * Dumps an Object to the specified file.
1120
+ * - *file* [_String_] _path file path to write the JSON document to
1121
+ * - *obj* [_Object_] Object to serialize as an JSON document String
1122
+ * - *options* [_Hash_] formating options
1123
+ * - *:indent* [_Fixnum_] format expected
1124
+ * - *:circular* [_Boolean_] allow circular references, default: false
1125
+ */
1126
+ static VALUE
1127
+ to_file(int argc, VALUE *argv, VALUE self) {
1128
+ struct _options copts = oj_default_options;
1129
+
1130
+ if (3 == argc) {
1131
+ oj_parse_options(argv[2], &copts);
1132
+ }
1133
+ Check_Type(*argv, T_STRING);
1134
+ oj_write_obj_to_file(argv[1], StringValuePtr(*argv), &copts);
1135
+
1136
+ return Qnil;
1137
+ }
1138
+
1139
+ /* Document-method: to_stream
1140
+ * call-seq: to_stream(io, obj, options)
1141
+ *
1142
+ * Dumps an Object to the specified IO stream.
1143
+ * - *io* [_IO_] IO stream to write the JSON document to
1144
+ * - *obj* [_Object_] Object to serialize as an JSON document String
1145
+ * - *options* [_Hash_] formating options
1146
+ * - *:indent* [_Fixnum_] format expected
1147
+ * - *:circular* [_Boolean_] allow circular references, default: false
1148
+ */
1149
+ static VALUE
1150
+ to_stream(int argc, VALUE *argv, VALUE self) {
1151
+ struct _options copts = oj_default_options;
1152
+
1153
+ if (3 == argc) {
1154
+ oj_parse_options(argv[2], &copts);
1155
+ }
1156
+ oj_write_obj_to_stream(argv[1], *argv, &copts);
1157
+
1158
+ return Qnil;
1159
+ }
1160
+
1161
+ /* Document-method: register_odd
1162
+ * call-seq: register_odd(clas, create_object, create_method, *members)
1163
+ *
1164
+ * Registers a class as special. This is useful for working around subclasses of
1165
+ * primitive types as is done with ActiveSupport classes. The use of this
1166
+ * function should be limited to just classes that can not be handled in the
1167
+ * normal way. It is not intended as a hook for changing the output of all
1168
+ * classes as it is not optimized for large numbers of classes.
1169
+ *
1170
+ * - *clas* [_Class__|_Module_] Class or Module to be made special
1171
+ * - *create_object* [_Object_] object to call the create method on
1172
+ * - *create_method* [_Symbol_] method on the clas that will create a new instance of the clas when given all the member values in the order specified.
1173
+ * - *members* [_Symbol__|_String_] methods used to get the member values from instances of the clas.
1174
+ */
1175
+ static VALUE
1176
+ register_odd(int argc, VALUE *argv, VALUE self) {
1177
+ if (3 > argc) {
1178
+ rb_raise(rb_eArgError, "incorrect number of arguments.");
1179
+ }
1180
+ switch (rb_type(*argv)) {
1181
+ case T_CLASS:
1182
+ case T_MODULE:
1183
+ break;
1184
+ default:
1185
+ rb_raise(rb_eTypeError, "expected a class or module.");
1186
+ break;
1187
+ }
1188
+ Check_Type(argv[2], T_SYMBOL);
1189
+ if (MAX_ODD_ARGS < argc - 2) {
1190
+ rb_raise(rb_eArgError, "too many members.");
1191
+ }
1192
+ oj_reg_odd(argv[0], argv[1], argv[2], argc - 3, argv + 3, false);
1193
+
1194
+ return Qnil;
1195
+ }
1196
+
1197
+ /* Document-method: register_odd_raw
1198
+ * call-seq: register_odd_raw(clas, create_object, create_method, dump_method)
1199
+ *
1200
+ * Registers a class as special and expect the output to be a string that can be
1201
+ * included in the dumped JSON directly. This is useful for working around
1202
+ * subclasses of primitive types as is done with ActiveSupport classes. The use
1203
+ * of this function should be limited to just classes that can not be handled in
1204
+ * the normal way. It is not intended as a hook for changing the output of all
1205
+ * classes as it is not optimized for large numbers of classes. Be careful with
1206
+ * this option as the JSON may be incorrect if invalid JSON is returned.
1207
+ *
1208
+ * - *clas* [_Class_|_Module_] Class or Module to be made special
1209
+ * - *create_object* [_Object_] object to call the create method on
1210
+ * - *create_method* [_Symbol_] method on the clas that will create a new instance of the clas when given all the member values in the order specified.
1211
+ * - *dump_method* [_Symbol_|_String_] method to call on the object being serialized to generate the raw JSON.
1212
+ */
1213
+ static VALUE
1214
+ register_odd_raw(int argc, VALUE *argv, VALUE self) {
1215
+ if (3 > argc) {
1216
+ rb_raise(rb_eArgError, "incorrect number of arguments.");
1217
+ }
1218
+ switch (rb_type(*argv)) {
1219
+ case T_CLASS:
1220
+ case T_MODULE:
1221
+ break;
1222
+ default:
1223
+ rb_raise(rb_eTypeError, "expected a class or module.");
1224
+ break;
1225
+ }
1226
+ Check_Type(argv[2], T_SYMBOL);
1227
+ if (MAX_ODD_ARGS < argc - 2) {
1228
+ rb_raise(rb_eArgError, "too many members.");
1229
+ }
1230
+ oj_reg_odd(argv[0], argv[1], argv[2], 1, argv + 3, true);
1231
+
1232
+ return Qnil;
1233
+ }
1234
+
1235
+ ////////////////////////////////////////////////////////////////////////////////
1236
+ // RDoc entries must be in the same file as the rb_define_method and must be
1237
+ // directly above the C method function. The extern declaration is enough to
1238
+ // get it to work.
1239
+ ////////////////////////////////////////////////////////////////////////////////
1240
+
1241
+ /* Document-method: strict_load
1242
+ * call-seq: strict_load(json, options) { _|_obj, start, len_|_ }
1243
+ *
1244
+ * Parses a JSON document String into an Hash, Array, String, Fixnum, Float,
1245
+ * true, false, or nil. It parses using a mode that is strict in that it maps
1246
+ * each primitive JSON type to a similar Ruby type. The :create_id is not
1247
+ * honored in this mode. Note that a Ruby Hash is used to represent the JSON
1248
+ * Object type. These two are not the same since the JSON Object type can have
1249
+ * repeating entries with the same key and Ruby Hash can not.
1250
+ *
1251
+ * When used with a document that has multiple JSON elements the block, if
1252
+ * any, will be yielded to. If no block then the last element read will be
1253
+ * returned.
1254
+ *
1255
+ * Raises an exception if the JSON is malformed or the classes specified are not
1256
+ * valid. If the input is not a valid JSON document (an empty string is not a
1257
+ * valid JSON document) an exception is raised.
1258
+ *
1259
+ * A block can be provided with a single argument. That argument will be the
1260
+ * parsed JSON document. This is useful when parsing a string that includes
1261
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1262
+ * object, the position in the string or stream of the start of the JSON for
1263
+ * that object, and the length of the JSON for that object plus trailing
1264
+ * whitespace.
1265
+ *
1266
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read().
1267
+ * - *options* [_Hash_] load options (same as default_options).
1268
+ * - -
1269
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1270
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1271
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1272
+ *
1273
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1274
+ */
1275
+ extern VALUE oj_strict_parse(int argc, VALUE *argv, VALUE self);
1276
+
1277
+ /* Document-method: compat_load
1278
+ * call-seq: compat_load(json, options) { _|_obj, start, len_|_ }
1279
+ *
1280
+ * Parses a JSON document String into an Object, Hash, Array, String, Fixnum,
1281
+ * Float, true, false, or nil. It parses using a mode that is generally
1282
+ * compatible with other Ruby JSON parsers in that it will create objects based
1283
+ * on the :create_id value. It is not compatible in every way to every other
1284
+ * parser though as each parser has it's own variations.
1285
+ *
1286
+ * When used with a document that has multiple JSON elements the block, if
1287
+ * any, will be yielded to. If no block then the last element read will be
1288
+ * returned.
1289
+ *
1290
+ * Raises an exception if the JSON is malformed or the classes specified are not
1291
+ * valid. If the input is not a valid JSON document (an empty string is not a
1292
+ * valid JSON document) an exception is raised.
1293
+ *
1294
+ * A block can be provided with a single argument. That argument will be the
1295
+ * parsed JSON document. This is useful when parsing a string that includes
1296
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1297
+ * object, the position in the string or stream of the start of the JSON for
1298
+ * that object, and the length of the JSON for that object plus trailing
1299
+ * whitespace.
1300
+ *
1301
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read().
1302
+ * - *options* [_Hash_] load options (same as default_options).
1303
+ * - -
1304
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1305
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1306
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1307
+ *
1308
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1309
+ */
1310
+ extern VALUE oj_compat_parse(int argc, VALUE *argv, VALUE self);
1311
+
1312
+ /* Document-method: object_load
1313
+ * call-seq: object_load(json, options) { _|_obj, start, len_|_ }
1314
+ *
1315
+ * Parses a JSON document String into an Object, Hash, Array, String, Fixnum,
1316
+ * Float, true, false, or nil. In the :object mode the JSON should have been
1317
+ * generated by Oj.dump(). The parser will reconstitute the original marshalled
1318
+ * or dumped Object. The :auto_define and :circular options have meaning with
1319
+ * this parsing mode.
1320
+ *
1321
+ * Raises an exception if the JSON is malformed or the classes specified are not
1322
+ * valid. If the input is not a valid JSON document (an empty string is not a
1323
+ * valid JSON document) an exception is raised.
1324
+ *
1325
+ * A block can be provided with a single argument. That argument will be the
1326
+ * parsed JSON document. This is useful when parsing a string that includes
1327
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1328
+ * object, the position in the string or stream of the start of the JSON for
1329
+ * that object, and the length of the JSON for that object plus trailing
1330
+ * whitespace.
1331
+ *
1332
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read().
1333
+ * - *options* [_Hash_] load options (same as default_options).
1334
+ * - -
1335
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1336
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1337
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1338
+ *
1339
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1340
+ */
1341
+ extern VALUE oj_object_parse(int argc, VALUE *argv, VALUE self);
1342
+
1343
+ /* Document-method: wab_load
1344
+ * call-seq: wab_load(json, options) { _|_obj, start, len_|_ }
1345
+ *
1346
+ * Parses a JSON document String into an Hash, Array, String, Fixnum, Float,
1347
+ * true, false, or nil. It parses using a mode that is :wab in that it maps
1348
+ * each primitive JSON type to a similar Ruby type. The :create_id is not
1349
+ * honored in this mode. Note that a Ruby Hash is used to represent the JSON
1350
+ * Object type. These two are not the same since the JSON Object type can have
1351
+ * repeating entries with the same key and Ruby Hash can not.
1352
+ *
1353
+ * When used with a document that has multiple JSON elements the block, if
1354
+ * any, will be yielded to. If no block then the last element read will be
1355
+ * returned.
1356
+ *
1357
+ * Raises an exception if the JSON is malformed or the classes specified are not
1358
+ * valid. If the input is not a valid JSON document (an empty string is not a
1359
+ * valid JSON document) an exception is raised.
1360
+ *
1361
+ * A block can be provided with a single argument. That argument will be the
1362
+ * parsed JSON document. This is useful when parsing a string that includes
1363
+ * multiple JSON documents. The block can take up to 3 arguments, the parsed
1364
+ * object, the position in the string or stream of the start of the JSON for
1365
+ * that object, and the length of the JSON for that object plus trailing
1366
+ * whitespace.
1367
+ *
1368
+ * - *json* [_String_|_IO_] JSON String or an Object that responds to read().
1369
+ * - *options* [_Hash_] load options (same as default_options).
1370
+ * - -
1371
+ * - *obj* [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_] parsed object.
1372
+ * - *start* [_optional, _Integer_] start position of parsed JSON for obj.
1373
+ * - *len* [_optional, _Integer_] length of parsed JSON for obj.
1374
+ *
1375
+ * Returns [_Hash_|_Array_|_String_|_Fixnum_|_Float_|_Boolean_|_nil_]
1376
+ */
1377
+ extern VALUE oj_wab_parse(int argc, VALUE *argv, VALUE self);
1378
+
1379
+ /* Document-method: add_to_json
1380
+ * call-seq: add_to_json(*args)
1381
+ *
1382
+ * Override simple to_s dump behavior in :compat mode to instead use an
1383
+ * optimized dump that includes the classname and attributes so that the
1384
+ * object can be re-created on load. The format is the same as the json gem
1385
+ * but does not use the ruby methods for encoding.
1386
+ *
1387
+ * The classes supported for optimization are: Array, BigDecimal, Complex,
1388
+ * Date, DateTime, Exception, Hash, Integer, OpenStruct, Range, Rational,
1389
+ * Regexp, Struct, and Time. Providing no classes will result in all those
1390
+ * classes being optimized.q
1391
+ *
1392
+ * - *args( [_Class_] zero or more classes to optimize.
1393
+ */
1394
+ extern VALUE oj_add_to_json(int argc, VALUE *argv, VALUE self);
1395
+
1396
+ /* @!method remove_to_json(*args)
1397
+ *
1398
+ * Reverts back to the to_s dump behavior in :compat mode to instead use an
1399
+ * optimized dump that includes the classname and attributes so that the
1400
+ * object can be re-created on load. The format is the same as the json gem
1401
+ * but does not use the ruby methods for encoding.
1402
+ *
1403
+ * The classes supported for optimization are: Array, BigDecimal, Complex,
1404
+ * Date, DateTime, Exception, Hash, Integer, OpenStruct, Range, Rational,
1405
+ * Regexp, Struct, and Time. Providing no classes will result in all those
1406
+ * classes being reverted from the optimized mode.
1407
+ *
1408
+ * - *args* [_Class_] zero or more classes to optimize.
1409
+ */
1410
+ extern VALUE oj_remove_to_json(int argc, VALUE *argv, VALUE self);
1411
+
1412
+ /* Document-method: mimic_JSON
1413
+ * call-seq: mimic_JSON()
1414
+ *
1415
+ * Creates the JSON module with methods and classes to mimic the JSON gem. After
1416
+ * this method is invoked calls that expect the JSON module will use Oj instead
1417
+ * and be faster than the original JSON. Most options that could be passed to
1418
+ * the JSON methods are supported. The calls to set parser or generator will not
1419
+ * raise an Exception but will not have any effect. The method can also be
1420
+ * called after the json gem is loaded. The necessary methods on the json gem
1421
+ * will be replaced with Oj methods.
1422
+ *
1423
+ * Note that this also sets the default options of :mode to :compat and
1424
+ * :encoding to :unicode_xss.
1425
+ *
1426
+ * Returns [_Module_] the JSON module.
1427
+ */
1428
+ extern VALUE oj_define_mimic_json(int argc, VALUE *argv, VALUE self);
1429
+
1430
+ /* Document-method: generate
1431
+ * call-seq: generate(obj, opts=nil)
1432
+ *
1433
+ * Encode obj as a JSON String. The obj argument must be a Hash, Array, or
1434
+ * respond to to_h or to_json. Options other than those listed such as
1435
+ * +:allow_nan+ or +:max_nesting+ are ignored.
1436
+ *
1437
+ * - *obj* [_Object__|_Hash_|_Array_] object to convert to a JSON String
1438
+ * - *opts* [_Hash_] options
1439
+ * - - *:indent* [_String_] String to use for indentation.
1440
+ * - *:space* [_String_] String placed after a , or : delimiter
1441
+ * - *:space * _before [_String_] String placed before a : delimiter
1442
+ * - *:object_nl* [_String_] String placed after a JSON object
1443
+ * - *:array_nl* [_String_] String placed after a JSON array
1444
+ * - *:ascii_only* [_Boolean_] if not nil or false then use only ascii characters in the output. Note JSON.generate does support this even if it is not documented.
1445
+ *
1446
+ * Returns [_String_]generated JSON.
1447
+ */
1448
+ extern VALUE oj_mimic_generate(int argc, VALUE *argv, VALUE self);
1449
+
1450
+ /* Document-module: Oj.optimize_rails()
1451
+ *
1452
+ * Sets the Oj as the Rails encoder and decoder. Oj::Rails.optimize is also
1453
+ * called.
1454
+ */
1455
+ extern VALUE oj_optimize_rails(VALUE self);
1456
+
1457
+ /*
1458
+ extern void oj_hash_test();
1459
+
1460
+ static VALUE
1461
+ hash_test(VALUE self) {
1462
+ oj_hash_test();
1463
+ return Qnil;
1464
+ }
1465
+ */
1466
+
1467
+ static VALUE
1468
+ protect_require(VALUE x) {
1469
+ rb_require("time");
1470
+ rb_require("bigdecimal");
1471
+ return Qnil;
1472
+ }
1473
+
1474
+ /* Document-module: Oj
1475
+ *
1476
+ * Optimized JSON (Oj), as the name implies was written to provide speed
1477
+ * optimized JSON handling.
1478
+ *
1479
+ * Oj uses modes to control how object are encoded and decoded. In addition
1480
+ * global and options to methods allow additional behavior modifications. The
1481
+ * modes are:
1482
+ *
1483
+ * - *:strict* mode will only allow the 7 basic JSON types to be serialized. Any other Object
1484
+ * will raise an Exception.
1485
+ *
1486
+ * - *:null* mode is similar to the :strict mode except any Object that is not
1487
+ * one of the JSON base types is replaced by a JSON null.
1488
+ *
1489
+ * - *:object* mode will dump any Object as a JSON Object with keys that match
1490
+ * the Ruby Object's variable names without the '@' character. This is the
1491
+ * highest performance mode.
1492
+ *
1493
+ * - *:compat* or *:json* mode is the compatible mode for the json gem. It mimics
1494
+ * the json gem including the options, defaults, and restrictions.
1495
+ *
1496
+ * - *:rails* is the compatibility mode for Rails or Active support.
1497
+ *
1498
+ * - *:custom* is the most configurable mode.
1499
+ *
1500
+ * - *:wab* specifically for WAB data exchange.
1501
+ */
1502
+ void
1503
+ Init_oj() {
1504
+ int err = 0;
1505
+
1506
+ Oj = rb_define_module("Oj");
1507
+
1508
+ oj_cstack_class = rb_define_class_under(Oj, "CStack", rb_cObject);
1509
+
1510
+ oj_string_writer_init();
1511
+ oj_stream_writer_init();
1512
+
1513
+ rb_require("date");
1514
+ // On Rubinius the require fails but can be done from a ruby file.
1515
+ rb_protect(protect_require, Qnil, &err);
1516
+ rb_require("stringio");
1517
+ oj_utf8_encoding = rb_enc_find("UTF-8");
1518
+
1519
+ //rb_define_module_function(Oj, "hash_test", hash_test, 0);
1520
+
1521
+ rb_define_module_function(Oj, "default_options", get_def_opts, 0);
1522
+ rb_define_module_function(Oj, "default_options=", set_def_opts, 1);
1523
+
1524
+ rb_define_module_function(Oj, "mimic_JSON", oj_define_mimic_json, -1);
1525
+ rb_define_module_function(Oj, "load", load, -1);
1526
+ rb_define_module_function(Oj, "load_file", load_file, -1);
1527
+ rb_define_module_function(Oj, "safe_load", safe_load, 1);
1528
+ rb_define_module_function(Oj, "strict_load", oj_strict_parse, -1);
1529
+ rb_define_module_function(Oj, "compat_load", oj_compat_parse, -1);
1530
+ rb_define_module_function(Oj, "object_load", oj_object_parse, -1);
1531
+ rb_define_module_function(Oj, "wab_load", oj_wab_parse, -1);
1532
+
1533
+ rb_define_module_function(Oj, "dump", dump, -1);
1534
+
1535
+ rb_define_module_function(Oj, "to_file", to_file, -1);
1536
+ rb_define_module_function(Oj, "to_stream", to_stream, -1);
1537
+ // JSON gem compatibility
1538
+ rb_define_module_function(Oj, "to_json", to_json, -1);
1539
+ rb_define_module_function(Oj, "generate", oj_mimic_generate, -1);
1540
+ rb_define_module_function(Oj, "fast_generate", oj_mimic_generate, -1);
1541
+
1542
+ rb_define_module_function(Oj, "add_to_json", oj_add_to_json, -1);
1543
+ rb_define_module_function(Oj, "remove_to_json", oj_remove_to_json, -1);
1544
+
1545
+ rb_define_module_function(Oj, "register_odd", register_odd, -1);
1546
+ rb_define_module_function(Oj, "register_odd_raw", register_odd_raw, -1);
1547
+
1548
+ rb_define_module_function(Oj, "saj_parse", oj_saj_parse, -1);
1549
+ rb_define_module_function(Oj, "sc_parse", oj_sc_parse, -1);
1550
+
1551
+ rb_define_module_function(Oj, "optimize_rails", oj_optimize_rails, 0);
1552
+
1553
+ oj_add_value_id = rb_intern("add_value");
1554
+ oj_array_append_id = rb_intern("array_append");
1555
+ oj_array_end_id = rb_intern("array_end");
1556
+ oj_array_start_id = rb_intern("array_start");
1557
+ oj_as_json_id = rb_intern("as_json");
1558
+ oj_begin_id = rb_intern("begin");
1559
+ oj_bigdecimal_id = rb_intern("BigDecimal");
1560
+ oj_end_id = rb_intern("end");
1561
+ oj_error_id = rb_intern("error");
1562
+ oj_exclude_end_id = rb_intern("exclude_end?");
1563
+ oj_file_id = rb_intern("file?");
1564
+ oj_fileno_id = rb_intern("fileno");
1565
+ oj_ftype_id = rb_intern("ftype");
1566
+ oj_has_key_id = rb_intern("has_key?");
1567
+ oj_hash_end_id = rb_intern("hash_end");
1568
+ oj_hash_key_id = rb_intern("hash_key");
1569
+ oj_hash_set_id = rb_intern("hash_set");
1570
+ oj_hash_start_id = rb_intern("hash_start");
1571
+ oj_iconv_id = rb_intern("iconv");
1572
+ oj_instance_variables_id = rb_intern("instance_variables");
1573
+ oj_json_create_id = rb_intern("json_create");
1574
+ oj_length_id = rb_intern("length");
1575
+ oj_new_id = rb_intern("new");
1576
+ oj_parse_id = rb_intern("parse");
1577
+ oj_pos_id = rb_intern("pos");
1578
+ oj_read_id = rb_intern("read");
1579
+ oj_readpartial_id = rb_intern("readpartial");
1580
+ oj_replace_id = rb_intern("replace");
1581
+ oj_stat_id = rb_intern("stat");
1582
+ oj_string_id = rb_intern("string");
1583
+ oj_to_h_id = rb_intern("to_h");
1584
+ oj_to_hash_id = rb_intern("to_hash");
1585
+ oj_to_json_id = rb_intern("to_json");
1586
+ oj_to_s_id = rb_intern("to_s");
1587
+ oj_to_sym_id = rb_intern("to_sym");
1588
+ oj_to_time_id = rb_intern("to_time");
1589
+ oj_tv_nsec_id = rb_intern("tv_nsec");
1590
+ oj_tv_sec_id = rb_intern("tv_sec");
1591
+ oj_tv_usec_id = rb_intern("tv_usec");
1592
+ oj_utc_id = rb_intern("utc");
1593
+ oj_utc_offset_id = rb_intern("utc_offset");
1594
+ oj_utcq_id = rb_intern("utc?");
1595
+ oj_write_id = rb_intern("write");
1596
+
1597
+ rb_require("oj/bag");
1598
+ rb_require("oj/error");
1599
+ rb_require("oj/mimic");
1600
+ rb_require("oj/saj");
1601
+ rb_require("oj/schandler");
1602
+
1603
+ oj_bag_class = rb_const_get_at(Oj, rb_intern("Bag"));
1604
+ oj_bigdecimal_class = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
1605
+ oj_date_class = rb_const_get(rb_cObject, rb_intern("Date"));
1606
+ oj_datetime_class = rb_const_get(rb_cObject, rb_intern("DateTime"));
1607
+ oj_enumerable_class = rb_const_get(rb_cObject, rb_intern("Enumerable"));
1608
+ oj_parse_error_class = rb_const_get_at(Oj, rb_intern("ParseError"));
1609
+ oj_stringio_class = rb_const_get(rb_cObject, rb_intern("StringIO"));
1610
+ oj_struct_class = rb_const_get(rb_cObject, rb_intern("Struct"));
1611
+ oj_json_parser_error_class = rb_eEncodingError; // replaced if mimic is called
1612
+ oj_json_generator_error_class = rb_eEncodingError; // replaced if mimic is called
1613
+
1614
+ allow_blank_sym = ID2SYM(rb_intern("allow_blank")); rb_gc_register_address(&allow_blank_sym);
1615
+ allow_gc_sym = ID2SYM(rb_intern("allow_gc")); rb_gc_register_address(&allow_gc_sym);
1616
+ allow_invalid_unicode_sym = ID2SYM(rb_intern("allow_invalid_unicode"));rb_gc_register_address(&allow_invalid_unicode_sym);
1617
+ ascii_sym = ID2SYM(rb_intern("ascii")); rb_gc_register_address(&ascii_sym);
1618
+ auto_define_sym = ID2SYM(rb_intern("auto_define")); rb_gc_register_address(&auto_define_sym);
1619
+ auto_sym = ID2SYM(rb_intern("auto")); rb_gc_register_address(&auto_sym);
1620
+ bigdecimal_as_decimal_sym = ID2SYM(rb_intern("bigdecimal_as_decimal"));rb_gc_register_address(&bigdecimal_as_decimal_sym);
1621
+ bigdecimal_load_sym = ID2SYM(rb_intern("bigdecimal_load")); rb_gc_register_address(&bigdecimal_load_sym);
1622
+ bigdecimal_sym = ID2SYM(rb_intern("bigdecimal")); rb_gc_register_address(&bigdecimal_sym);
1623
+ circular_sym = ID2SYM(rb_intern("circular")); rb_gc_register_address(&circular_sym);
1624
+ class_cache_sym = ID2SYM(rb_intern("class_cache")); rb_gc_register_address(&class_cache_sym);
1625
+ compat_sym = ID2SYM(rb_intern("compat")); rb_gc_register_address(&compat_sym);
1626
+ create_id_sym = ID2SYM(rb_intern("create_id")); rb_gc_register_address(&create_id_sym);
1627
+ custom_sym = ID2SYM(rb_intern("custom")); rb_gc_register_address(&custom_sym);
1628
+ empty_string_sym = ID2SYM(rb_intern("empty_string")); rb_gc_register_address(&empty_string_sym);
1629
+ escape_mode_sym = ID2SYM(rb_intern("escape_mode")); rb_gc_register_address(&escape_mode_sym);
1630
+ integer_range_sym = ID2SYM(rb_intern("integer_range")); rb_gc_register_address(&integer_range_sym);
1631
+ float_prec_sym = ID2SYM(rb_intern("float_precision")); rb_gc_register_address(&float_prec_sym);
1632
+ float_sym = ID2SYM(rb_intern("float")); rb_gc_register_address(&float_sym);
1633
+ huge_sym = ID2SYM(rb_intern("huge")); rb_gc_register_address(&huge_sym);
1634
+ ignore_sym = ID2SYM(rb_intern("ignore")); rb_gc_register_address(&ignore_sym);
1635
+ json_sym = ID2SYM(rb_intern("json")); rb_gc_register_address(&json_sym);
1636
+ match_string_sym = ID2SYM(rb_intern("match_string")); rb_gc_register_address(&match_string_sym);
1637
+ mode_sym = ID2SYM(rb_intern("mode")); rb_gc_register_address(&mode_sym);
1638
+ nan_sym = ID2SYM(rb_intern("nan")); rb_gc_register_address(&nan_sym);
1639
+ newline_sym = ID2SYM(rb_intern("newline")); rb_gc_register_address(&newline_sym);
1640
+ nilnil_sym = ID2SYM(rb_intern("nilnil")); rb_gc_register_address(&nilnil_sym);
1641
+ null_sym = ID2SYM(rb_intern("null")); rb_gc_register_address(&null_sym);
1642
+ object_sym = ID2SYM(rb_intern("object")); rb_gc_register_address(&object_sym);
1643
+ oj_allow_nan_sym = ID2SYM(rb_intern("allow_nan")); rb_gc_register_address(&oj_allow_nan_sym);
1644
+ oj_array_class_sym = ID2SYM(rb_intern("array_class")); rb_gc_register_address(&oj_array_class_sym);
1645
+ oj_array_nl_sym = ID2SYM(rb_intern("array_nl")); rb_gc_register_address(&oj_array_nl_sym);
1646
+ oj_ascii_only_sym = ID2SYM(rb_intern("ascii_only")); rb_gc_register_address(&oj_ascii_only_sym);
1647
+ oj_create_additions_sym = ID2SYM(rb_intern("create_additions"));rb_gc_register_address(&oj_create_additions_sym);
1648
+ oj_hash_class_sym = ID2SYM(rb_intern("hash_class")); rb_gc_register_address(&oj_hash_class_sym);
1649
+ oj_indent_sym = ID2SYM(rb_intern("indent")); rb_gc_register_address(&oj_indent_sym);
1650
+ oj_max_nesting_sym = ID2SYM(rb_intern("max_nesting")); rb_gc_register_address(&oj_max_nesting_sym);
1651
+ oj_object_class_sym = ID2SYM(rb_intern("object_class")); rb_gc_register_address(&oj_object_class_sym);
1652
+ oj_object_nl_sym = ID2SYM(rb_intern("object_nl")); rb_gc_register_address(&oj_object_nl_sym);
1653
+ oj_quirks_mode_sym = ID2SYM(rb_intern("quirks_mode")); rb_gc_register_address(&oj_quirks_mode_sym);
1654
+ oj_space_before_sym = ID2SYM(rb_intern("space_before")); rb_gc_register_address(&oj_space_before_sym);
1655
+ oj_space_sym = ID2SYM(rb_intern("space")); rb_gc_register_address(&oj_space_sym);
1656
+ oj_trace_sym = ID2SYM(rb_intern("trace")); rb_gc_register_address(&oj_trace_sym);
1657
+ omit_nil_sym = ID2SYM(rb_intern("omit_nil")); rb_gc_register_address(&omit_nil_sym);
1658
+ rails_sym = ID2SYM(rb_intern("rails")); rb_gc_register_address(&rails_sym);
1659
+ raise_sym = ID2SYM(rb_intern("raise")); rb_gc_register_address(&raise_sym);
1660
+ ruby_sym = ID2SYM(rb_intern("ruby")); rb_gc_register_address(&ruby_sym);
1661
+ sec_prec_sym = ID2SYM(rb_intern("second_precision")); rb_gc_register_address(&sec_prec_sym);
1662
+ strict_sym = ID2SYM(rb_intern("strict")); rb_gc_register_address(&strict_sym);
1663
+ symbol_keys_sym = ID2SYM(rb_intern("symbol_keys")); rb_gc_register_address(&symbol_keys_sym);
1664
+ time_format_sym = ID2SYM(rb_intern("time_format")); rb_gc_register_address(&time_format_sym);
1665
+ unicode_xss_sym = ID2SYM(rb_intern("unicode_xss")); rb_gc_register_address(&unicode_xss_sym);
1666
+ unix_sym = ID2SYM(rb_intern("unix")); rb_gc_register_address(&unix_sym);
1667
+ unix_zone_sym = ID2SYM(rb_intern("unix_zone")); rb_gc_register_address(&unix_zone_sym);
1668
+ use_as_json_sym = ID2SYM(rb_intern("use_as_json")); rb_gc_register_address(&use_as_json_sym);
1669
+ use_to_hash_sym = ID2SYM(rb_intern("use_to_hash")); rb_gc_register_address(&use_to_hash_sym);
1670
+ use_to_json_sym = ID2SYM(rb_intern("use_to_json")); rb_gc_register_address(&use_to_json_sym);
1671
+ wab_sym = ID2SYM(rb_intern("wab")); rb_gc_register_address(&wab_sym);
1672
+ word_sym = ID2SYM(rb_intern("word")); rb_gc_register_address(&word_sym);
1673
+ xmlschema_sym = ID2SYM(rb_intern("xmlschema")); rb_gc_register_address(&xmlschema_sym);
1674
+ xss_safe_sym = ID2SYM(rb_intern("xss_safe")); rb_gc_register_address(&xss_safe_sym);
1675
+
1676
+ oj_slash_string = rb_str_new2("/"); rb_gc_register_address(&oj_slash_string);
1677
+ OBJ_FREEZE(oj_slash_string);
1678
+
1679
+ oj_default_options.mode = ObjectMode;
1680
+
1681
+ oj_hash_init();
1682
+ oj_odd_init();
1683
+ oj_mimic_rails_init();
1684
+
1685
+ #if HAVE_LIBPTHREAD
1686
+ if (0 != (err = pthread_mutex_init(&oj_cache_mutex, 0))) {
1687
+ rb_raise(rb_eException, "failed to initialize a mutex. %s", strerror(err));
1688
+ }
1689
+ #else
1690
+ oj_cache_mutex = rb_mutex_new();
1691
+ rb_gc_register_address(&oj_cache_mutex);
1692
+ #endif
1693
+ oj_init_doc();
1694
+ }