Class: Oj::StreamWriter
- Inherits:
-
Object
- Object
- Oj::StreamWriter
- Defined in:
- ext/oj/stream_writer.c,
ext/oj/stream_writer.c
Overview
Supports building a JSON document one element at a time. Build the IO stream document by pushing values into the document. Pushing an array or an object will create that element in the JSON document and subsequent pushes will add the elements to that array or object until a pop() is called.
Class Method Summary collapse
Instance Method Summary collapse
- #flush ⇒ Object
- #pop ⇒ Object
- #pop_all ⇒ Object
- #push_array(*args) ⇒ Object
- #push_json(*args) ⇒ Object
- #push_key(key) ⇒ Object
- #push_object(*args) ⇒ Object
- #push_value(*args) ⇒ Object
Class Method Details
.new(*args) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'ext/oj/stream_writer.c', line 88
static VALUE stream_writer_new(int argc, VALUE *argv, VALUE self) {
StreamWriterType type = STREAM_IO;
int fd = 0;
VALUE stream = argv[0];
VALUE clas = rb_obj_class(stream);
StreamWriter sw;
#if !IS_WINDOWS
VALUE s;
#endif
if (oj_stringio_class == clas) {
type = STRING_IO;
#if !IS_WINDOWS
} else if (rb_respond_to(stream, oj_fileno_id) && Qnil != (s = rb_funcall(stream, oj_fileno_id, 0)) &&
0 != (fd = FIX2INT(s))) {
type = FILE_IO;
#endif
} else if (rb_respond_to(stream, oj_write_id)) {
type = STREAM_IO;
} else {
rb_raise(rb_eArgError, "expected an IO Object.");
}
sw = OJ_R_ALLOC(struct _streamWriter);
if (2 == argc && T_HASH == rb_type(argv[1])) {
volatile VALUE v;
int buf_size = 0;
if (Qundef == buffer_size_sym) {
buffer_size_sym = ID2SYM(rb_intern("buffer_size"));
rb_gc_register_address(&buffer_size_sym);
}
if (Qnil != (v = rb_hash_lookup(argv[1], buffer_size_sym))) {
if (rb_cInteger != rb_obj_class(v)) {
OJ_R_FREE(sw);
rb_raise(rb_eArgError, ":buffer size must be a Integer.");
}
buf_size = FIX2INT(v);
}
oj_str_writer_init(&sw->sw, buf_size);
oj_parse_options(argv[1], &sw->sw.opts);
sw->flush_limit = buf_size;
} else {
oj_str_writer_init(&sw->sw, 4096);
sw->flush_limit = 0;
}
oj_options_take_ownership(&sw->sw.opts);
sw->sw.out.indent = sw->sw.opts.indent;
sw->stream = stream;
sw->type = type;
sw->fd = fd;
return TypedData_Wrap_Struct(oj_stream_writer_class, &oj_stream_writer_type, sw);
}
|
Instance Method Details
#flush ⇒ Object
316 317 318 319 320 321 322 |
# File 'ext/oj/stream_writer.c', line 316
static VALUE stream_writer_flush(VALUE self) {
StreamWriter sw;
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
stream_writer_write(sw);
return Qnil;
}
|
#pop ⇒ Object
284 285 286 287 288 289 290 291 292 293 |
# File 'ext/oj/stream_writer.c', line 284
static VALUE stream_writer_pop(VALUE self) {
StreamWriter sw;
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
oj_str_writer_pop(&sw->sw);
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
stream_writer_write(sw);
}
return Qnil;
}
|
#pop_all ⇒ Object
301 302 303 304 305 306 307 308 309 |
# File 'ext/oj/stream_writer.c', line 301
static VALUE stream_writer_pop_all(VALUE self) {
StreamWriter sw;
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
oj_str_writer_pop_all(&sw->sw);
stream_writer_write(sw);
return Qnil;
}
|
#push_array(*args) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'ext/oj/stream_writer.c', line 199
static VALUE stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
StreamWriter sw;
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
switch (argc) {
case 0: oj_str_writer_push_array(&sw->sw, 0); break;
case 1:
if (Qnil == argv[0]) {
oj_str_writer_push_array(&sw->sw, 0);
} else {
oj_str_writer_push_array(&sw->sw, StringValuePtr(argv[0]));
}
break;
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
}
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
stream_writer_write(sw);
}
return Qnil;
}
|
#push_json(*args) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'ext/oj/stream_writer.c', line 257
static VALUE stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
StreamWriter sw;
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
switch (argc) {
case 1: oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0); break;
case 2:
if (Qnil == argv[1]) {
oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0);
} else {
oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), StringValuePtr(argv[1]));
}
break;
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
}
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
stream_writer_write(sw);
}
return Qnil;
}
|
#push_key(key) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'ext/oj/stream_writer.c', line 151
static VALUE stream_writer_push_key(VALUE self, VALUE key) {
StreamWriter sw;
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
oj_str_writer_push_key(&sw->sw, StringValuePtr(key));
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
stream_writer_write(sw);
}
return Qnil;
}
|
#push_object(*args) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'ext/oj/stream_writer.c', line 170
static VALUE stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
StreamWriter sw;
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
switch (argc) {
case 0: oj_str_writer_push_object(&sw->sw, 0); break;
case 1:
if (Qnil == argv[0]) {
oj_str_writer_push_object(&sw->sw, 0);
} else {
oj_str_writer_push_object(&sw->sw, StringValuePtr(argv[0]));
}
break;
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
}
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
stream_writer_write(sw);
}
return Qnil;
}
|
#push_value(*args) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'ext/oj/stream_writer.c', line 227
static VALUE stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
StreamWriter sw;
TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
switch (argc) {
case 1: oj_str_writer_push_value((StrWriter)sw, *argv, 0); break;
case 2:
if (Qnil == argv[1]) {
oj_str_writer_push_value((StrWriter)sw, *argv, 0);
} else {
oj_str_writer_push_value((StrWriter)sw, *argv, StringValuePtr(argv[1]));
}
break;
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
}
if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
stream_writer_write(sw);
}
return Qnil;
}
|