/
compat.c
286 lines (250 loc) · 9.65 KB
/
compat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2012 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.
#include <stdio.h>
#include "encode.h"
#include "err.h"
#include "hash.h"
#include "oj.h"
#include "parse.h"
#include "resolve.h"
#include "trace.h"
static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
const char * key = kval->key;
int klen = kval->klen;
Val parent = stack_peek(&pi->stack);
volatile VALUE rkey = kval->key_val;
if (Qundef == rkey && Yes == pi->options.create_ok && NULL != pi->options.create_id &&
*pi->options.create_id == *key && (int)pi->options.create_id_len == klen &&
0 == strncmp(pi->options.create_id, key, klen)) {
parent->classname = oj_strndup(str, len);
parent->clen = len;
} else {
volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
if (Qundef == rkey) {
VALUE *slot;
if (Yes == pi->options.sym_key) {
if (Qnil == (rkey = oj_sym_hash_get(key, klen, &slot))) {
rkey = rb_str_new(key, klen);
rkey = oj_encode(rkey);
rkey = rb_str_intern(rkey);
*slot = rkey;
rb_gc_register_address(slot);
}
} else {
if (Qnil == (rkey = oj_str_hash_get(key, klen, &slot))) {
rkey = rb_str_new(key, klen);
rkey = oj_encode(rkey);
*slot = rkey;
rb_gc_register_address(slot);
}
}
}
if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
if (Qnil != clas) {
rstr = rb_funcall(clas, oj_json_create_id, 1, rstr);
}
}
if (rb_cHash != rb_obj_class(parent->val)) {
// The rb_hash_set would still work but the unit tests for the
// json gem require the less efficient []= method be called to set
// values. Even using the store method to set the values will fail
// the unit tests.
rb_funcall(parent->val, rb_intern("[]="), 2, rkey, rstr);
} else {
rb_hash_aset(parent->val, rkey, rstr);
}
if (Yes == pi->options.trace) {
oj_trace_parse_call("set_string", pi, __FILE__, __LINE__, rstr);
}
}
}
static VALUE start_hash(ParseInfo pi) {
volatile VALUE h;
if (Qnil != pi->options.hash_class) {
h = rb_class_new_instance(0, NULL, pi->options.hash_class);
} else {
h = rb_hash_new();
}
if (Yes == pi->options.trace) {
oj_trace_parse_in("start_hash", pi, __FILE__, __LINE__);
}
return h;
}
static void end_hash(struct _parseInfo *pi) {
Val parent = stack_peek(&pi->stack);
if (0 != parent->classname) {
volatile VALUE clas;
clas = oj_name2class(pi, parent->classname, parent->clen, 0, rb_eArgError);
if (Qundef != clas) { // else an error
ID creatable = rb_intern("json_creatable?");
if (!rb_respond_to(clas, creatable) || Qtrue == rb_funcall(clas, creatable, 0)) {
parent->val = rb_funcall(clas, oj_json_create_id, 1, parent->val);
}
}
if (0 != parent->classname) {
xfree((char *)parent->classname);
parent->classname = 0;
}
}
if (Yes == pi->options.trace) {
oj_trace_parse_hash_end(pi, __FILE__, __LINE__);
}
}
static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
if (Qnil != clas) {
pi->stack.head->val = rb_funcall(clas, oj_json_create_id, 1, rstr);
return;
}
}
pi->stack.head->val = rstr;
if (Yes == pi->options.trace) {
oj_trace_parse_call("add_string", pi, __FILE__, __LINE__, rstr);
}
}
static void add_num(ParseInfo pi, NumInfo ni) {
pi->stack.head->val = oj_num_as_value(ni);
if (Yes == pi->options.trace) {
oj_trace_parse_call("add_number", pi, __FILE__, __LINE__, pi->stack.head->val);
}
}
static void hash_set_num(struct _parseInfo *pi, Val parent, NumInfo ni) {
volatile VALUE rval = oj_num_as_value(ni);
if (!oj_use_hash_alt && rb_cHash != rb_obj_class(parent->val)) {
// The rb_hash_set would still work but the unit tests for the
// json gem require the less efficient []= method be called to set
// values. Even using the store method to set the values will fail
// the unit tests.
rb_funcall(stack_peek(&pi->stack)->val,
rb_intern("[]="),
2,
oj_calc_hash_key(pi, parent),
rval);
} else {
rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), rval);
}
if (Yes == pi->options.trace) {
oj_trace_parse_call("set_number", pi, __FILE__, __LINE__, rval);
}
}
static void hash_set_value(ParseInfo pi, Val parent, VALUE value) {
if (rb_cHash != rb_obj_class(parent->val)) {
// The rb_hash_set would still work but the unit tests for the
// json gem require the less efficient []= method be called to set
// values. Even using the store method to set the values will fail
// the unit tests.
rb_funcall(stack_peek(&pi->stack)->val,
rb_intern("[]="),
2,
oj_calc_hash_key(pi, parent),
value);
} else {
rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), value);
}
if (Yes == pi->options.trace) {
oj_trace_parse_call("set_value", pi, __FILE__, __LINE__, value);
}
}
static VALUE start_array(ParseInfo pi) {
if (Qnil != pi->options.array_class) {
return rb_class_new_instance(0, NULL, pi->options.array_class);
}
if (Yes == pi->options.trace) {
oj_trace_parse_in("start_array", pi, __FILE__, __LINE__);
}
return rb_ary_new();
}
static void array_append_num(ParseInfo pi, NumInfo ni) {
Val parent = stack_peek(&pi->stack);
volatile VALUE rval = oj_num_as_value(ni);
if (!oj_use_array_alt && rb_cArray != rb_obj_class(parent->val)) {
// The rb_ary_push would still work but the unit tests for the json
// gem require the less efficient << method be called to push the
// values.
rb_funcall(parent->val, rb_intern("<<"), 1, rval);
} else {
rb_ary_push(parent->val, rval);
}
if (Yes == pi->options.trace) {
oj_trace_parse_call("append_number", pi, __FILE__, __LINE__, rval);
}
}
static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
if (Qnil != clas) {
rb_ary_push(stack_peek(&pi->stack)->val, rb_funcall(clas, oj_json_create_id, 1, rstr));
return;
}
}
rb_ary_push(stack_peek(&pi->stack)->val, rstr);
if (Yes == pi->options.trace) {
oj_trace_parse_call("append_string", pi, __FILE__, __LINE__, rstr);
}
}
void oj_set_compat_callbacks(ParseInfo pi) {
oj_set_strict_callbacks(pi);
pi->start_hash = start_hash;
pi->end_hash = end_hash;
pi->hash_set_cstr = hash_set_cstr;
pi->hash_set_num = hash_set_num;
pi->hash_set_value = hash_set_value;
pi->add_num = add_num;
pi->add_cstr = add_cstr;
pi->array_append_cstr = array_append_cstr;
pi->start_array = start_array;
pi->array_append_num = array_append_num;
}
VALUE
oj_compat_parse(int argc, VALUE *argv, VALUE self) {
struct _parseInfo pi;
parse_info_init(&pi);
pi.options = oj_default_options;
pi.handler = Qnil;
pi.err_class = Qnil;
pi.max_depth = 0;
pi.options.allow_nan = Yes;
pi.options.nilnil = Yes;
pi.options.empty_string = No;
oj_set_compat_callbacks(&pi);
if (T_STRING == rb_type(*argv)) {
return oj_pi_parse(argc, argv, &pi, 0, 0, false);
} else {
return oj_pi_sparse(argc, argv, &pi, 0);
}
}
VALUE
oj_compat_load(int argc, VALUE *argv, VALUE self) {
struct _parseInfo pi;
parse_info_init(&pi);
pi.options = oj_default_options;
pi.handler = Qnil;
pi.err_class = Qnil;
pi.max_depth = 0;
pi.options.allow_nan = Yes;
pi.options.nilnil = Yes;
pi.options.empty_string = Yes;
oj_set_compat_callbacks(&pi);
if (T_STRING == rb_type(*argv)) {
return oj_pi_parse(argc, argv, &pi, 0, 0, false);
} else {
return oj_pi_sparse(argc, argv, &pi, 0);
}
}
VALUE
oj_compat_parse_cstr(int argc, VALUE *argv, char *json, size_t len) {
struct _parseInfo pi;
parse_info_init(&pi);
pi.options = oj_default_options;
pi.handler = Qnil;
pi.err_class = Qnil;
pi.max_depth = 0;
pi.options.allow_nan = Yes;
pi.options.nilnil = Yes;
oj_set_compat_callbacks(&pi);
return oj_pi_parse(argc, argv, &pi, json, len, false);
}