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