oj_windows 3.16.15
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 +7 -0
- data/CHANGELOG.md +44 -0
- data/LICENSE +21 -0
- data/README.md +164 -0
- data/ext/oj_windows/buf.h +85 -0
- data/ext/oj_windows/cache.c +339 -0
- data/ext/oj_windows/cache.h +22 -0
- data/ext/oj_windows/cache8.c +105 -0
- data/ext/oj_windows/cache8.h +21 -0
- data/ext/oj_windows/circarray.c +64 -0
- data/ext/oj_windows/circarray.h +22 -0
- data/ext/oj_windows/code.c +214 -0
- data/ext/oj_windows/code.h +40 -0
- data/ext/oj_windows/compat.c +239 -0
- data/ext/oj_windows/custom.c +1074 -0
- data/ext/oj_windows/debug.c +126 -0
- data/ext/oj_windows/dump.c +1556 -0
- data/ext/oj_windows/dump.h +110 -0
- data/ext/oj_windows/dump_compat.c +901 -0
- data/ext/oj_windows/dump_leaf.c +162 -0
- data/ext/oj_windows/dump_object.c +710 -0
- data/ext/oj_windows/dump_strict.c +405 -0
- data/ext/oj_windows/encode.h +16 -0
- data/ext/oj_windows/err.c +57 -0
- data/ext/oj_windows/err.h +67 -0
- data/ext/oj_windows/extconf.rb +77 -0
- data/ext/oj_windows/fast.c +1710 -0
- data/ext/oj_windows/intern.c +325 -0
- data/ext/oj_windows/intern.h +22 -0
- data/ext/oj_windows/mem.c +320 -0
- data/ext/oj_windows/mem.h +53 -0
- data/ext/oj_windows/mimic_json.c +919 -0
- data/ext/oj_windows/object.c +726 -0
- data/ext/oj_windows/odd.c +245 -0
- data/ext/oj_windows/odd.h +43 -0
- data/ext/oj_windows/oj.c +2097 -0
- data/ext/oj_windows/oj.h +420 -0
- data/ext/oj_windows/parse.c +1317 -0
- data/ext/oj_windows/parse.h +113 -0
- data/ext/oj_windows/parser.c +1600 -0
- data/ext/oj_windows/parser.h +103 -0
- data/ext/oj_windows/rails.c +1484 -0
- data/ext/oj_windows/rails.h +18 -0
- data/ext/oj_windows/reader.c +222 -0
- data/ext/oj_windows/reader.h +137 -0
- data/ext/oj_windows/resolve.c +80 -0
- data/ext/oj_windows/resolve.h +12 -0
- data/ext/oj_windows/rxclass.c +144 -0
- data/ext/oj_windows/rxclass.h +26 -0
- data/ext/oj_windows/saj.c +675 -0
- data/ext/oj_windows/saj2.c +584 -0
- data/ext/oj_windows/saj2.h +23 -0
- data/ext/oj_windows/scp.c +187 -0
- data/ext/oj_windows/simd.h +47 -0
- data/ext/oj_windows/sparse.c +946 -0
- data/ext/oj_windows/stream_writer.c +329 -0
- data/ext/oj_windows/strict.c +189 -0
- data/ext/oj_windows/string_writer.c +517 -0
- data/ext/oj_windows/trace.c +72 -0
- data/ext/oj_windows/trace.h +55 -0
- data/ext/oj_windows/usual.c +1218 -0
- data/ext/oj_windows/usual.h +69 -0
- data/ext/oj_windows/util.c +136 -0
- data/ext/oj_windows/util.h +20 -0
- data/ext/oj_windows/val_stack.c +101 -0
- data/ext/oj_windows/val_stack.h +151 -0
- data/ext/oj_windows/validate.c +46 -0
- data/ext/oj_windows/wab.c +584 -0
- data/lib/oj/active_support_helper.rb +39 -0
- data/lib/oj/bag.rb +95 -0
- data/lib/oj/easy_hash.rb +52 -0
- data/lib/oj/error.rb +21 -0
- data/lib/oj/json.rb +188 -0
- data/lib/oj/mimic.rb +301 -0
- data/lib/oj/saj.rb +80 -0
- data/lib/oj/schandler.rb +143 -0
- data/lib/oj/state.rb +135 -0
- data/lib/oj/version.rb +4 -0
- data/lib/oj_windows/active_support_helper.rb +39 -0
- data/lib/oj_windows/bag.rb +95 -0
- data/lib/oj_windows/easy_hash.rb +52 -0
- data/lib/oj_windows/error.rb +21 -0
- data/lib/oj_windows/json.rb +188 -0
- data/lib/oj_windows/mimic.rb +301 -0
- data/lib/oj_windows/saj.rb +80 -0
- data/lib/oj_windows/schandler.rb +143 -0
- data/lib/oj_windows/state.rb +135 -0
- data/lib/oj_windows/version.rb +4 -0
- data/lib/oj_windows.rb +15 -0
- data/pages/Advanced.md +38 -0
- data/pages/Compatibility.md +49 -0
- data/pages/Custom.md +37 -0
- data/pages/Encoding.md +61 -0
- data/pages/InstallOptions.md +20 -0
- data/pages/JsonGem.md +60 -0
- data/pages/Modes.md +94 -0
- data/pages/Options.md +339 -0
- data/pages/Parser.md +134 -0
- data/pages/Rails.md +85 -0
- data/pages/Security.md +43 -0
- data/pages/WAB.md +12 -0
- metadata +242 -0
|
@@ -0,0 +1,329 @@
|
|
|
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 <errno.h>
|
|
5
|
+
#include <ruby.h>
|
|
6
|
+
|
|
7
|
+
#include "encode.h"
|
|
8
|
+
#include "mem.h"
|
|
9
|
+
|
|
10
|
+
extern VALUE Oj;
|
|
11
|
+
|
|
12
|
+
static void stream_writer_free(void *ptr) {
|
|
13
|
+
StreamWriter sw;
|
|
14
|
+
|
|
15
|
+
if (0 == ptr) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
sw = (StreamWriter)ptr;
|
|
19
|
+
OJ_R_FREE(sw->sw.out.buf);
|
|
20
|
+
OJ_R_FREE(sw->sw.types);
|
|
21
|
+
OJ_R_FREE(ptr);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static const rb_data_type_t oj_stream_writer_type = {
|
|
25
|
+
"oj_windows/stream_writer",
|
|
26
|
+
{
|
|
27
|
+
NULL,
|
|
28
|
+
stream_writer_free,
|
|
29
|
+
NULL,
|
|
30
|
+
},
|
|
31
|
+
0,
|
|
32
|
+
0,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
static void stream_writer_reset_buf(StreamWriter sw) {
|
|
36
|
+
sw->sw.out.cur = sw->sw.out.buf;
|
|
37
|
+
*sw->sw.out.cur = '\0';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static void stream_writer_write(StreamWriter sw) {
|
|
41
|
+
ssize_t size = sw->sw.out.cur - sw->sw.out.buf;
|
|
42
|
+
|
|
43
|
+
switch (sw->type) {
|
|
44
|
+
case STRING_IO:
|
|
45
|
+
case STREAM_IO:
|
|
46
|
+
case FILE_IO: {
|
|
47
|
+
volatile VALUE rs = rb_utf8_str_new(sw->sw.out.buf, size);
|
|
48
|
+
rb_funcall(sw->stream, oj_write_id, 1, rs);
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
default: rb_raise(rb_eArgError, "expected an IO Object.");
|
|
52
|
+
}
|
|
53
|
+
stream_writer_reset_buf(sw);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static VALUE buffer_size_sym = Qundef;
|
|
57
|
+
|
|
58
|
+
/* Document-method: new
|
|
59
|
+
* call-seq: new(io, options)
|
|
60
|
+
*
|
|
61
|
+
* Creates a new StreamWriter. Options are supported according the specified
|
|
62
|
+
* mode or the mode in the default options. Note that if mimic_JSON or
|
|
63
|
+
* Oj.optimize_rails has not been called then the behavior of the modes may
|
|
64
|
+
* not be the same as if they were.
|
|
65
|
+
*
|
|
66
|
+
* In addition to the regular dump options for the various modes a
|
|
67
|
+
* _:buffer_size_ option is available. It should be set to a positive
|
|
68
|
+
* integer. It is considered a hint of how large the initial internal buffer
|
|
69
|
+
* should be and also a hint on when to flush.
|
|
70
|
+
*
|
|
71
|
+
* - *io* [_IO_] stream to write to
|
|
72
|
+
* - *options* [_Hash_] formatting options
|
|
73
|
+
*/
|
|
74
|
+
static VALUE stream_writer_new(int argc, VALUE *argv, VALUE self) {
|
|
75
|
+
StreamWriterType type = STREAM_IO;
|
|
76
|
+
int fd = 0;
|
|
77
|
+
VALUE stream = argv[0];
|
|
78
|
+
VALUE clas = rb_obj_class(stream);
|
|
79
|
+
StreamWriter sw;
|
|
80
|
+
#if !IS_WINDOWS
|
|
81
|
+
VALUE s;
|
|
82
|
+
#endif
|
|
83
|
+
|
|
84
|
+
if (oj_stringio_class == clas) {
|
|
85
|
+
type = STRING_IO;
|
|
86
|
+
#if !IS_WINDOWS
|
|
87
|
+
} else if (rb_respond_to(stream, oj_fileno_id) && Qnil != (s = rb_funcall(stream, oj_fileno_id, 0)) &&
|
|
88
|
+
0 != (fd = FIX2INT(s))) {
|
|
89
|
+
type = FILE_IO;
|
|
90
|
+
#endif
|
|
91
|
+
} else if (rb_respond_to(stream, oj_write_id)) {
|
|
92
|
+
type = STREAM_IO;
|
|
93
|
+
} else {
|
|
94
|
+
rb_raise(rb_eArgError, "expected an IO Object.");
|
|
95
|
+
}
|
|
96
|
+
sw = OJ_R_ALLOC(struct _streamWriter);
|
|
97
|
+
if (2 == argc && T_HASH == rb_type(argv[1])) {
|
|
98
|
+
volatile VALUE v;
|
|
99
|
+
int buf_size = 0;
|
|
100
|
+
|
|
101
|
+
if (Qundef == buffer_size_sym) {
|
|
102
|
+
buffer_size_sym = ID2SYM(rb_intern("buffer_size"));
|
|
103
|
+
rb_gc_register_address(&buffer_size_sym);
|
|
104
|
+
}
|
|
105
|
+
if (Qnil != (v = rb_hash_lookup(argv[1], buffer_size_sym))) {
|
|
106
|
+
if (rb_cInteger != rb_obj_class(v)) {
|
|
107
|
+
OJ_R_FREE(sw);
|
|
108
|
+
rb_raise(rb_eArgError, ":buffer size must be a Integer.");
|
|
109
|
+
}
|
|
110
|
+
buf_size = FIX2INT(v);
|
|
111
|
+
}
|
|
112
|
+
oj_str_writer_init(&sw->sw, buf_size);
|
|
113
|
+
oj_parse_options(argv[1], &sw->sw.opts);
|
|
114
|
+
sw->flush_limit = buf_size;
|
|
115
|
+
} else {
|
|
116
|
+
oj_str_writer_init(&sw->sw, 4096);
|
|
117
|
+
sw->flush_limit = 0;
|
|
118
|
+
}
|
|
119
|
+
sw->sw.out.indent = sw->sw.opts.indent;
|
|
120
|
+
sw->stream = stream;
|
|
121
|
+
sw->type = type;
|
|
122
|
+
sw->fd = fd;
|
|
123
|
+
|
|
124
|
+
return TypedData_Wrap_Struct(oj_stream_writer_class, &oj_stream_writer_type, sw);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/* Document-method: push_key
|
|
128
|
+
* call-seq: push_key(key)
|
|
129
|
+
*
|
|
130
|
+
* Pushes a key onto the JSON document. The key will be used for the next push
|
|
131
|
+
* if currently in a JSON object and ignored otherwise. If a key is provided on
|
|
132
|
+
* the next push then that new key will be ignored.
|
|
133
|
+
*
|
|
134
|
+
* - *key* [_String_] the key pending for the next push
|
|
135
|
+
*/
|
|
136
|
+
static VALUE stream_writer_push_key(VALUE self, VALUE key) {
|
|
137
|
+
StreamWriter sw;
|
|
138
|
+
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
|
|
139
|
+
|
|
140
|
+
oj_str_writer_push_key(&sw->sw, StringValuePtr(key));
|
|
141
|
+
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
142
|
+
stream_writer_write(sw);
|
|
143
|
+
}
|
|
144
|
+
return Qnil;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/* Document-method: push_object
|
|
148
|
+
* call-seq: push_object(key=nil)
|
|
149
|
+
*
|
|
150
|
+
* Pushes an object onto the JSON document. Future pushes will be to this object
|
|
151
|
+
* until a pop() is called.
|
|
152
|
+
*
|
|
153
|
+
* - *key* [_String_] the key if adding to an object in the JSON document
|
|
154
|
+
*/
|
|
155
|
+
static VALUE stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
|
|
156
|
+
StreamWriter sw;
|
|
157
|
+
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
|
|
158
|
+
|
|
159
|
+
switch (argc) {
|
|
160
|
+
case 0: oj_str_writer_push_object(&sw->sw, 0); break;
|
|
161
|
+
case 1:
|
|
162
|
+
if (Qnil == argv[0]) {
|
|
163
|
+
oj_str_writer_push_object(&sw->sw, 0);
|
|
164
|
+
} else {
|
|
165
|
+
oj_str_writer_push_object(&sw->sw, StringValuePtr(argv[0]));
|
|
166
|
+
}
|
|
167
|
+
break;
|
|
168
|
+
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
|
|
169
|
+
}
|
|
170
|
+
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
171
|
+
stream_writer_write(sw);
|
|
172
|
+
}
|
|
173
|
+
return Qnil;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/* Document-method: push_array
|
|
177
|
+
* call-seq: push_array(key=nil)
|
|
178
|
+
*
|
|
179
|
+
* Pushes an array onto the JSON document. Future pushes will be to this object
|
|
180
|
+
* until a pop() is called.
|
|
181
|
+
*
|
|
182
|
+
* - *key* [_String_] the key if adding to an object in the JSON document
|
|
183
|
+
*/
|
|
184
|
+
static VALUE stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
|
|
185
|
+
StreamWriter sw;
|
|
186
|
+
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
|
|
187
|
+
|
|
188
|
+
switch (argc) {
|
|
189
|
+
case 0: oj_str_writer_push_array(&sw->sw, 0); break;
|
|
190
|
+
case 1:
|
|
191
|
+
if (Qnil == argv[0]) {
|
|
192
|
+
oj_str_writer_push_array(&sw->sw, 0);
|
|
193
|
+
} else {
|
|
194
|
+
oj_str_writer_push_array(&sw->sw, StringValuePtr(argv[0]));
|
|
195
|
+
}
|
|
196
|
+
break;
|
|
197
|
+
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
|
|
198
|
+
}
|
|
199
|
+
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
200
|
+
stream_writer_write(sw);
|
|
201
|
+
}
|
|
202
|
+
return Qnil;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/* Document-method: push_value
|
|
206
|
+
* call-seq: push_value(value, key=nil)
|
|
207
|
+
*
|
|
208
|
+
* Pushes a value onto the JSON document.
|
|
209
|
+
* - *value* [_Object_] value to add to the JSON document
|
|
210
|
+
* - *key* [_String_] the key if adding to an object in the JSON document
|
|
211
|
+
*/
|
|
212
|
+
static VALUE stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
|
|
213
|
+
StreamWriter sw;
|
|
214
|
+
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
|
|
215
|
+
|
|
216
|
+
switch (argc) {
|
|
217
|
+
case 1: oj_str_writer_push_value((StrWriter)sw, *argv, 0); break;
|
|
218
|
+
case 2:
|
|
219
|
+
if (Qnil == argv[1]) {
|
|
220
|
+
oj_str_writer_push_value((StrWriter)sw, *argv, 0);
|
|
221
|
+
} else {
|
|
222
|
+
oj_str_writer_push_value((StrWriter)sw, *argv, StringValuePtr(argv[1]));
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
|
|
226
|
+
}
|
|
227
|
+
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
228
|
+
stream_writer_write(sw);
|
|
229
|
+
}
|
|
230
|
+
return Qnil;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/* Document-method: push_json
|
|
234
|
+
* call-seq: push_json(value, key=nil)
|
|
235
|
+
*
|
|
236
|
+
* Pushes a string onto the JSON document. The String must be a valid JSON
|
|
237
|
+
* encoded string. No additional checking is done to verify the validity of the
|
|
238
|
+
* string.
|
|
239
|
+
* - *value* [_Object_] value to add to the JSON document
|
|
240
|
+
* - *key* [_String_] the key if adding to an object in the JSON document
|
|
241
|
+
*/
|
|
242
|
+
static VALUE stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
|
|
243
|
+
StreamWriter sw;
|
|
244
|
+
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
|
|
245
|
+
|
|
246
|
+
switch (argc) {
|
|
247
|
+
case 1: oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0); break;
|
|
248
|
+
case 2:
|
|
249
|
+
if (Qnil == argv[1]) {
|
|
250
|
+
oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0);
|
|
251
|
+
} else {
|
|
252
|
+
oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), StringValuePtr(argv[1]));
|
|
253
|
+
}
|
|
254
|
+
break;
|
|
255
|
+
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
|
|
256
|
+
}
|
|
257
|
+
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
258
|
+
stream_writer_write(sw);
|
|
259
|
+
}
|
|
260
|
+
return Qnil;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/* Document-method: pop
|
|
264
|
+
* call-seq: pop()
|
|
265
|
+
*
|
|
266
|
+
* Pops up a level in the JSON document closing the array or object that is
|
|
267
|
+
* currently open.
|
|
268
|
+
*/
|
|
269
|
+
static VALUE stream_writer_pop(VALUE self) {
|
|
270
|
+
StreamWriter sw;
|
|
271
|
+
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
|
|
272
|
+
|
|
273
|
+
oj_str_writer_pop(&sw->sw);
|
|
274
|
+
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
275
|
+
stream_writer_write(sw);
|
|
276
|
+
}
|
|
277
|
+
return Qnil;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/* Document-method: pop_all
|
|
281
|
+
* call-seq: pop_all()
|
|
282
|
+
*
|
|
283
|
+
* Pops all level in the JSON document closing all the array or object that is
|
|
284
|
+
* currently open.
|
|
285
|
+
*/
|
|
286
|
+
static VALUE stream_writer_pop_all(VALUE self) {
|
|
287
|
+
StreamWriter sw;
|
|
288
|
+
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
|
|
289
|
+
|
|
290
|
+
oj_str_writer_pop_all(&sw->sw);
|
|
291
|
+
stream_writer_write(sw);
|
|
292
|
+
|
|
293
|
+
return Qnil;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/* Document-method: flush
|
|
297
|
+
* call-seq: flush()
|
|
298
|
+
*
|
|
299
|
+
* Flush any remaining characters in the buffer.
|
|
300
|
+
*/
|
|
301
|
+
static VALUE stream_writer_flush(VALUE self) {
|
|
302
|
+
StreamWriter sw;
|
|
303
|
+
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
|
|
304
|
+
stream_writer_write(sw);
|
|
305
|
+
|
|
306
|
+
return Qnil;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/* Document-class: Oj::StreamWriter
|
|
310
|
+
*
|
|
311
|
+
* Supports building a JSON document one element at a time. Build the IO stream
|
|
312
|
+
* document by pushing values into the document. Pushing an array or an object
|
|
313
|
+
* will create that element in the JSON document and subsequent pushes will add
|
|
314
|
+
* the elements to that array or object until a pop() is called.
|
|
315
|
+
*/
|
|
316
|
+
void oj_stream_writer_init(void) {
|
|
317
|
+
oj_stream_writer_class = rb_define_class_under(Oj, "StreamWriter", rb_cObject);
|
|
318
|
+
rb_gc_register_address(&oj_stream_writer_class);
|
|
319
|
+
rb_undef_alloc_func(oj_stream_writer_class);
|
|
320
|
+
rb_define_module_function(oj_stream_writer_class, "new", stream_writer_new, -1);
|
|
321
|
+
rb_define_method(oj_stream_writer_class, "push_key", stream_writer_push_key, 1);
|
|
322
|
+
rb_define_method(oj_stream_writer_class, "push_object", stream_writer_push_object, -1);
|
|
323
|
+
rb_define_method(oj_stream_writer_class, "push_array", stream_writer_push_array, -1);
|
|
324
|
+
rb_define_method(oj_stream_writer_class, "push_value", stream_writer_push_value, -1);
|
|
325
|
+
rb_define_method(oj_stream_writer_class, "push_json", stream_writer_push_json, -1);
|
|
326
|
+
rb_define_method(oj_stream_writer_class, "pop", stream_writer_pop, 0);
|
|
327
|
+
rb_define_method(oj_stream_writer_class, "pop_all", stream_writer_pop_all, 0);
|
|
328
|
+
rb_define_method(oj_stream_writer_class, "flush", stream_writer_flush, 0);
|
|
329
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// Copyright (c) 2012 Peter Ohler. All rights reserved.
|
|
2
|
+
// Licensed under the MIT License. See LICENSE file in the project root for license details.
|
|
3
|
+
|
|
4
|
+
#include <stdio.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
#include <string.h>
|
|
7
|
+
#if !IS_WINDOWS
|
|
8
|
+
#include <unistd.h>
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
#include "encode.h"
|
|
12
|
+
#include "err.h"
|
|
13
|
+
#include "intern.h"
|
|
14
|
+
#include "oj.h"
|
|
15
|
+
#include "parse.h"
|
|
16
|
+
#include "trace.h"
|
|
17
|
+
|
|
18
|
+
VALUE oj_cstr_to_value(const char *str, size_t len, size_t cache_str) {
|
|
19
|
+
volatile VALUE rstr = Qnil;
|
|
20
|
+
|
|
21
|
+
if (len < cache_str) {
|
|
22
|
+
rstr = oj_str_intern(str, len);
|
|
23
|
+
} else {
|
|
24
|
+
rstr = rb_utf8_str_new(str, len);
|
|
25
|
+
}
|
|
26
|
+
return rstr;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
VALUE oj_calc_hash_key(ParseInfo pi, Val parent) {
|
|
30
|
+
volatile VALUE rkey = parent->key_val;
|
|
31
|
+
|
|
32
|
+
if (Qundef != rkey) {
|
|
33
|
+
return rkey;
|
|
34
|
+
}
|
|
35
|
+
if (Yes != pi->options.cache_keys) {
|
|
36
|
+
if (Yes == pi->options.sym_key) {
|
|
37
|
+
rkey = ID2SYM(rb_intern3(parent->key, parent->klen, oj_utf8_encoding));
|
|
38
|
+
} else {
|
|
39
|
+
rkey = rb_utf8_str_new(parent->key, parent->klen);
|
|
40
|
+
OBJ_FREEZE(rkey); // frozen when used as a Hash key anyway
|
|
41
|
+
}
|
|
42
|
+
return rkey;
|
|
43
|
+
}
|
|
44
|
+
if (Yes == pi->options.sym_key) {
|
|
45
|
+
rkey = oj_sym_intern(parent->key, parent->klen);
|
|
46
|
+
} else {
|
|
47
|
+
rkey = oj_str_intern(parent->key, parent->klen);
|
|
48
|
+
}
|
|
49
|
+
return rkey;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static void hash_end(ParseInfo pi) {
|
|
53
|
+
TRACE_PARSE_HASH_END(pi->options.trace, pi);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static void array_end(ParseInfo pi) {
|
|
57
|
+
TRACE_PARSE_ARRAY_END(pi->options.trace, pi);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static VALUE noop_hash_key(ParseInfo pi, const char *key, size_t klen) {
|
|
61
|
+
return Qundef;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static void add_value(ParseInfo pi, VALUE val) {
|
|
65
|
+
TRACE_PARSE_CALL(pi->options.trace, "add_value", pi, val);
|
|
66
|
+
pi->stack.head->val = val;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
|
|
70
|
+
volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
|
|
71
|
+
|
|
72
|
+
pi->stack.head->val = rstr;
|
|
73
|
+
TRACE_PARSE_CALL(pi->options.trace, "add_string", pi, rstr);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static void add_num(ParseInfo pi, NumInfo ni) {
|
|
77
|
+
if (ni->infinity || ni->nan) {
|
|
78
|
+
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
|
|
79
|
+
}
|
|
80
|
+
pi->stack.head->val = oj_num_as_value(ni);
|
|
81
|
+
TRACE_PARSE_CALL(pi->options.trace, "add_number", pi, pi->stack.head->val);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static VALUE start_hash(ParseInfo pi) {
|
|
85
|
+
if (Qnil != pi->options.hash_class) {
|
|
86
|
+
return rb_class_new_instance(0, NULL, pi->options.hash_class);
|
|
87
|
+
}
|
|
88
|
+
TRACE_PARSE_IN(pi->options.trace, "start_hash", pi);
|
|
89
|
+
return rb_hash_new();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static void hash_set_cstr(ParseInfo pi, Val parent, const char *str, size_t len, const char *orig) {
|
|
93
|
+
volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
|
|
94
|
+
|
|
95
|
+
rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), rstr);
|
|
96
|
+
TRACE_PARSE_CALL(pi->options.trace, "set_string", pi, rstr);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
static void hash_set_num(ParseInfo pi, Val parent, NumInfo ni) {
|
|
100
|
+
volatile VALUE v;
|
|
101
|
+
|
|
102
|
+
if (ni->infinity || ni->nan) {
|
|
103
|
+
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
|
|
104
|
+
}
|
|
105
|
+
v = oj_num_as_value(ni);
|
|
106
|
+
rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), v);
|
|
107
|
+
TRACE_PARSE_CALL(pi->options.trace, "set_number", pi, v);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static void hash_set_value(ParseInfo pi, Val parent, VALUE value) {
|
|
111
|
+
rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), value);
|
|
112
|
+
TRACE_PARSE_CALL(pi->options.trace, "set_value", pi, value);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static VALUE start_array(ParseInfo pi) {
|
|
116
|
+
TRACE_PARSE_IN(pi->options.trace, "start_array", pi);
|
|
117
|
+
return rb_ary_new();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
|
|
121
|
+
volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
|
|
122
|
+
|
|
123
|
+
rb_ary_push(stack_peek(&pi->stack)->val, rstr);
|
|
124
|
+
TRACE_PARSE_CALL(pi->options.trace, "append_string", pi, rstr);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static void array_append_num(ParseInfo pi, NumInfo ni) {
|
|
128
|
+
volatile VALUE v;
|
|
129
|
+
|
|
130
|
+
if (ni->infinity || ni->nan) {
|
|
131
|
+
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
|
|
132
|
+
}
|
|
133
|
+
v = oj_num_as_value(ni);
|
|
134
|
+
rb_ary_push(stack_peek(&pi->stack)->val, v);
|
|
135
|
+
TRACE_PARSE_CALL(pi->options.trace, "append_number", pi, v);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static void array_append_value(ParseInfo pi, VALUE value) {
|
|
139
|
+
rb_ary_push(stack_peek(&pi->stack)->val, value);
|
|
140
|
+
TRACE_PARSE_CALL(pi->options.trace, "append_value", pi, value);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
void oj_set_strict_callbacks(ParseInfo pi) {
|
|
144
|
+
pi->start_hash = start_hash;
|
|
145
|
+
pi->end_hash = hash_end;
|
|
146
|
+
pi->hash_key = noop_hash_key;
|
|
147
|
+
pi->hash_set_cstr = hash_set_cstr;
|
|
148
|
+
pi->hash_set_num = hash_set_num;
|
|
149
|
+
pi->hash_set_value = hash_set_value;
|
|
150
|
+
pi->start_array = start_array;
|
|
151
|
+
pi->end_array = array_end;
|
|
152
|
+
pi->array_append_cstr = array_append_cstr;
|
|
153
|
+
pi->array_append_num = array_append_num;
|
|
154
|
+
pi->array_append_value = array_append_value;
|
|
155
|
+
pi->add_cstr = add_cstr;
|
|
156
|
+
pi->add_num = add_num;
|
|
157
|
+
pi->add_value = add_value;
|
|
158
|
+
pi->expect_value = 1;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
VALUE
|
|
162
|
+
oj_strict_parse(int argc, VALUE *argv, VALUE self) {
|
|
163
|
+
struct _parseInfo pi;
|
|
164
|
+
|
|
165
|
+
parse_info_init(&pi);
|
|
166
|
+
pi.options = oj_default_options;
|
|
167
|
+
pi.handler = Qnil;
|
|
168
|
+
pi.err_class = Qnil;
|
|
169
|
+
oj_set_strict_callbacks(&pi);
|
|
170
|
+
|
|
171
|
+
if (T_STRING == rb_type(*argv)) {
|
|
172
|
+
return oj_pi_parse(argc, argv, &pi, 0, 0, true);
|
|
173
|
+
} else {
|
|
174
|
+
return oj_pi_sparse(argc, argv, &pi, 0);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
VALUE
|
|
179
|
+
oj_strict_parse_cstr(int argc, VALUE *argv, char *json, size_t len) {
|
|
180
|
+
struct _parseInfo pi;
|
|
181
|
+
|
|
182
|
+
parse_info_init(&pi);
|
|
183
|
+
pi.options = oj_default_options;
|
|
184
|
+
pi.handler = Qnil;
|
|
185
|
+
pi.err_class = Qnil;
|
|
186
|
+
oj_set_strict_callbacks(&pi);
|
|
187
|
+
|
|
188
|
+
return oj_pi_parse(argc, argv, &pi, json, len, true);
|
|
189
|
+
}
|