oj 3.8.0

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