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
@@ -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/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
|
+
}
|
data/ext/oj/strict.c
CHANGED
@@ -1,185 +1,187 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
* All rights reserved.
|
4
|
-
*
|
5
|
-
* Redistribution and use in source and binary forms, with or without
|
6
|
-
* modification, are permitted provided that the following conditions are met:
|
7
|
-
*
|
8
|
-
* - Redistributions of source code must retain the above copyright notice, this
|
9
|
-
* list of conditions and the following disclaimer.
|
10
|
-
*
|
11
|
-
* - Redistributions in binary form must reproduce the above copyright notice,
|
12
|
-
* this list of conditions and the following disclaimer in the documentation
|
13
|
-
* and/or other materials provided with the distribution.
|
14
|
-
*
|
15
|
-
* - Neither the name of Peter Ohler nor the names of its contributors may be
|
16
|
-
* used to endorse or promote products derived from this software without
|
17
|
-
* specific prior written permission.
|
18
|
-
*
|
19
|
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
-
*/
|
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.
|
30
3
|
|
31
|
-
#include <stdlib.h>
|
32
4
|
#include <stdio.h>
|
5
|
+
#include <stdlib.h>
|
33
6
|
#include <string.h>
|
34
7
|
#include <unistd.h>
|
35
8
|
|
36
|
-
#include "
|
9
|
+
#include "encode.h"
|
37
10
|
#include "err.h"
|
11
|
+
#include "intern.h"
|
12
|
+
#include "oj.h"
|
38
13
|
#include "parse.h"
|
39
|
-
#include "
|
14
|
+
#include "trace.h"
|
40
15
|
|
41
|
-
|
42
|
-
|
16
|
+
VALUE oj_cstr_to_value(const char *str, size_t len, size_t cache_str) {
|
17
|
+
volatile VALUE rstr = Qnil;
|
18
|
+
|
19
|
+
if (len < cache_str) {
|
20
|
+
rstr = oj_str_intern(str, len);
|
21
|
+
} else {
|
22
|
+
rstr = rb_utf8_str_new(str, len);
|
23
|
+
}
|
24
|
+
return rstr;
|
43
25
|
}
|
44
26
|
|
45
|
-
|
46
|
-
|
27
|
+
VALUE oj_calc_hash_key(ParseInfo pi, Val parent) {
|
28
|
+
volatile VALUE rkey = parent->key_val;
|
29
|
+
|
30
|
+
if (Qundef != rkey) {
|
31
|
+
return rkey;
|
32
|
+
}
|
33
|
+
if (Yes != pi->options.cache_keys) {
|
34
|
+
if (Yes == pi->options.sym_key) {
|
35
|
+
rkey = ID2SYM(rb_intern3(parent->key, parent->klen, oj_utf8_encoding));
|
36
|
+
} else {
|
37
|
+
rkey = rb_utf8_str_new(parent->key, parent->klen);
|
38
|
+
OBJ_FREEZE(rkey); // frozen when used as a Hash key anyway
|
39
|
+
}
|
40
|
+
return rkey;
|
41
|
+
}
|
42
|
+
if (Yes == pi->options.sym_key) {
|
43
|
+
rkey = oj_sym_intern(parent->key, parent->klen);
|
44
|
+
} else {
|
45
|
+
rkey = oj_str_intern(parent->key, parent->klen);
|
46
|
+
}
|
47
|
+
return rkey;
|
48
|
+
}
|
49
|
+
|
50
|
+
static void hash_end(ParseInfo pi) {
|
51
|
+
TRACE_PARSE_HASH_END(pi->options.trace, pi);
|
52
|
+
}
|
53
|
+
|
54
|
+
static void array_end(ParseInfo pi) {
|
55
|
+
TRACE_PARSE_ARRAY_END(pi->options.trace, pi);
|
56
|
+
}
|
57
|
+
|
58
|
+
static VALUE noop_hash_key(ParseInfo pi, const char *key, size_t klen) {
|
47
59
|
return Qundef;
|
48
60
|
}
|
49
61
|
|
50
|
-
static void
|
51
|
-
|
62
|
+
static void add_value(ParseInfo pi, VALUE val) {
|
63
|
+
TRACE_PARSE_CALL(pi->options.trace, "add_value", pi, val);
|
52
64
|
pi->stack.head->val = val;
|
53
65
|
}
|
54
66
|
|
55
|
-
static void
|
56
|
-
|
57
|
-
volatile VALUE rstr = rb_str_new(str, len);
|
67
|
+
static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
|
68
|
+
volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
|
58
69
|
|
59
|
-
rstr = oj_encode(rstr);
|
60
70
|
pi->stack.head->val = rstr;
|
71
|
+
TRACE_PARSE_CALL(pi->options.trace, "add_string", pi, rstr);
|
61
72
|
}
|
62
73
|
|
63
|
-
static void
|
64
|
-
add_num(ParseInfo pi, NumInfo ni) {
|
74
|
+
static void add_num(ParseInfo pi, NumInfo ni) {
|
65
75
|
if (ni->infinity || ni->nan) {
|
66
|
-
|
76
|
+
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
|
67
77
|
}
|
68
78
|
pi->stack.head->val = oj_num_as_value(ni);
|
79
|
+
TRACE_PARSE_CALL(pi->options.trace, "add_number", pi, pi->stack.head->val);
|
69
80
|
}
|
70
81
|
|
71
|
-
static VALUE
|
72
|
-
start_hash(ParseInfo pi) {
|
82
|
+
static VALUE start_hash(ParseInfo pi) {
|
73
83
|
if (Qnil != pi->options.hash_class) {
|
74
|
-
|
84
|
+
return rb_class_new_instance(0, NULL, pi->options.hash_class);
|
75
85
|
}
|
86
|
+
TRACE_PARSE_IN(pi->options.trace, "start_hash", pi);
|
76
87
|
return rb_hash_new();
|
77
88
|
}
|
78
89
|
|
79
|
-
static
|
80
|
-
|
81
|
-
volatile VALUE rkey = parent->key_val;
|
90
|
+
static void hash_set_cstr(ParseInfo pi, Val parent, const char *str, size_t len, const char *orig) {
|
91
|
+
volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
|
82
92
|
|
83
|
-
|
84
|
-
|
85
|
-
}
|
86
|
-
rkey = oj_encode(rkey);
|
87
|
-
if (Yes == pi->options.sym_key) {
|
88
|
-
rkey = rb_str_intern(rkey);
|
89
|
-
}
|
90
|
-
return rkey;
|
93
|
+
rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), rstr);
|
94
|
+
TRACE_PARSE_CALL(pi->options.trace, "set_string", pi, rstr);
|
91
95
|
}
|
92
96
|
|
93
|
-
static void
|
94
|
-
|
95
|
-
volatile VALUE rstr = rb_str_new(str, len);
|
96
|
-
|
97
|
-
rstr = oj_encode(rstr);
|
98
|
-
rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), rstr);
|
99
|
-
}
|
97
|
+
static void hash_set_num(ParseInfo pi, Val parent, NumInfo ni) {
|
98
|
+
volatile VALUE v;
|
100
99
|
|
101
|
-
static void
|
102
|
-
hash_set_num(struct _ParseInfo *pi, Val parent, NumInfo ni) {
|
103
100
|
if (ni->infinity || ni->nan) {
|
104
|
-
|
101
|
+
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
|
105
102
|
}
|
106
|
-
|
103
|
+
v = oj_num_as_value(ni);
|
104
|
+
rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), v);
|
105
|
+
TRACE_PARSE_CALL(pi->options.trace, "set_number", pi, v);
|
107
106
|
}
|
108
107
|
|
109
|
-
static void
|
110
|
-
|
111
|
-
|
108
|
+
static void hash_set_value(ParseInfo pi, Val parent, VALUE value) {
|
109
|
+
rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), value);
|
110
|
+
TRACE_PARSE_CALL(pi->options.trace, "set_value", pi, value);
|
112
111
|
}
|
113
112
|
|
114
|
-
static VALUE
|
115
|
-
|
113
|
+
static VALUE start_array(ParseInfo pi) {
|
114
|
+
TRACE_PARSE_IN(pi->options.trace, "start_array", pi);
|
116
115
|
return rb_ary_new();
|
117
116
|
}
|
118
117
|
|
119
|
-
static void
|
120
|
-
|
121
|
-
volatile VALUE rstr = rb_str_new(str, len);
|
118
|
+
static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
|
119
|
+
volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
|
122
120
|
|
123
|
-
rstr = oj_encode(rstr);
|
124
121
|
rb_ary_push(stack_peek(&pi->stack)->val, rstr);
|
122
|
+
TRACE_PARSE_CALL(pi->options.trace, "append_string", pi, rstr);
|
125
123
|
}
|
126
124
|
|
127
|
-
static void
|
128
|
-
|
125
|
+
static void array_append_num(ParseInfo pi, NumInfo ni) {
|
126
|
+
volatile VALUE v;
|
127
|
+
|
129
128
|
if (ni->infinity || ni->nan) {
|
130
|
-
|
129
|
+
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
|
131
130
|
}
|
132
|
-
|
131
|
+
v = oj_num_as_value(ni);
|
132
|
+
rb_ary_push(stack_peek(&pi->stack)->val, v);
|
133
|
+
TRACE_PARSE_CALL(pi->options.trace, "append_number", pi, v);
|
133
134
|
}
|
134
135
|
|
135
|
-
static void
|
136
|
-
array_append_value(ParseInfo pi, VALUE value) {
|
136
|
+
static void array_append_value(ParseInfo pi, VALUE value) {
|
137
137
|
rb_ary_push(stack_peek(&pi->stack)->val, value);
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
oj_set_strict_callbacks(ParseInfo pi) {
|
142
|
-
pi->start_hash
|
143
|
-
pi->end_hash
|
144
|
-
pi->hash_key
|
145
|
-
pi->hash_set_cstr
|
146
|
-
pi->hash_set_num
|
147
|
-
pi->hash_set_value
|
148
|
-
pi->start_array
|
149
|
-
pi->end_array
|
150
|
-
pi->array_append_cstr
|
151
|
-
pi->array_append_num
|
138
|
+
TRACE_PARSE_CALL(pi->options.trace, "append_value", pi, value);
|
139
|
+
}
|
140
|
+
|
141
|
+
void oj_set_strict_callbacks(ParseInfo pi) {
|
142
|
+
pi->start_hash = start_hash;
|
143
|
+
pi->end_hash = hash_end;
|
144
|
+
pi->hash_key = noop_hash_key;
|
145
|
+
pi->hash_set_cstr = hash_set_cstr;
|
146
|
+
pi->hash_set_num = hash_set_num;
|
147
|
+
pi->hash_set_value = hash_set_value;
|
148
|
+
pi->start_array = start_array;
|
149
|
+
pi->end_array = array_end;
|
150
|
+
pi->array_append_cstr = array_append_cstr;
|
151
|
+
pi->array_append_num = array_append_num;
|
152
152
|
pi->array_append_value = array_append_value;
|
153
|
-
pi->add_cstr
|
154
|
-
pi->add_num
|
155
|
-
pi->add_value
|
156
|
-
pi->expect_value
|
153
|
+
pi->add_cstr = add_cstr;
|
154
|
+
pi->add_num = add_num;
|
155
|
+
pi->add_value = add_value;
|
156
|
+
pi->expect_value = 1;
|
157
157
|
}
|
158
158
|
|
159
159
|
VALUE
|
160
160
|
oj_strict_parse(int argc, VALUE *argv, VALUE self) {
|
161
|
-
struct
|
161
|
+
struct _parseInfo pi;
|
162
162
|
|
163
|
-
pi
|
164
|
-
pi.
|
163
|
+
parse_info_init(&pi);
|
164
|
+
pi.options = oj_default_options;
|
165
|
+
pi.handler = Qnil;
|
165
166
|
pi.err_class = Qnil;
|
166
167
|
oj_set_strict_callbacks(&pi);
|
167
168
|
|
168
169
|
if (T_STRING == rb_type(*argv)) {
|
169
|
-
|
170
|
+
return oj_pi_parse(argc, argv, &pi, 0, 0, true);
|
170
171
|
} else {
|
171
|
-
|
172
|
+
return oj_pi_sparse(argc, argv, &pi, 0);
|
172
173
|
}
|
173
174
|
}
|
174
175
|
|
175
176
|
VALUE
|
176
177
|
oj_strict_parse_cstr(int argc, VALUE *argv, char *json, size_t len) {
|
177
|
-
struct
|
178
|
+
struct _parseInfo pi;
|
178
179
|
|
179
|
-
pi
|
180
|
-
pi.
|
180
|
+
parse_info_init(&pi);
|
181
|
+
pi.options = oj_default_options;
|
182
|
+
pi.handler = Qnil;
|
181
183
|
pi.err_class = Qnil;
|
182
184
|
oj_set_strict_callbacks(&pi);
|
183
185
|
|
184
|
-
return oj_pi_parse(argc, argv, &pi, json, len,
|
186
|
+
return oj_pi_parse(argc, argv, &pi, json, len, true);
|
185
187
|
}
|