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