oj 3.7.4 → 3.13.21
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 +4 -4
- data/CHANGELOG.md +1352 -0
- data/README.md +29 -8
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +53 -72
- data/ext/oj/cache.c +326 -0
- data/ext/oj/cache.h +21 -0
- data/ext/oj/cache8.c +61 -64
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +37 -43
- data/ext/oj/circarray.h +16 -17
- data/ext/oj/code.c +165 -179
- data/ext/oj/code.h +27 -29
- data/ext/oj/compat.c +174 -194
- data/ext/oj/custom.c +809 -866
- data/ext/oj/debug.c +132 -0
- data/ext/oj/dump.c +848 -863
- data/ext/oj/dump.h +81 -67
- data/ext/oj/dump_compat.c +85 -123
- data/ext/oj/dump_leaf.c +100 -188
- data/ext/oj/dump_object.c +527 -656
- data/ext/oj/dump_strict.c +315 -338
- data/ext/oj/encode.h +7 -34
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +40 -29
- data/ext/oj/err.h +48 -48
- data/ext/oj/extconf.rb +17 -4
- data/ext/oj/fast.c +1070 -1087
- data/ext/oj/intern.c +301 -0
- data/ext/oj/intern.h +26 -0
- data/ext/oj/mimic_json.c +469 -436
- data/ext/oj/object.c +525 -593
- data/ext/oj/odd.c +154 -138
- data/ext/oj/odd.h +37 -38
- data/ext/oj/oj.c +1325 -986
- data/ext/oj/oj.h +333 -316
- data/ext/oj/parse.c +1002 -846
- data/ext/oj/parse.h +92 -87
- data/ext/oj/parser.c +1557 -0
- data/ext/oj/parser.h +91 -0
- data/ext/oj/rails.c +888 -878
- data/ext/oj/rails.h +11 -14
- data/ext/oj/reader.c +141 -147
- data/ext/oj/reader.h +73 -89
- data/ext/oj/resolve.c +41 -62
- data/ext/oj/resolve.h +7 -9
- data/ext/oj/rxclass.c +71 -75
- data/ext/oj/rxclass.h +18 -19
- data/ext/oj/saj.c +443 -486
- data/ext/oj/saj2.c +602 -0
- data/ext/oj/scp.c +88 -113
- data/ext/oj/sparse.c +787 -709
- data/ext/oj/stream_writer.c +133 -159
- data/ext/oj/strict.c +127 -118
- data/ext/oj/string_writer.c +230 -249
- data/ext/oj/trace.c +34 -41
- data/ext/oj/trace.h +19 -19
- data/ext/oj/usual.c +1254 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +59 -67
- data/ext/oj/val_stack.h +91 -129
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +342 -353
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +5 -4
- data/lib/oj/error.rb +1 -1
- data/lib/oj/json.rb +1 -1
- data/lib/oj/mimic.rb +48 -14
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/state.rb +8 -7
- data/lib/oj/version.rb +2 -2
- data/lib/oj.rb +0 -8
- data/pages/Compatibility.md +1 -1
- data/pages/JsonGem.md +15 -0
- data/pages/Modes.md +53 -46
- data/pages/Options.md +72 -11
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +73 -22
- data/pages/Security.md +1 -1
- data/test/activerecord/result_test.rb +7 -2
- data/test/activesupport5/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +68 -60
- data/test/activesupport5/encoding_test.rb +111 -96
- data/test/activesupport5/encoding_test_cases.rb +33 -25
- data/test/activesupport5/test_helper.rb +43 -21
- data/test/activesupport5/time_zone_test_helpers.rb +18 -3
- data/test/activesupport6/abstract_unit.rb +44 -0
- data/test/activesupport6/decoding_test.rb +133 -0
- data/test/activesupport6/encoding_test.rb +507 -0
- data/test/activesupport6/encoding_test_cases.rb +98 -0
- data/test/activesupport6/test_common.rb +17 -0
- data/test/activesupport6/test_helper.rb +163 -0
- data/test/activesupport6/time_zone_test_helpers.rb +39 -0
- data/test/activesupport7/abstract_unit.rb +49 -0
- data/test/activesupport7/decoding_test.rb +125 -0
- data/test/activesupport7/encoding_test.rb +486 -0
- data/test/activesupport7/encoding_test_cases.rb +104 -0
- data/test/activesupport7/time_zone_test_helpers.rb +47 -0
- data/test/bar.rb +6 -12
- data/test/baz.rb +16 -0
- data/test/bug.rb +16 -0
- data/test/foo.rb +69 -75
- data/test/helper.rb +16 -0
- data/test/json_gem/json_common_interface_test.rb +8 -3
- data/test/json_gem/json_generator_test.rb +18 -4
- data/test/json_gem/json_parser_test.rb +9 -0
- data/test/json_gem/test_helper.rb +12 -0
- data/test/mem.rb +33 -0
- data/test/perf.rb +1 -1
- data/test/perf_dump.rb +50 -0
- data/test/perf_once.rb +58 -0
- data/test/perf_parser.rb +189 -0
- data/test/perf_scp.rb +11 -10
- data/test/perf_strict.rb +17 -23
- data/test/prec.rb +23 -0
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +46 -10
- data/test/test_custom.rb +147 -8
- data/test/test_fast.rb +62 -2
- data/test/test_file.rb +25 -2
- data/test/test_gc.rb +13 -0
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +11 -1
- data/test/test_integer_range.rb +7 -2
- data/test/test_object.rb +85 -9
- data/test/test_parser.rb +27 -0
- data/test/test_parser_saj.rb +335 -0
- data/test/test_parser_usual.rb +217 -0
- data/test/test_rails.rb +35 -0
- data/test/test_saj.rb +1 -1
- data/test/test_scp.rb +5 -5
- data/test/test_strict.rb +26 -1
- data/test/test_various.rb +87 -65
- data/test/test_wab.rb +2 -0
- data/test/test_writer.rb +19 -2
- data/test/tests.rb +1 -1
- data/test/zoo.rb +13 -0
- metadata +60 -110
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
data/ext/oj/stream_writer.c
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* All rights reserved.
|
|
4
|
-
*/
|
|
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.
|
|
5
3
|
|
|
6
4
|
#include <errno.h>
|
|
7
|
-
|
|
8
5
|
#include <ruby.h>
|
|
9
6
|
|
|
10
|
-
#include "
|
|
7
|
+
#include "encode.h"
|
|
11
8
|
|
|
12
|
-
extern VALUE
|
|
9
|
+
extern VALUE Oj;
|
|
13
10
|
|
|
14
|
-
static void
|
|
15
|
-
|
|
16
|
-
StreamWriter sw;
|
|
11
|
+
static void stream_writer_free(void *ptr) {
|
|
12
|
+
StreamWriter sw;
|
|
17
13
|
|
|
18
14
|
if (0 == ptr) {
|
|
19
|
-
|
|
15
|
+
return;
|
|
20
16
|
}
|
|
21
17
|
sw = (StreamWriter)ptr;
|
|
22
18
|
xfree(sw->sw.out.buf);
|
|
@@ -24,42 +20,45 @@ stream_writer_free(void *ptr) {
|
|
|
24
20
|
xfree(ptr);
|
|
25
21
|
}
|
|
26
22
|
|
|
27
|
-
static void
|
|
28
|
-
|
|
29
|
-
sw->sw.out.cur = sw->sw.out.buf;
|
|
23
|
+
static void stream_writer_reset_buf(StreamWriter sw) {
|
|
24
|
+
sw->sw.out.cur = sw->sw.out.buf;
|
|
30
25
|
*sw->sw.out.cur = '\0';
|
|
31
26
|
}
|
|
32
27
|
|
|
33
|
-
static void
|
|
34
|
-
|
|
35
|
-
ssize_t size = sw->sw.out.cur - sw->sw.out.buf;
|
|
28
|
+
static void stream_writer_write(StreamWriter sw) {
|
|
29
|
+
ssize_t size = sw->sw.out.cur - sw->sw.out.buf;
|
|
36
30
|
|
|
37
31
|
switch (sw->type) {
|
|
38
32
|
case STRING_IO:
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
case STREAM_IO: {
|
|
34
|
+
volatile VALUE rs = rb_str_new(sw->sw.out.buf, size);
|
|
35
|
+
|
|
36
|
+
// Oddly enough, when pushing ASCII characters with UTF-8 encoding or
|
|
37
|
+
// even ASCII-8BIT does not change the output encoding. Pushing any
|
|
38
|
+
// non-ASCII no matter what the encoding changes the output encoding
|
|
39
|
+
// to ASCII-8BIT if it the string is not forced to UTF-8 here.
|
|
40
|
+
rs = oj_encode(rs);
|
|
41
|
+
rb_funcall(sw->stream, oj_write_id, 1, rs);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
44
|
case FILE_IO:
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
default:
|
|
50
|
-
rb_raise(rb_eArgError, "expected an IO Object.");
|
|
45
|
+
if (size != write(sw->fd, sw->sw.out.buf, size)) {
|
|
46
|
+
rb_raise(rb_eIOError, "Write failed. [_%d_:%s]\n", errno, strerror(errno));
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
default: rb_raise(rb_eArgError, "expected an IO Object.");
|
|
51
50
|
}
|
|
52
51
|
stream_writer_reset_buf(sw);
|
|
53
52
|
}
|
|
54
53
|
|
|
55
|
-
static VALUE
|
|
54
|
+
static VALUE buffer_size_sym = Qundef;
|
|
56
55
|
|
|
57
56
|
/* Document-method: new
|
|
58
57
|
* call-seq: new(io, options)
|
|
59
58
|
*
|
|
60
|
-
* Creates a new StreamWriter. Options are supported according the
|
|
61
|
-
*
|
|
62
|
-
*
|
|
59
|
+
* Creates a new StreamWriter. Options are supported according the specified
|
|
60
|
+
* mode or the mode in the default options. Note that if mimic_JSON or
|
|
61
|
+
* Oj.optimize_rails has not been called then the behavior of the modes may
|
|
63
62
|
* not be the same as if they were.
|
|
64
63
|
*
|
|
65
64
|
* In addition to the regular dump options for the various modes a
|
|
@@ -68,64 +67,62 @@ static VALUE buffer_size_sym = Qundef;
|
|
|
68
67
|
* should be and also a hint on when to flush.
|
|
69
68
|
*
|
|
70
69
|
* - *io* [_IO_] stream to write to
|
|
71
|
-
* - *options* [_Hash_]
|
|
70
|
+
* - *options* [_Hash_] formatting options
|
|
72
71
|
*/
|
|
73
|
-
static VALUE
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
VALUE
|
|
78
|
-
|
|
79
|
-
StreamWriter sw;
|
|
72
|
+
static VALUE stream_writer_new(int argc, VALUE *argv, VALUE self) {
|
|
73
|
+
StreamWriterType type = STREAM_IO;
|
|
74
|
+
int fd = 0;
|
|
75
|
+
VALUE stream = argv[0];
|
|
76
|
+
VALUE clas = rb_obj_class(stream);
|
|
77
|
+
StreamWriter sw;
|
|
80
78
|
#if !IS_WINDOWS
|
|
81
|
-
VALUE
|
|
79
|
+
VALUE s;
|
|
82
80
|
#endif
|
|
83
|
-
|
|
81
|
+
|
|
84
82
|
if (oj_stringio_class == clas) {
|
|
85
|
-
|
|
83
|
+
type = STRING_IO;
|
|
86
84
|
#if !IS_WINDOWS
|
|
87
85
|
} else if (rb_respond_to(stream, oj_fileno_id) &&
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
type = FILE_IO;
|
|
86
|
+
Qnil != (s = rb_funcall(stream, oj_fileno_id, 0)) && 0 != (fd = FIX2INT(s))) {
|
|
87
|
+
type = FILE_IO;
|
|
91
88
|
#endif
|
|
92
89
|
} else if (rb_respond_to(stream, oj_write_id)) {
|
|
93
|
-
|
|
90
|
+
type = STREAM_IO;
|
|
94
91
|
} else {
|
|
95
|
-
|
|
92
|
+
rb_raise(rb_eArgError, "expected an IO Object.");
|
|
96
93
|
}
|
|
97
|
-
sw = ALLOC(struct
|
|
94
|
+
sw = ALLOC(struct _streamWriter);
|
|
98
95
|
if (2 == argc && T_HASH == rb_type(argv[1])) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
96
|
+
volatile VALUE v;
|
|
97
|
+
int buf_size = 0;
|
|
98
|
+
|
|
99
|
+
if (Qundef == buffer_size_sym) {
|
|
100
|
+
buffer_size_sym = ID2SYM(rb_intern("buffer_size"));
|
|
101
|
+
rb_gc_register_address(&buffer_size_sym);
|
|
102
|
+
}
|
|
103
|
+
if (Qnil != (v = rb_hash_lookup(argv[1], buffer_size_sym))) {
|
|
107
104
|
#ifdef RUBY_INTEGER_UNIFICATION
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
105
|
+
if (rb_cInteger != rb_obj_class(v)) {
|
|
106
|
+
rb_raise(rb_eArgError, ":buffer size must be a Integer.");
|
|
107
|
+
}
|
|
111
108
|
#else
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
109
|
+
if (T_FIXNUM != rb_type(v)) {
|
|
110
|
+
rb_raise(rb_eArgError, ":buffer size must be a Integer.");
|
|
111
|
+
}
|
|
115
112
|
#endif
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
113
|
+
buf_size = FIX2INT(v);
|
|
114
|
+
}
|
|
115
|
+
oj_str_writer_init(&sw->sw, buf_size);
|
|
116
|
+
oj_parse_options(argv[1], &sw->sw.opts);
|
|
117
|
+
sw->flush_limit = buf_size;
|
|
121
118
|
} else {
|
|
122
|
-
|
|
123
|
-
|
|
119
|
+
oj_str_writer_init(&sw->sw, 4096);
|
|
120
|
+
sw->flush_limit = 0;
|
|
124
121
|
}
|
|
125
122
|
sw->sw.out.indent = sw->sw.opts.indent;
|
|
126
|
-
sw->stream
|
|
127
|
-
sw->type
|
|
128
|
-
sw->fd
|
|
123
|
+
sw->stream = stream;
|
|
124
|
+
sw->type = type;
|
|
125
|
+
sw->fd = fd;
|
|
129
126
|
|
|
130
127
|
return Data_Wrap_Struct(oj_stream_writer_class, 0, stream_writer_free, sw);
|
|
131
128
|
}
|
|
@@ -139,14 +136,13 @@ stream_writer_new(int argc, VALUE *argv, VALUE self) {
|
|
|
139
136
|
*
|
|
140
137
|
* - *key* [_String_] the key pending for the next push
|
|
141
138
|
*/
|
|
142
|
-
static VALUE
|
|
143
|
-
|
|
144
|
-
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
139
|
+
static VALUE stream_writer_push_key(VALUE self, VALUE key) {
|
|
140
|
+
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
145
141
|
|
|
146
142
|
rb_check_type(key, T_STRING);
|
|
147
143
|
oj_str_writer_push_key(&sw->sw, StringValuePtr(key));
|
|
148
144
|
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
149
|
-
|
|
145
|
+
stream_writer_write(sw);
|
|
150
146
|
}
|
|
151
147
|
return Qnil;
|
|
152
148
|
}
|
|
@@ -159,28 +155,23 @@ stream_writer_push_key(VALUE self, VALUE key) {
|
|
|
159
155
|
*
|
|
160
156
|
* - *key* [_String_] the key if adding to an object in the JSON document
|
|
161
157
|
*/
|
|
162
|
-
static VALUE
|
|
163
|
-
|
|
164
|
-
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
158
|
+
static VALUE stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
|
|
159
|
+
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
165
160
|
|
|
166
161
|
switch (argc) {
|
|
167
|
-
case 0:
|
|
168
|
-
oj_str_writer_push_object(&sw->sw, 0);
|
|
169
|
-
break;
|
|
162
|
+
case 0: oj_str_writer_push_object(&sw->sw, 0); break;
|
|
170
163
|
case 1:
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
default:
|
|
179
|
-
rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
|
|
180
|
-
break;
|
|
164
|
+
if (Qnil == argv[0]) {
|
|
165
|
+
oj_str_writer_push_object(&sw->sw, 0);
|
|
166
|
+
} else {
|
|
167
|
+
rb_check_type(argv[0], T_STRING);
|
|
168
|
+
oj_str_writer_push_object(&sw->sw, StringValuePtr(argv[0]));
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
|
|
181
172
|
}
|
|
182
173
|
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
183
|
-
|
|
174
|
+
stream_writer_write(sw);
|
|
184
175
|
}
|
|
185
176
|
return Qnil;
|
|
186
177
|
}
|
|
@@ -193,28 +184,23 @@ stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
|
|
|
193
184
|
*
|
|
194
185
|
* - *key* [_String_] the key if adding to an object in the JSON document
|
|
195
186
|
*/
|
|
196
|
-
static VALUE
|
|
197
|
-
|
|
198
|
-
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
187
|
+
static VALUE stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
|
|
188
|
+
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
199
189
|
|
|
200
190
|
switch (argc) {
|
|
201
|
-
case 0:
|
|
202
|
-
oj_str_writer_push_array(&sw->sw, 0);
|
|
203
|
-
break;
|
|
191
|
+
case 0: oj_str_writer_push_array(&sw->sw, 0); break;
|
|
204
192
|
case 1:
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
default:
|
|
213
|
-
rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
|
|
214
|
-
break;
|
|
193
|
+
if (Qnil == argv[0]) {
|
|
194
|
+
oj_str_writer_push_array(&sw->sw, 0);
|
|
195
|
+
} else {
|
|
196
|
+
rb_check_type(argv[0], T_STRING);
|
|
197
|
+
oj_str_writer_push_array(&sw->sw, StringValuePtr(argv[0]));
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
|
|
215
201
|
}
|
|
216
202
|
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
217
|
-
|
|
203
|
+
stream_writer_write(sw);
|
|
218
204
|
}
|
|
219
205
|
return Qnil;
|
|
220
206
|
}
|
|
@@ -226,28 +212,23 @@ stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
|
|
|
226
212
|
* - *value* [_Object_] value to add to the JSON document
|
|
227
213
|
* - *key* [_String_] the key if adding to an object in the JSON document
|
|
228
214
|
*/
|
|
229
|
-
static VALUE
|
|
230
|
-
|
|
231
|
-
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
215
|
+
static VALUE stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
|
|
216
|
+
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
232
217
|
|
|
233
218
|
switch (argc) {
|
|
234
|
-
case 1:
|
|
235
|
-
oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
|
|
236
|
-
break;
|
|
219
|
+
case 1: oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0); break;
|
|
237
220
|
case 2:
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
default:
|
|
246
|
-
rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'.");
|
|
247
|
-
break;
|
|
221
|
+
if (Qnil == argv[1]) {
|
|
222
|
+
oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
|
|
223
|
+
} else {
|
|
224
|
+
rb_check_type(argv[1], T_STRING);
|
|
225
|
+
oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, StringValuePtr(argv[1]));
|
|
226
|
+
}
|
|
227
|
+
break;
|
|
228
|
+
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
|
|
248
229
|
}
|
|
249
230
|
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
250
|
-
|
|
231
|
+
stream_writer_write(sw);
|
|
251
232
|
}
|
|
252
233
|
return Qnil;
|
|
253
234
|
}
|
|
@@ -261,29 +242,26 @@ stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
|
|
|
261
242
|
* - *value* [_Object_] value to add to the JSON document
|
|
262
243
|
* - *key* [_String_] the key if adding to an object in the JSON document
|
|
263
244
|
*/
|
|
264
|
-
static VALUE
|
|
265
|
-
|
|
266
|
-
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
245
|
+
static VALUE stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
|
|
246
|
+
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
267
247
|
|
|
268
248
|
rb_check_type(argv[0], T_STRING);
|
|
269
249
|
switch (argc) {
|
|
270
|
-
case 1:
|
|
271
|
-
oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
|
|
272
|
-
break;
|
|
250
|
+
case 1: oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0); break;
|
|
273
251
|
case 2:
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
252
|
+
if (Qnil == argv[1]) {
|
|
253
|
+
oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
|
|
254
|
+
} else {
|
|
255
|
+
rb_check_type(argv[1], T_STRING);
|
|
256
|
+
oj_str_writer_push_json((StrWriter)DATA_PTR(self),
|
|
257
|
+
StringValuePtr(*argv),
|
|
258
|
+
StringValuePtr(argv[1]));
|
|
259
|
+
}
|
|
260
|
+
break;
|
|
261
|
+
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
|
|
284
262
|
}
|
|
285
263
|
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
286
|
-
|
|
264
|
+
stream_writer_write(sw);
|
|
287
265
|
}
|
|
288
266
|
return Qnil;
|
|
289
267
|
}
|
|
@@ -294,13 +272,12 @@ stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
|
|
|
294
272
|
* Pops up a level in the JSON document closing the array or object that is
|
|
295
273
|
* currently open.
|
|
296
274
|
*/
|
|
297
|
-
static VALUE
|
|
298
|
-
|
|
299
|
-
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
275
|
+
static VALUE stream_writer_pop(VALUE self) {
|
|
276
|
+
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
300
277
|
|
|
301
278
|
oj_str_writer_pop(&sw->sw);
|
|
302
279
|
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
|
|
303
|
-
|
|
280
|
+
stream_writer_write(sw);
|
|
304
281
|
}
|
|
305
282
|
return Qnil;
|
|
306
283
|
}
|
|
@@ -311,9 +288,8 @@ stream_writer_pop(VALUE self) {
|
|
|
311
288
|
* Pops all level in the JSON document closing all the array or object that is
|
|
312
289
|
* currently open.
|
|
313
290
|
*/
|
|
314
|
-
static VALUE
|
|
315
|
-
|
|
316
|
-
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
291
|
+
static VALUE stream_writer_pop_all(VALUE self) {
|
|
292
|
+
StreamWriter sw = (StreamWriter)DATA_PTR(self);
|
|
317
293
|
|
|
318
294
|
oj_str_writer_pop_all(&sw->sw);
|
|
319
295
|
stream_writer_write(sw);
|
|
@@ -326,23 +302,23 @@ stream_writer_pop_all(VALUE self) {
|
|
|
326
302
|
*
|
|
327
303
|
* Flush any remaining characters in the buffer.
|
|
328
304
|
*/
|
|
329
|
-
static VALUE
|
|
330
|
-
stream_writer_flush(VALUE self) {
|
|
305
|
+
static VALUE stream_writer_flush(VALUE self) {
|
|
331
306
|
stream_writer_write((StreamWriter)DATA_PTR(self));
|
|
332
307
|
|
|
333
308
|
return Qnil;
|
|
334
309
|
}
|
|
335
310
|
|
|
336
311
|
/* Document-class: Oj::StreamWriter
|
|
337
|
-
*
|
|
312
|
+
*
|
|
338
313
|
* Supports building a JSON document one element at a time. Build the IO stream
|
|
339
314
|
* document by pushing values into the document. Pushing an array or an object
|
|
340
315
|
* will create that element in the JSON document and subsequent pushes will add
|
|
341
316
|
* the elements to that array or object until a pop() is called.
|
|
342
317
|
*/
|
|
343
|
-
void
|
|
344
|
-
oj_stream_writer_init() {
|
|
318
|
+
void oj_stream_writer_init(void) {
|
|
345
319
|
oj_stream_writer_class = rb_define_class_under(Oj, "StreamWriter", rb_cObject);
|
|
320
|
+
rb_gc_register_address(&oj_stream_writer_class);
|
|
321
|
+
rb_undef_alloc_func(oj_stream_writer_class);
|
|
346
322
|
rb_define_module_function(oj_stream_writer_class, "new", stream_writer_new, -1);
|
|
347
323
|
rb_define_method(oj_stream_writer_class, "push_key", stream_writer_push_key, 1);
|
|
348
324
|
rb_define_method(oj_stream_writer_class, "push_object", stream_writer_push_object, -1);
|
|
@@ -353,5 +329,3 @@ oj_stream_writer_init() {
|
|
|
353
329
|
rb_define_method(oj_stream_writer_class, "pop_all", stream_writer_pop_all, 0);
|
|
354
330
|
rb_define_method(oj_stream_writer_class, "flush", stream_writer_flush, 0);
|
|
355
331
|
}
|
|
356
|
-
|
|
357
|
-
|