oj 2.18.5 → 3.16.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1452 -0
- data/README.md +53 -221
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +54 -72
- data/ext/oj/cache.c +329 -0
- data/ext/oj/cache.h +22 -0
- data/ext/oj/cache8.c +61 -63
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +38 -67
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +214 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +194 -110
- data/ext/oj/custom.c +1074 -0
- data/ext/oj/debug.c +126 -0
- data/ext/oj/dump.c +1276 -2494
- data/ext/oj/dump.h +110 -0
- data/ext/oj/dump_compat.c +897 -0
- data/ext/oj/dump_leaf.c +162 -0
- data/ext/oj/dump_object.c +710 -0
- data/ext/oj/dump_strict.c +399 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +28 -53
- data/ext/oj/err.h +49 -46
- data/ext/oj/extconf.rb +33 -32
- data/ext/oj/fast.c +1082 -1098
- data/ext/oj/intern.c +313 -0
- data/ext/oj/intern.h +22 -0
- data/ext/oj/mem.c +318 -0
- data/ext/oj/mem.h +53 -0
- data/ext/oj/mimic_json.c +919 -0
- data/ext/oj/object.c +545 -625
- data/ext/oj/odd.c +158 -168
- data/ext/oj/odd.h +32 -58
- data/ext/oj/oj.c +1727 -2080
- data/ext/oj/oj.h +334 -259
- data/ext/oj/parse.c +974 -753
- data/ext/oj/parse.h +97 -90
- data/ext/oj/parser.c +1600 -0
- data/ext/oj/parser.h +103 -0
- data/ext/oj/rails.c +1478 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +136 -163
- data/ext/oj/reader.h +76 -112
- data/ext/oj/resolve.c +45 -94
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +144 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +445 -511
- data/ext/oj/saj2.c +584 -0
- data/ext/oj/saj2.h +23 -0
- data/ext/oj/scp.c +82 -143
- data/ext/oj/simd.h +10 -0
- data/ext/oj/sparse.c +749 -644
- data/ext/oj/stream_writer.c +329 -0
- data/ext/oj/strict.c +114 -112
- data/ext/oj/string_writer.c +517 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +55 -0
- data/ext/oj/usual.c +1218 -0
- data/ext/oj/usual.h +69 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +75 -72
- data/ext/oj/val_stack.h +94 -127
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +586 -0
- data/lib/oj/active_support_helper.rb +1 -3
- data/lib/oj/bag.rb +8 -1
- data/lib/oj/easy_hash.rb +21 -13
- data/lib/oj/error.rb +10 -12
- data/lib/oj/json.rb +188 -0
- data/lib/oj/mimic.rb +165 -26
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/schandler.rb +5 -4
- data/lib/oj/state.rb +135 -0
- data/lib/oj/version.rb +2 -3
- data/lib/oj.rb +3 -31
- 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/InstallOptions.md +20 -0
- data/pages/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +337 -0
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +167 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- metadata +126 -163
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/_test_active.rb +0 -76
- data/test/_test_active_mimic.rb +0 -96
- data/test/_test_mimic_rails.rb +0 -126
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug.rb +0 -51
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/files.rb +0 -29
- data/test/foo.rb +0 -24
- data/test/helper.rb +0 -27
- data/test/io.rb +0 -48
- data/test/isolated/shared.rb +0 -310
- data/test/isolated/test_mimic_after.rb +0 -13
- data/test/isolated/test_mimic_alone.rb +0 -12
- data/test/isolated/test_mimic_as_json.rb +0 -45
- data/test/isolated/test_mimic_before.rb +0 -13
- data/test/isolated/test_mimic_define.rb +0 -28
- data/test/isolated/test_mimic_rails_after.rb +0 -22
- data/test/isolated/test_mimic_rails_before.rb +0 -21
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/isolated/test_mimic_redefine.rb +0 -15
- data/test/mod.rb +0 -16
- data/test/perf.rb +0 -107
- data/test/perf_compat.rb +0 -128
- data/test/perf_fast.rb +0 -164
- data/test/perf_file.rb +0 -64
- data/test/perf_object.rb +0 -138
- data/test/perf_saj.rb +0 -109
- data/test/perf_scp.rb +0 -151
- data/test/perf_simple.rb +0 -287
- data/test/perf_strict.rb +0 -128
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/sample/change.rb +0 -14
- data/test/sample/dir.rb +0 -19
- data/test/sample/doc.rb +0 -36
- data/test/sample/file.rb +0 -48
- data/test/sample/group.rb +0 -16
- data/test/sample/hasprops.rb +0 -16
- data/test/sample/layer.rb +0 -12
- data/test/sample/line.rb +0 -20
- data/test/sample/oval.rb +0 -10
- data/test/sample/rect.rb +0 -10
- data/test/sample/shape.rb +0 -35
- data/test/sample/text.rb +0 -20
- data/test/sample.rb +0 -55
- data/test/sample_json.rb +0 -37
- data/test/struct.rb +0 -29
- data/test/test_compat.rb +0 -398
- data/test/test_debian.rb +0 -53
- data/test/test_fast.rb +0 -458
- data/test/test_file.rb +0 -245
- data/test/test_gc.rb +0 -49
- data/test/test_hash.rb +0 -29
- data/test/test_object.rb +0 -745
- data/test/test_saj.rb +0 -186
- data/test/test_scp.rb +0 -396
- data/test/test_serializer.rb +0 -59
- data/test/test_strict.rb +0 -254
- data/test/test_various.rb +0 -1383
- data/test/test_writer.rb +0 -308
- data/test/write_timebars.rb +0 -31
data/ext/oj/mimic_json.c
ADDED
@@ -0,0 +1,919 @@
|
|
1
|
+
// Copyright (c) 2012, 2017 Peter Ohler. All rights reserved.
|
2
|
+
// Licensed under the MIT License. See LICENSE file in the project root for license details.
|
3
|
+
|
4
|
+
#include "dump.h"
|
5
|
+
#include "encode.h"
|
6
|
+
#include "mem.h"
|
7
|
+
#include "oj.h"
|
8
|
+
#include "parse.h"
|
9
|
+
|
10
|
+
extern const char oj_json_class[];
|
11
|
+
|
12
|
+
VALUE oj_array_nl_sym;
|
13
|
+
VALUE oj_ascii_only_sym;
|
14
|
+
VALUE oj_json_generator_error_class;
|
15
|
+
VALUE oj_json_parser_error_class;
|
16
|
+
VALUE oj_max_nesting_sym;
|
17
|
+
VALUE oj_object_nl_sym;
|
18
|
+
VALUE oj_space_before_sym;
|
19
|
+
VALUE oj_space_sym;
|
20
|
+
|
21
|
+
static VALUE state_class = Qundef;
|
22
|
+
|
23
|
+
// mimic JSON documentation
|
24
|
+
|
25
|
+
/* Document-module: JSON::Ext
|
26
|
+
*
|
27
|
+
* The Ext module is a placeholder in the mimic JSON module used for
|
28
|
+
* compatibility only.
|
29
|
+
*/
|
30
|
+
/* Document-class: JSON::Ext::Parser
|
31
|
+
*
|
32
|
+
* The JSON::Ext::Parser is a placeholder in the mimic JSON module used for
|
33
|
+
* compatibility only.
|
34
|
+
*/
|
35
|
+
/* Document-class: JSON::Ext::Generator
|
36
|
+
*
|
37
|
+
* The JSON::Ext::Generator is a placeholder in the mimic JSON module used for
|
38
|
+
* compatibility only.
|
39
|
+
*/
|
40
|
+
|
41
|
+
/* Document-method: parser=
|
42
|
+
* call-seq: parser=(parser)
|
43
|
+
*
|
44
|
+
* Does nothing other than provide compatibility.
|
45
|
+
* - *parser* [_Object_] ignored
|
46
|
+
*/
|
47
|
+
/* Document-method: generator=
|
48
|
+
* call-seq: generator=(generator)
|
49
|
+
*
|
50
|
+
* Does nothing other than provide compatibility.
|
51
|
+
* - *generator* [_Object_] ignored
|
52
|
+
*/
|
53
|
+
|
54
|
+
VALUE
|
55
|
+
oj_get_json_err_class(const char *err_classname) {
|
56
|
+
volatile VALUE json_module;
|
57
|
+
volatile VALUE clas;
|
58
|
+
volatile VALUE json_error_class;
|
59
|
+
|
60
|
+
if (rb_const_defined_at(rb_cObject, rb_intern("JSON"))) {
|
61
|
+
json_module = rb_const_get_at(rb_cObject, rb_intern("JSON"));
|
62
|
+
} else {
|
63
|
+
json_module = rb_define_module("JSON");
|
64
|
+
}
|
65
|
+
if (rb_const_defined_at(json_module, rb_intern("JSONError"))) {
|
66
|
+
json_error_class = rb_const_get(json_module, rb_intern("JSONError"));
|
67
|
+
} else {
|
68
|
+
json_error_class = rb_define_class_under(json_module, "JSONError", rb_eStandardError);
|
69
|
+
}
|
70
|
+
if (0 == strcmp(err_classname, "JSONError")) {
|
71
|
+
clas = json_error_class;
|
72
|
+
} else {
|
73
|
+
if (rb_const_defined_at(json_module, rb_intern(err_classname))) {
|
74
|
+
clas = rb_const_get(json_module, rb_intern(err_classname));
|
75
|
+
} else {
|
76
|
+
clas = rb_define_class_under(json_module, err_classname, json_error_class);
|
77
|
+
}
|
78
|
+
}
|
79
|
+
return clas;
|
80
|
+
}
|
81
|
+
|
82
|
+
void oj_parse_mimic_dump_options(VALUE ropts, Options copts) {
|
83
|
+
VALUE v;
|
84
|
+
size_t len;
|
85
|
+
|
86
|
+
if (T_HASH != rb_type(ropts)) {
|
87
|
+
if (rb_respond_to(ropts, oj_to_hash_id)) {
|
88
|
+
ropts = rb_funcall(ropts, oj_to_hash_id, 0);
|
89
|
+
} else if (rb_respond_to(ropts, oj_to_h_id)) {
|
90
|
+
ropts = rb_funcall(ropts, oj_to_h_id, 0);
|
91
|
+
} else if (Qnil == ropts) {
|
92
|
+
return;
|
93
|
+
} else {
|
94
|
+
rb_raise(rb_eArgError, "options must be a hash.");
|
95
|
+
}
|
96
|
+
}
|
97
|
+
v = rb_hash_lookup(ropts, oj_max_nesting_sym);
|
98
|
+
if (Qtrue == v) {
|
99
|
+
copts->dump_opts.max_depth = 100;
|
100
|
+
} else if (Qfalse == v || Qnil == v) {
|
101
|
+
copts->dump_opts.max_depth = MAX_DEPTH;
|
102
|
+
} else if (T_FIXNUM == rb_type(v)) {
|
103
|
+
copts->dump_opts.max_depth = NUM2INT(v);
|
104
|
+
if (0 >= copts->dump_opts.max_depth) {
|
105
|
+
copts->dump_opts.max_depth = MAX_DEPTH;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
if (Qnil != (v = rb_hash_lookup(ropts, oj_allow_nan_sym))) {
|
109
|
+
if (Qtrue == v) {
|
110
|
+
copts->dump_opts.nan_dump = WordNan;
|
111
|
+
} else {
|
112
|
+
copts->dump_opts.nan_dump = RaiseNan;
|
113
|
+
}
|
114
|
+
}
|
115
|
+
if (Qnil != (v = rb_hash_lookup(ropts, oj_indent_sym))) {
|
116
|
+
rb_check_type(v, T_STRING);
|
117
|
+
if (sizeof(copts->dump_opts.indent_str) <= (len = RSTRING_LEN(v))) {
|
118
|
+
rb_raise(rb_eArgError,
|
119
|
+
"indent string is limited to %lu characters.",
|
120
|
+
(unsigned long)sizeof(copts->dump_opts.indent_str));
|
121
|
+
}
|
122
|
+
strcpy(copts->dump_opts.indent_str, StringValuePtr(v));
|
123
|
+
copts->dump_opts.indent_size = (uint8_t)len;
|
124
|
+
copts->dump_opts.use = true;
|
125
|
+
}
|
126
|
+
if (Qnil != (v = rb_hash_lookup(ropts, oj_space_sym))) {
|
127
|
+
rb_check_type(v, T_STRING);
|
128
|
+
if (sizeof(copts->dump_opts.after_sep) <= (len = RSTRING_LEN(v))) {
|
129
|
+
rb_raise(rb_eArgError,
|
130
|
+
"space string is limited to %lu characters.",
|
131
|
+
(unsigned long)sizeof(copts->dump_opts.after_sep));
|
132
|
+
}
|
133
|
+
strcpy(copts->dump_opts.after_sep, StringValuePtr(v));
|
134
|
+
copts->dump_opts.after_size = (uint8_t)len;
|
135
|
+
copts->dump_opts.use = true;
|
136
|
+
}
|
137
|
+
if (Qnil != (v = rb_hash_lookup(ropts, oj_space_before_sym))) {
|
138
|
+
rb_check_type(v, T_STRING);
|
139
|
+
if (sizeof(copts->dump_opts.before_sep) <= (len = RSTRING_LEN(v))) {
|
140
|
+
rb_raise(rb_eArgError,
|
141
|
+
"space_before string is limited to %lu characters.",
|
142
|
+
(unsigned long)sizeof(copts->dump_opts.before_sep));
|
143
|
+
}
|
144
|
+
strcpy(copts->dump_opts.before_sep, StringValuePtr(v));
|
145
|
+
copts->dump_opts.before_size = (uint8_t)len;
|
146
|
+
copts->dump_opts.use = true;
|
147
|
+
}
|
148
|
+
if (Qnil != (v = rb_hash_lookup(ropts, oj_object_nl_sym))) {
|
149
|
+
rb_check_type(v, T_STRING);
|
150
|
+
if (sizeof(copts->dump_opts.hash_nl) <= (len = RSTRING_LEN(v))) {
|
151
|
+
rb_raise(rb_eArgError,
|
152
|
+
"object_nl string is limited to %lu characters.",
|
153
|
+
(unsigned long)sizeof(copts->dump_opts.hash_nl));
|
154
|
+
}
|
155
|
+
strcpy(copts->dump_opts.hash_nl, StringValuePtr(v));
|
156
|
+
copts->dump_opts.hash_size = (uint8_t)len;
|
157
|
+
copts->dump_opts.use = true;
|
158
|
+
}
|
159
|
+
if (Qnil != (v = rb_hash_lookup(ropts, oj_array_nl_sym))) {
|
160
|
+
rb_check_type(v, T_STRING);
|
161
|
+
if (sizeof(copts->dump_opts.array_nl) <= (len = RSTRING_LEN(v))) {
|
162
|
+
rb_raise(rb_eArgError,
|
163
|
+
"array_nl string is limited to %lu characters.",
|
164
|
+
(unsigned long)sizeof(copts->dump_opts.array_nl));
|
165
|
+
}
|
166
|
+
strcpy(copts->dump_opts.array_nl, StringValuePtr(v));
|
167
|
+
copts->dump_opts.array_size = (uint8_t)len;
|
168
|
+
copts->dump_opts.use = true;
|
169
|
+
}
|
170
|
+
if (Qnil != (v = rb_hash_lookup(ropts, oj_quirks_mode_sym))) {
|
171
|
+
copts->quirks_mode = (Qtrue == v) ? Yes : No;
|
172
|
+
}
|
173
|
+
if (Qnil != (v = rb_hash_lookup(ropts, oj_ascii_only_sym))) {
|
174
|
+
// generate seems to assume anything except nil and false are true.
|
175
|
+
if (Qfalse == v) {
|
176
|
+
copts->escape_mode = JXEsc;
|
177
|
+
} else {
|
178
|
+
copts->escape_mode = ASCIIEsc;
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
static int mimic_limit_arg(VALUE a) {
|
184
|
+
if (Qnil == a || T_FIXNUM != rb_type(a)) {
|
185
|
+
return -1;
|
186
|
+
}
|
187
|
+
return NUM2INT(a);
|
188
|
+
}
|
189
|
+
|
190
|
+
/* Document-method: dump
|
191
|
+
* call-seq: dump(obj, anIO=nil, limit=nil)
|
192
|
+
*
|
193
|
+
* Encodes an object as a JSON String.
|
194
|
+
*
|
195
|
+
* - *obj* [_Object_] object to convert to encode as JSON
|
196
|
+
* - *anIO* [_IO_] an IO that allows writing
|
197
|
+
* - *limit* [_Fixnum_] ignored
|
198
|
+
*
|
199
|
+
* Returns [_String_] a JSON string.
|
200
|
+
*/
|
201
|
+
static VALUE mimic_dump(int argc, VALUE *argv, VALUE self) {
|
202
|
+
struct _out out;
|
203
|
+
struct _options copts = oj_default_options;
|
204
|
+
VALUE rstr;
|
205
|
+
VALUE active_hack[1];
|
206
|
+
|
207
|
+
copts.str_rx.head = NULL;
|
208
|
+
copts.str_rx.tail = NULL;
|
209
|
+
|
210
|
+
oj_out_init(&out);
|
211
|
+
|
212
|
+
copts.escape_mode = JXEsc;
|
213
|
+
copts.mode = CompatMode;
|
214
|
+
|
215
|
+
/* seems like this is not correct
|
216
|
+
if (No == copts.nilnil && Qnil == *argv) {
|
217
|
+
rb_raise(rb_eTypeError, "nil not allowed.");
|
218
|
+
}
|
219
|
+
*/
|
220
|
+
copts.dump_opts.max_depth = MAX_DEPTH; // when using dump there is no limit
|
221
|
+
out.omit_nil = copts.dump_opts.omit_nil;
|
222
|
+
|
223
|
+
if (2 <= argc) {
|
224
|
+
int limit;
|
225
|
+
|
226
|
+
// The json gem take a more liberal approach to optional
|
227
|
+
// arguments. Expected are (obj, anIO=nil, limit=nil) yet the io
|
228
|
+
// argument can be left off completely and the 2nd argument is then
|
229
|
+
// the limit.
|
230
|
+
if (0 <= (limit = mimic_limit_arg(argv[1]))) {
|
231
|
+
copts.dump_opts.max_depth = limit;
|
232
|
+
}
|
233
|
+
if (3 <= argc && 0 <= (limit = mimic_limit_arg(argv[2]))) {
|
234
|
+
copts.dump_opts.max_depth = limit;
|
235
|
+
}
|
236
|
+
}
|
237
|
+
// ActiveSupport in active_support/core_ext/object/json.rb check the
|
238
|
+
// optional argument type to to_json and it the argument is a
|
239
|
+
// ::JSON::State it calls the JSON gem code otherwise it calls the active
|
240
|
+
// support encoder code. To make sure the desired branch is called a
|
241
|
+
// default ::JSON::State argument is passed in. Basically a hack to get
|
242
|
+
// around the active support hack so two wrongs make a right this time.
|
243
|
+
active_hack[0] = rb_funcall(state_class, oj_new_id, 0);
|
244
|
+
oj_dump_obj_to_json_using_params(*argv, &copts, &out, 1, active_hack);
|
245
|
+
|
246
|
+
if (0 == out.buf) {
|
247
|
+
rb_raise(rb_eNoMemError, "Not enough memory.");
|
248
|
+
}
|
249
|
+
rstr = rb_utf8_str_new_cstr(out.buf);
|
250
|
+
if (2 <= argc && Qnil != argv[1] && rb_respond_to(argv[1], oj_write_id)) {
|
251
|
+
VALUE io = argv[1];
|
252
|
+
VALUE args[1];
|
253
|
+
|
254
|
+
*args = rstr;
|
255
|
+
rb_funcall2(io, oj_write_id, 1, args);
|
256
|
+
rstr = io;
|
257
|
+
}
|
258
|
+
|
259
|
+
oj_out_free(&out);
|
260
|
+
|
261
|
+
return rstr;
|
262
|
+
}
|
263
|
+
|
264
|
+
// This is the signature for the hash_foreach callback also.
|
265
|
+
static int mimic_walk(VALUE key, VALUE obj, VALUE proc) {
|
266
|
+
switch (rb_type(obj)) {
|
267
|
+
case T_HASH: rb_hash_foreach(obj, mimic_walk, proc); break;
|
268
|
+
case T_ARRAY: {
|
269
|
+
size_t cnt = RARRAY_LEN(obj);
|
270
|
+
size_t i;
|
271
|
+
|
272
|
+
for (i = 0; i < cnt; i++) {
|
273
|
+
mimic_walk(Qnil, RARRAY_AREF(obj, i), proc);
|
274
|
+
}
|
275
|
+
break;
|
276
|
+
}
|
277
|
+
default: break;
|
278
|
+
}
|
279
|
+
if (Qnil == proc) {
|
280
|
+
if (rb_block_given_p()) {
|
281
|
+
rb_yield(obj);
|
282
|
+
}
|
283
|
+
} else {
|
284
|
+
VALUE args[1];
|
285
|
+
|
286
|
+
*args = obj;
|
287
|
+
rb_proc_call_with_block(proc, 1, args, Qnil);
|
288
|
+
}
|
289
|
+
return ST_CONTINUE;
|
290
|
+
}
|
291
|
+
|
292
|
+
/* Document-method: restore
|
293
|
+
* call-seq: restore(source, proc=nil)
|
294
|
+
*
|
295
|
+
* Loads a Ruby Object from a JSON source that can be either a String or an
|
296
|
+
* IO. If Proc is given or a block is provided it is called with each nested
|
297
|
+
* element of the loaded Object.
|
298
|
+
*
|
299
|
+
* - *source* [_String_|IO] JSON source
|
300
|
+
* - *proc* [_Proc_] to yield to on each element or nil
|
301
|
+
*
|
302
|
+
* Returns [_Object_] the decoded Object.
|
303
|
+
*/
|
304
|
+
|
305
|
+
/* Document-method: load
|
306
|
+
* call-seq: load(source, proc=nil)
|
307
|
+
*
|
308
|
+
* Loads a Ruby Object from a JSON source that can be either a String or an
|
309
|
+
* IO. If Proc is given or a block is provided it is called with each nested
|
310
|
+
* element of the loaded Object.
|
311
|
+
*
|
312
|
+
* - *source* [_String_|IO] JSON source
|
313
|
+
* - *proc* [_Proc_] to yield to on each element or nil
|
314
|
+
*
|
315
|
+
* Returns [_Object_] the decode Object.
|
316
|
+
*/
|
317
|
+
static VALUE mimic_load(int argc, VALUE *argv, VALUE self) {
|
318
|
+
VALUE obj;
|
319
|
+
VALUE p = Qnil;
|
320
|
+
|
321
|
+
obj = oj_compat_load(argc, argv, self);
|
322
|
+
if (2 <= argc) {
|
323
|
+
if (rb_cProc == rb_obj_class(argv[1])) {
|
324
|
+
p = argv[1];
|
325
|
+
} else if (3 <= argc) {
|
326
|
+
if (rb_cProc == rb_obj_class(argv[2])) {
|
327
|
+
p = argv[2];
|
328
|
+
}
|
329
|
+
}
|
330
|
+
}
|
331
|
+
mimic_walk(Qnil, obj, p);
|
332
|
+
|
333
|
+
return obj;
|
334
|
+
}
|
335
|
+
|
336
|
+
/* Document-method: []
|
337
|
+
* call-seq: [](obj, opts={})
|
338
|
+
*
|
339
|
+
* If the obj argument is a String then it is assumed to be a JSON String and
|
340
|
+
* parsed otherwise the obj is encoded as a JSON String.
|
341
|
+
*
|
342
|
+
* - *obj* [_String_|Hash|Array] object to convert
|
343
|
+
* - *opts* [_Hash_] same options as either generate or parse
|
344
|
+
*
|
345
|
+
* Returns [_Object_]
|
346
|
+
*/
|
347
|
+
static VALUE mimic_dump_load(int argc, VALUE *argv, VALUE self) {
|
348
|
+
if (1 > argc) {
|
349
|
+
rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
|
350
|
+
}
|
351
|
+
if (T_STRING == rb_type(*argv)) {
|
352
|
+
return mimic_load(argc, argv, self);
|
353
|
+
}
|
354
|
+
return mimic_dump(argc, argv, self);
|
355
|
+
}
|
356
|
+
|
357
|
+
static VALUE mimic_generate_core(int argc, VALUE *argv, Options copts) {
|
358
|
+
struct _out out;
|
359
|
+
VALUE rstr;
|
360
|
+
|
361
|
+
if (0 == argc) {
|
362
|
+
rb_raise(rb_eArgError, "wrong number of arguments (0))");
|
363
|
+
}
|
364
|
+
memset(out.stack_buffer, 0, sizeof(out.stack_buffer));
|
365
|
+
|
366
|
+
oj_out_init(&out);
|
367
|
+
|
368
|
+
out.omit_nil = copts->dump_opts.omit_nil;
|
369
|
+
// For obj.to_json or generate nan is not allowed but if called from dump
|
370
|
+
// it is.
|
371
|
+
copts->dump_opts.nan_dump = RaiseNan;
|
372
|
+
copts->mode = CompatMode;
|
373
|
+
copts->to_json = Yes;
|
374
|
+
if (2 == argc && Qnil != argv[1]) {
|
375
|
+
oj_parse_mimic_dump_options(argv[1], copts);
|
376
|
+
}
|
377
|
+
/* seems like this is not correct
|
378
|
+
if (No == copts->nilnil && Qnil == *argv) {
|
379
|
+
rb_raise(rb_eTypeError, "nil not allowed.");
|
380
|
+
}
|
381
|
+
*/
|
382
|
+
if (1 < argc) {
|
383
|
+
oj_dump_obj_to_json_using_params(*argv, copts, &out, argc - 1, argv + 1);
|
384
|
+
} else {
|
385
|
+
VALUE active_hack[1];
|
386
|
+
|
387
|
+
if (Qundef == state_class) {
|
388
|
+
rb_warn("Oj::Rails.mimic_JSON was called implicitly. "
|
389
|
+
"Call it explicitly beforehand if you want to remove this warning.");
|
390
|
+
oj_define_mimic_json(0, NULL, Qnil);
|
391
|
+
}
|
392
|
+
active_hack[0] = rb_funcall(state_class, oj_new_id, 0);
|
393
|
+
oj_dump_obj_to_json_using_params(*argv, copts, &out, 1, active_hack);
|
394
|
+
}
|
395
|
+
if (0 == out.buf) {
|
396
|
+
rb_raise(rb_eNoMemError, "Not enough memory.");
|
397
|
+
}
|
398
|
+
rstr = rb_utf8_str_new_cstr(out.buf);
|
399
|
+
|
400
|
+
oj_out_free(&out);
|
401
|
+
|
402
|
+
return rstr;
|
403
|
+
}
|
404
|
+
|
405
|
+
/* Document-method: fast_generate
|
406
|
+
* call-seq: fast_generate(obj, opts=nil)
|
407
|
+
* Same as generate().
|
408
|
+
* @see generate
|
409
|
+
*/
|
410
|
+
|
411
|
+
/* Document-method: generate
|
412
|
+
* call-seq: generate(obj, opts=nil)
|
413
|
+
*
|
414
|
+
* Encode obj as a JSON String. The obj argument must be a Hash, Array, or
|
415
|
+
* respond to to_h or to_json. Options other than those listed such as
|
416
|
+
* +:allow_nan+ or +:max_nesting+ are ignored.
|
417
|
+
*
|
418
|
+
* - *obj* [_Object_|Hash|Array] object to convert to a JSON String
|
419
|
+
* - *opts* [_Hash_] options
|
420
|
+
* - - *:indent* [_String_] String to use for indentation.
|
421
|
+
* - *:space* [_String_] String placed after a , or : delimiter
|
422
|
+
* - *:space_before* [_String_] String placed before a : delimiter
|
423
|
+
* - *:object_nl* [_String_] String placed after a JSON object
|
424
|
+
* - *:array_nl* [_String_] String placed after a JSON array
|
425
|
+
* - *:ascii_only* [_Boolean_] if not nil or false then use only ascii characters in the output.
|
426
|
+
* Note JSON.generate does support this even if it is not documented.
|
427
|
+
*
|
428
|
+
* Returns [_String_] generated JSON.
|
429
|
+
*/
|
430
|
+
VALUE
|
431
|
+
oj_mimic_generate(int argc, VALUE *argv, VALUE self) {
|
432
|
+
struct _options copts = oj_default_options;
|
433
|
+
|
434
|
+
copts.str_rx.head = NULL;
|
435
|
+
copts.str_rx.tail = NULL;
|
436
|
+
|
437
|
+
return mimic_generate_core(argc, argv, &copts);
|
438
|
+
}
|
439
|
+
|
440
|
+
/* Document-method: pretty_generate
|
441
|
+
* call-seq: pretty_generate(obj, opts=nil)
|
442
|
+
*
|
443
|
+
* Same as generate() but with different defaults for the spacing options.
|
444
|
+
* @see generate
|
445
|
+
*
|
446
|
+
* Return [_String_] the generated JSON.
|
447
|
+
*/
|
448
|
+
VALUE
|
449
|
+
oj_mimic_pretty_generate(int argc, VALUE *argv, VALUE self) {
|
450
|
+
struct _options copts = oj_default_options;
|
451
|
+
VALUE rargs[2];
|
452
|
+
volatile VALUE h;
|
453
|
+
|
454
|
+
// Some (all?) json gem to_json methods need a State instance and not just
|
455
|
+
// a Hash. I haven't dug deep enough to find out why but using a State
|
456
|
+
// instance and not a Hash gives the desired behavior.
|
457
|
+
*rargs = *argv;
|
458
|
+
if (0 == argc) {
|
459
|
+
rb_raise(rb_eArgError, "wrong number of arguments (0))");
|
460
|
+
}
|
461
|
+
if (1 == argc || Qnil == argv[1]) {
|
462
|
+
h = rb_hash_new();
|
463
|
+
} else {
|
464
|
+
h = argv[1];
|
465
|
+
}
|
466
|
+
if (!oj_hash_has_key(h, oj_indent_sym)) {
|
467
|
+
rb_hash_aset(h, oj_indent_sym, rb_str_new2(" "));
|
468
|
+
}
|
469
|
+
if (!oj_hash_has_key(h, oj_space_before_sym)) {
|
470
|
+
rb_hash_aset(h, oj_space_before_sym, rb_str_new2(""));
|
471
|
+
}
|
472
|
+
if (!oj_hash_has_key(h, oj_space_sym)) {
|
473
|
+
rb_hash_aset(h, oj_space_sym, rb_str_new2(" "));
|
474
|
+
}
|
475
|
+
if (!oj_hash_has_key(h, oj_object_nl_sym)) {
|
476
|
+
rb_hash_aset(h, oj_object_nl_sym, rb_str_new2("\n"));
|
477
|
+
}
|
478
|
+
if (!oj_hash_has_key(h, oj_array_nl_sym)) {
|
479
|
+
rb_hash_aset(h, oj_array_nl_sym, rb_str_new2("\n"));
|
480
|
+
}
|
481
|
+
if (Qundef == state_class) {
|
482
|
+
rb_warn("Oj::Rails.mimic_JSON was called implicitly. "
|
483
|
+
"Call it explicitly beforehand if you want to remove this warning.");
|
484
|
+
oj_define_mimic_json(0, NULL, Qnil);
|
485
|
+
}
|
486
|
+
rargs[1] = rb_funcall(state_class, oj_new_id, 1, h);
|
487
|
+
|
488
|
+
copts.str_rx.head = NULL;
|
489
|
+
copts.str_rx.tail = NULL;
|
490
|
+
strcpy(copts.dump_opts.indent_str, " ");
|
491
|
+
copts.dump_opts.indent_size = (uint8_t)strlen(copts.dump_opts.indent_str);
|
492
|
+
strcpy(copts.dump_opts.before_sep, "");
|
493
|
+
copts.dump_opts.before_size = (uint8_t)strlen(copts.dump_opts.before_sep);
|
494
|
+
strcpy(copts.dump_opts.after_sep, " ");
|
495
|
+
copts.dump_opts.after_size = (uint8_t)strlen(copts.dump_opts.after_sep);
|
496
|
+
strcpy(copts.dump_opts.hash_nl, "\n");
|
497
|
+
copts.dump_opts.hash_size = (uint8_t)strlen(copts.dump_opts.hash_nl);
|
498
|
+
strcpy(copts.dump_opts.array_nl, "\n");
|
499
|
+
copts.dump_opts.array_size = (uint8_t)strlen(copts.dump_opts.array_nl);
|
500
|
+
copts.dump_opts.use = true;
|
501
|
+
|
502
|
+
return mimic_generate_core(2, rargs, &copts);
|
503
|
+
}
|
504
|
+
|
505
|
+
static int parse_options_cb(VALUE k, VALUE v, VALUE info) {
|
506
|
+
struct _parseInfo *pi = (struct _parseInfo *)info;
|
507
|
+
|
508
|
+
if (oj_symbolize_names_sym == k) {
|
509
|
+
pi->options.sym_key = (Qtrue == v) ? Yes : No;
|
510
|
+
} else if (oj_quirks_mode_sym == k) {
|
511
|
+
pi->options.quirks_mode = (Qtrue == v) ? Yes : No;
|
512
|
+
} else if (oj_create_additions_sym == k) {
|
513
|
+
pi->options.create_ok = (Qtrue == v) ? Yes : No;
|
514
|
+
} else if (oj_allow_nan_sym == k) {
|
515
|
+
pi->options.allow_nan = (Qtrue == v) ? Yes : No;
|
516
|
+
} else if (oj_hash_class_sym == k) {
|
517
|
+
if (Qnil == v) {
|
518
|
+
pi->options.hash_class = Qnil;
|
519
|
+
} else {
|
520
|
+
rb_check_type(v, T_CLASS);
|
521
|
+
pi->options.hash_class = v;
|
522
|
+
}
|
523
|
+
} else if (oj_object_class_sym == k) {
|
524
|
+
if (Qnil == v) {
|
525
|
+
pi->options.hash_class = Qnil;
|
526
|
+
} else {
|
527
|
+
rb_check_type(v, T_CLASS);
|
528
|
+
pi->options.hash_class = v;
|
529
|
+
}
|
530
|
+
} else if (oj_array_class_sym == k) {
|
531
|
+
if (Qnil == v) {
|
532
|
+
pi->options.array_class = Qnil;
|
533
|
+
} else {
|
534
|
+
rb_check_type(v, T_CLASS);
|
535
|
+
pi->options.array_class = v;
|
536
|
+
}
|
537
|
+
} else if (oj_decimal_class_sym == k) {
|
538
|
+
pi->options.compat_bigdec = (oj_bigdecimal_class == v);
|
539
|
+
}
|
540
|
+
return ST_CONTINUE;
|
541
|
+
}
|
542
|
+
|
543
|
+
static VALUE mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
|
544
|
+
struct _parseInfo pi;
|
545
|
+
VALUE ropts;
|
546
|
+
VALUE args[1];
|
547
|
+
|
548
|
+
rb_scan_args(argc, argv, "11", NULL, &ropts);
|
549
|
+
parse_info_init(&pi);
|
550
|
+
oj_set_compat_callbacks(&pi);
|
551
|
+
|
552
|
+
pi.err_class = oj_json_parser_error_class;
|
553
|
+
// pi.err_class = Qnil;
|
554
|
+
|
555
|
+
pi.options = oj_default_options;
|
556
|
+
pi.options.auto_define = No;
|
557
|
+
pi.options.quirks_mode = Yes;
|
558
|
+
pi.options.allow_invalid = Yes;
|
559
|
+
pi.options.empty_string = No;
|
560
|
+
pi.options.create_ok = No;
|
561
|
+
pi.options.allow_nan = (bang ? Yes : No);
|
562
|
+
pi.options.nilnil = No;
|
563
|
+
pi.options.bigdec_load = RubyDec;
|
564
|
+
pi.options.mode = CompatMode;
|
565
|
+
pi.max_depth = 100;
|
566
|
+
|
567
|
+
if (Qnil != ropts) {
|
568
|
+
VALUE v;
|
569
|
+
|
570
|
+
if (T_HASH != rb_type(ropts)) {
|
571
|
+
rb_raise(rb_eArgError, "options must be a hash.");
|
572
|
+
}
|
573
|
+
rb_hash_foreach(ropts, parse_options_cb, (VALUE)&pi);
|
574
|
+
v = rb_hash_lookup(ropts, oj_max_nesting_sym);
|
575
|
+
if (Qtrue == v) {
|
576
|
+
pi.max_depth = 100;
|
577
|
+
} else if (Qfalse == v || Qnil == v) {
|
578
|
+
pi.max_depth = 0;
|
579
|
+
} else if (T_FIXNUM == rb_type(v)) {
|
580
|
+
pi.max_depth = NUM2INT(v);
|
581
|
+
}
|
582
|
+
oj_parse_opt_match_string(&pi.options.str_rx, ropts);
|
583
|
+
if (Yes == pi.options.create_ok && Yes == pi.options.sym_key) {
|
584
|
+
rb_raise(rb_eArgError, ":symbolize_names and :create_additions can not both be true.");
|
585
|
+
}
|
586
|
+
}
|
587
|
+
*args = *argv;
|
588
|
+
|
589
|
+
if (T_STRING == rb_type(*args)) {
|
590
|
+
return oj_pi_parse(1, args, &pi, 0, 0, false);
|
591
|
+
} else {
|
592
|
+
return oj_pi_sparse(1, args, &pi, 0);
|
593
|
+
}
|
594
|
+
}
|
595
|
+
|
596
|
+
/* Document-method: parse
|
597
|
+
* call-seq: parse(source, opts=nil)
|
598
|
+
*
|
599
|
+
* Parses a JSON String or IO into a Ruby Object. Options other than those
|
600
|
+
* listed such as +:allow_nan+ or +:max_nesting+ are ignored. +:object_class+ and
|
601
|
+
* +:array_object+ are not supported.
|
602
|
+
*
|
603
|
+
* - *source* [_String_|IO] source to parse
|
604
|
+
* - *opts* [_Hash_] options
|
605
|
+
* - *:symbolize* [Boolean] _names flag indicating JSON object keys should be Symbols instead of
|
606
|
+
* Strings
|
607
|
+
* - *:create_additions* [Boolean] flag indicating a key matching +create_id+ in a JSON object
|
608
|
+
* should trigger the creation of Ruby Object
|
609
|
+
*
|
610
|
+
* Returns [Object]
|
611
|
+
* @see create_id=
|
612
|
+
*/
|
613
|
+
VALUE
|
614
|
+
oj_mimic_parse(int argc, VALUE *argv, VALUE self) {
|
615
|
+
return mimic_parse_core(argc, argv, self, false);
|
616
|
+
}
|
617
|
+
|
618
|
+
/* Document-method: parse!
|
619
|
+
* call-seq: parse!(source, opts=nil)
|
620
|
+
*
|
621
|
+
* Same as parse().
|
622
|
+
* @see parse
|
623
|
+
*/
|
624
|
+
static VALUE mimic_parse_bang(int argc, VALUE *argv, VALUE self) {
|
625
|
+
return mimic_parse_core(argc, argv, self, true);
|
626
|
+
}
|
627
|
+
|
628
|
+
/* Document-method: recurse_proc
|
629
|
+
* call-seq: recurse_proc(obj, &proc)
|
630
|
+
*
|
631
|
+
* Yields to the proc for every element in the obj recursively.
|
632
|
+
*
|
633
|
+
* - *obj* [_Hash_|Array] object to walk
|
634
|
+
* - *proc* [_Proc_] to yield to on each element
|
635
|
+
*/
|
636
|
+
static VALUE mimic_recurse_proc(VALUE self, VALUE obj) {
|
637
|
+
rb_need_block();
|
638
|
+
mimic_walk(Qnil, obj, Qnil);
|
639
|
+
|
640
|
+
return Qnil;
|
641
|
+
}
|
642
|
+
|
643
|
+
/* Document-method: create_id=
|
644
|
+
* call-seq: create_id=(id)
|
645
|
+
*
|
646
|
+
* Sets the create_id tag to look for in JSON document. That key triggers the
|
647
|
+
* creation of a class with the same name.
|
648
|
+
*
|
649
|
+
* - *id* [_nil_|String] new create_id
|
650
|
+
*
|
651
|
+
* Returns [_nil_|_String_] the id.
|
652
|
+
*/
|
653
|
+
static VALUE mimic_set_create_id(VALUE self, VALUE id) {
|
654
|
+
if (NULL != oj_default_options.create_id) {
|
655
|
+
if (oj_json_class != oj_default_options.create_id) {
|
656
|
+
OJ_R_FREE((char *)oj_default_options.create_id);
|
657
|
+
}
|
658
|
+
oj_default_options.create_id = NULL;
|
659
|
+
oj_default_options.create_id_len = 0;
|
660
|
+
}
|
661
|
+
if (Qnil != id) {
|
662
|
+
const char *ptr = StringValueCStr(id);
|
663
|
+
size_t len = RSTRING_LEN(id) + 1;
|
664
|
+
|
665
|
+
oj_default_options.create_id = OJ_R_ALLOC_N(char, len);
|
666
|
+
strcpy((char *)oj_default_options.create_id, ptr);
|
667
|
+
oj_default_options.create_id_len = len - 1;
|
668
|
+
}
|
669
|
+
return id;
|
670
|
+
}
|
671
|
+
|
672
|
+
/* Document-method: create_id
|
673
|
+
* call-seq: create_id()
|
674
|
+
*
|
675
|
+
* Returns [_String_] the create_id.
|
676
|
+
*/
|
677
|
+
static VALUE mimic_create_id(VALUE self) {
|
678
|
+
if (NULL != oj_default_options.create_id) {
|
679
|
+
return rb_utf8_str_new(oj_default_options.create_id, oj_default_options.create_id_len);
|
680
|
+
}
|
681
|
+
return rb_str_new_cstr(oj_json_class);
|
682
|
+
}
|
683
|
+
|
684
|
+
static struct _options mimic_object_to_json_options = {0, // indent
|
685
|
+
No, // circular
|
686
|
+
No, // auto_define
|
687
|
+
No, // sym_key
|
688
|
+
JXEsc, // escape_mode
|
689
|
+
CompatMode, // mode
|
690
|
+
No, // class_cache
|
691
|
+
RubyTime, // time_format
|
692
|
+
No, // bigdec_as_num
|
693
|
+
RubyDec, // bigdec_load
|
694
|
+
false, // compat_bigdec
|
695
|
+
No, // to_hash
|
696
|
+
No, // to_json
|
697
|
+
No, // as_json
|
698
|
+
No, // raw_json
|
699
|
+
No, // nilnil
|
700
|
+
No, // empty_string
|
701
|
+
Yes, // allow_gc
|
702
|
+
Yes, // quirks_mode
|
703
|
+
Yes, // allow_invalid
|
704
|
+
No, // create_ok
|
705
|
+
No, // allow_nan
|
706
|
+
No, // trace
|
707
|
+
No, // safe
|
708
|
+
false, // sec_prec_set
|
709
|
+
No, // ignore_under
|
710
|
+
Yes, // cache_keys
|
711
|
+
0, // cache_str
|
712
|
+
0, // int_range_min
|
713
|
+
0, // int_range_max
|
714
|
+
oj_json_class, // create_id
|
715
|
+
10, // create_id_len
|
716
|
+
3, // sec_prec
|
717
|
+
0, // float_prec
|
718
|
+
"%0.16g", // float_fmt
|
719
|
+
Qnil, // hash_class
|
720
|
+
Qnil, // array_class
|
721
|
+
{
|
722
|
+
// dump_opts
|
723
|
+
false, // use
|
724
|
+
"", // indent
|
725
|
+
"", // before_sep
|
726
|
+
"", // after_sep
|
727
|
+
"", // hash_nl
|
728
|
+
"", // array_nl
|
729
|
+
0, // indent_size
|
730
|
+
0, // before_size
|
731
|
+
0, // after_size
|
732
|
+
0, // hash_size
|
733
|
+
0, // array_size
|
734
|
+
RaiseNan, // nan_dump
|
735
|
+
false, // omit_nil
|
736
|
+
false, // omit_null_byte
|
737
|
+
100, // max_depth
|
738
|
+
},
|
739
|
+
{
|
740
|
+
// str_rx
|
741
|
+
NULL, // head
|
742
|
+
NULL, // tail
|
743
|
+
{'\0'}, // err
|
744
|
+
}};
|
745
|
+
|
746
|
+
static VALUE mimic_object_to_json(int argc, VALUE *argv, VALUE self) {
|
747
|
+
struct _out out;
|
748
|
+
VALUE rstr;
|
749
|
+
struct _options copts = oj_default_options;
|
750
|
+
|
751
|
+
copts.str_rx.head = NULL;
|
752
|
+
copts.str_rx.tail = NULL;
|
753
|
+
|
754
|
+
oj_out_init(&out);
|
755
|
+
|
756
|
+
out.omit_nil = copts.dump_opts.omit_nil;
|
757
|
+
copts.mode = CompatMode;
|
758
|
+
copts.to_json = No;
|
759
|
+
if (1 <= argc && Qnil != argv[0]) {
|
760
|
+
oj_parse_mimic_dump_options(argv[0], &copts);
|
761
|
+
}
|
762
|
+
// To be strict the mimic_object_to_json_options should be used but people
|
763
|
+
// seem to prefer the option of changing that.
|
764
|
+
// oj_dump_obj_to_json(self, &mimic_object_to_json_options, &out);
|
765
|
+
oj_dump_obj_to_json_using_params(self, &copts, &out, argc, argv);
|
766
|
+
if (NULL == out.buf) {
|
767
|
+
rb_raise(rb_eNoMemError, "Not enough memory.");
|
768
|
+
}
|
769
|
+
rstr = rb_utf8_str_new_cstr(out.buf);
|
770
|
+
|
771
|
+
oj_out_free(&out);
|
772
|
+
|
773
|
+
return rstr;
|
774
|
+
}
|
775
|
+
|
776
|
+
/* Document-method: state
|
777
|
+
* call-seq: state()
|
778
|
+
*
|
779
|
+
* Returns [_JSON::State_] the JSON::State class.
|
780
|
+
*/
|
781
|
+
static VALUE mimic_state(VALUE self) {
|
782
|
+
return state_class;
|
783
|
+
}
|
784
|
+
|
785
|
+
void oj_mimic_json_methods(VALUE json) {
|
786
|
+
VALUE json_error;
|
787
|
+
VALUE generator;
|
788
|
+
VALUE ext;
|
789
|
+
VALUE verbose;
|
790
|
+
|
791
|
+
// rb_undef_method doesn't work for modules or maybe sometimes
|
792
|
+
// doesn't. Anyway setting verbose should hide the warning.
|
793
|
+
verbose = rb_gv_get("$VERBOSE");
|
794
|
+
rb_gv_set("$VERBOSE", Qfalse);
|
795
|
+
|
796
|
+
rb_undef_method(json, "create_id=");
|
797
|
+
rb_define_module_function(json, "create_id=", mimic_set_create_id, 1);
|
798
|
+
rb_undef_method(json, "create_id");
|
799
|
+
rb_define_module_function(json, "create_id", mimic_create_id, 0);
|
800
|
+
|
801
|
+
rb_undef_method(json, "dump");
|
802
|
+
rb_define_module_function(json, "dump", mimic_dump, -1);
|
803
|
+
rb_undef_method(json, "load");
|
804
|
+
rb_define_module_function(json, "load", mimic_load, -1);
|
805
|
+
rb_define_module_function(json, "restore", mimic_load, -1);
|
806
|
+
rb_undef_method(json, "recurse_proc");
|
807
|
+
rb_define_module_function(json, "recurse_proc", mimic_recurse_proc, 1);
|
808
|
+
rb_undef_method(json, "[]");
|
809
|
+
rb_define_module_function(json, "[]", mimic_dump_load, -1);
|
810
|
+
|
811
|
+
rb_undef_method(json, "generate");
|
812
|
+
rb_define_module_function(json, "generate", oj_mimic_generate, -1);
|
813
|
+
rb_undef_method(json, "fast_generate");
|
814
|
+
rb_define_module_function(json, "fast_generate", oj_mimic_generate, -1);
|
815
|
+
rb_undef_method(json, "pretty_generate");
|
816
|
+
rb_define_module_function(json, "pretty_generate", oj_mimic_pretty_generate, -1);
|
817
|
+
// For older versions of JSON, the deprecated unparse methods.
|
818
|
+
rb_undef_method(json, "unparse");
|
819
|
+
rb_define_module_function(json, "unparse", oj_mimic_generate, -1);
|
820
|
+
rb_define_module_function(json, "fast_unparse", oj_mimic_generate, -1);
|
821
|
+
rb_define_module_function(json, "pretty_unparse", oj_mimic_pretty_generate, -1);
|
822
|
+
|
823
|
+
rb_undef_method(json, "parse");
|
824
|
+
rb_define_module_function(json, "parse", oj_mimic_parse, -1);
|
825
|
+
rb_undef_method(json, "parse!");
|
826
|
+
rb_define_module_function(json, "parse!", mimic_parse_bang, -1);
|
827
|
+
|
828
|
+
rb_undef_method(json, "state");
|
829
|
+
rb_define_module_function(json, "state", mimic_state, 0);
|
830
|
+
rb_gv_set("$VERBOSE", verbose);
|
831
|
+
|
832
|
+
if (rb_const_defined_at(json, rb_intern("JSONError"))) {
|
833
|
+
json_error = rb_const_get(json, rb_intern("JSONError"));
|
834
|
+
} else {
|
835
|
+
json_error = rb_define_class_under(json, "JSONError", rb_eStandardError);
|
836
|
+
}
|
837
|
+
|
838
|
+
rb_global_variable(&oj_json_parser_error_class);
|
839
|
+
if (rb_const_defined_at(json, rb_intern("ParserError"))) {
|
840
|
+
oj_json_parser_error_class = rb_const_get(json, rb_intern("ParserError"));
|
841
|
+
} else {
|
842
|
+
oj_json_parser_error_class = rb_define_class_under(json, "ParserError", json_error);
|
843
|
+
}
|
844
|
+
|
845
|
+
rb_global_variable(&oj_json_generator_error_class);
|
846
|
+
if (rb_const_defined_at(json, rb_intern("GeneratorError"))) {
|
847
|
+
oj_json_generator_error_class = rb_const_get(json, rb_intern("GeneratorError"));
|
848
|
+
} else {
|
849
|
+
oj_json_generator_error_class = rb_define_class_under(json, "GeneratorError", json_error);
|
850
|
+
}
|
851
|
+
if (rb_const_defined_at(json, rb_intern("NestingError"))) {
|
852
|
+
rb_const_get(json, rb_intern("NestingError"));
|
853
|
+
} else {
|
854
|
+
rb_define_class_under(json, "NestingError", json_error);
|
855
|
+
}
|
856
|
+
|
857
|
+
if (rb_const_defined_at(json, rb_intern("Ext"))) {
|
858
|
+
ext = rb_const_get_at(json, rb_intern("Ext"));
|
859
|
+
} else {
|
860
|
+
ext = rb_define_module_under(json, "Ext");
|
861
|
+
}
|
862
|
+
if (rb_const_defined_at(ext, rb_intern("Generator"))) {
|
863
|
+
generator = rb_const_get_at(ext, rb_intern("Generator"));
|
864
|
+
} else {
|
865
|
+
generator = rb_define_module_under(ext, "Generator");
|
866
|
+
}
|
867
|
+
if (!rb_const_defined_at(generator, rb_intern("State"))) {
|
868
|
+
rb_require("oj/state");
|
869
|
+
}
|
870
|
+
// Pull in the JSON::State mimic file.
|
871
|
+
rb_global_variable(&state_class);
|
872
|
+
state_class = rb_const_get_at(generator, rb_intern("State"));
|
873
|
+
}
|
874
|
+
|
875
|
+
/* Document-module: JSON
|
876
|
+
*
|
877
|
+
* A mimic of the json gem module.
|
878
|
+
*/
|
879
|
+
VALUE
|
880
|
+
oj_define_mimic_json(int argc, VALUE *argv, VALUE self) {
|
881
|
+
VALUE dummy;
|
882
|
+
VALUE verbose;
|
883
|
+
VALUE json;
|
884
|
+
|
885
|
+
// Either set the paths to indicate JSON has been loaded or replaces the
|
886
|
+
// methods if it has been loaded.
|
887
|
+
if (rb_const_defined_at(rb_cObject, rb_intern("JSON"))) {
|
888
|
+
json = rb_const_get_at(rb_cObject, rb_intern("JSON"));
|
889
|
+
} else {
|
890
|
+
json = rb_define_module("JSON");
|
891
|
+
}
|
892
|
+
verbose = rb_gv_get("$VERBOSE");
|
893
|
+
rb_gv_set("$VERBOSE", Qfalse);
|
894
|
+
rb_define_module_function(rb_cObject, "JSON", mimic_dump_load, -1);
|
895
|
+
dummy = rb_gv_get("$LOADED_FEATURES");
|
896
|
+
if (rb_type(dummy) == T_ARRAY) {
|
897
|
+
rb_ary_push(dummy, rb_str_new2("json"));
|
898
|
+
if (0 < argc) {
|
899
|
+
VALUE mimic_args[1];
|
900
|
+
|
901
|
+
*mimic_args = *argv;
|
902
|
+
rb_funcall2(Oj, rb_intern("mimic_loaded"), 1, mimic_args);
|
903
|
+
} else {
|
904
|
+
rb_funcall2(Oj, rb_intern("mimic_loaded"), 0, 0);
|
905
|
+
}
|
906
|
+
}
|
907
|
+
oj_mimic_json_methods(json);
|
908
|
+
|
909
|
+
if (!rb_const_defined(rb_cObject, rb_intern("ActiveSupport"))) {
|
910
|
+
rb_define_method(rb_cObject, "to_json", mimic_object_to_json, -1);
|
911
|
+
}
|
912
|
+
|
913
|
+
rb_gv_set("$VERBOSE", verbose);
|
914
|
+
|
915
|
+
oj_default_options = mimic_object_to_json_options;
|
916
|
+
oj_default_options.to_json = Yes;
|
917
|
+
|
918
|
+
return json;
|
919
|
+
}
|