Class: Oj::StringWriter
- Inherits:
-
Object
- Object
- Oj::StringWriter
- Defined in:
- ext/oj/string_writer.c,
ext/oj/string_writer.c
Overview
Supports building a JSON document one element at a time. Build the 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. When complete calling to_s() will return the JSON document. Note that calling to_s() before construction is complete will return the document in it’s current state.
Class Method Summary collapse
-
.new(io, options) ⇒ Object
Creates a new StringWriter.
Instance Method Summary collapse
-
#as_json ⇒ Object
Returns the contents of the writer as a JSON element.
-
#pop ⇒ Object
Pops up a level in the JSON document closing the array or object that is currently open.
-
#pop_all ⇒ Object
Pops all level in the JSON document closing all the array or object that is currently open.
-
#push_array(key = nil) ⇒ Object
Pushes an array onto the JSON document.
-
#push_json(value, key = nil) ⇒ Object
Pushes a string onto the JSON document.
-
#push_key(key) ⇒ Object
Pushes a key onto the JSON document.
-
#push_object(key = nil) ⇒ Object
Pushes an object onto the JSON document.
-
#push_value(value, key = nil) ⇒ Object
Pushes a value onto the JSON document.
-
#to_s ⇒ Object
Returns the JSON document string in what ever state the construction is at.
-
#reset ⇒ Object
Reset the writer back to the empty state.
-
#to_s ⇒ Object
Returns the JSON document string in what ever state the construction is at.
Class Method Details
.new(io, options) ⇒ Object
Creates a new StringWriter. Options are supported according the the specified mode or the mode in the default options. Note that if mimic_JSON or Oj.optimize_rails has not been called then the behavior of the modes may not be the same as if they were.
In addition to the regular dump options for the various modes a :buffer_size option is available. It should be set to a positive integer. It is considered a hint of how large the initial internal buffer should be.
-
io [IO] stream to write to
-
options [Hash] formatting options
259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'ext/oj/string_writer.c', line 259
static VALUE str_writer_new(int argc, VALUE *argv, VALUE self) {
StrWriter sw = OJ_R_ALLOC(struct _strWriter);
oj_str_writer_init(sw, 0);
if (1 == argc) {
oj_parse_options(argv[0], &sw->opts);
}
sw->out.argc = argc - 1;
sw->out.argv = argv + 1;
sw->out.indent = sw->opts.indent;
return Data_Wrap_Struct(oj_string_writer_class, 0, str_writer_free, sw);
}
|
Instance Method Details
#as_json ⇒ Object
Returns the contents of the writer as a JSON element. If called from inside an array or hash by Oj the raw buffer will be used othersize a more inefficient parse of the contents and a return of the result is completed. The parse uses the strict mode.
return [Hash|Array|String|Integer|Float|True|False|_nil|)
462 463 464 465 466 467 |
# File 'ext/oj/string_writer.c', line 462
static VALUE str_writer_as_json(VALUE self) {
if (string_writer_optimized) {
return self;
}
return rb_hash_new();
}
|
#pop ⇒ Object
Pops up a level in the JSON document closing the array or object that is currently open.
404 405 406 407 |
# File 'ext/oj/string_writer.c', line 404
static VALUE str_writer_pop(VALUE self) {
oj_str_writer_pop((StrWriter)DATA_PTR(self));
return Qnil;
}
|
#pop_all ⇒ Object
Pops all level in the JSON document closing all the array or object that is currently open.
415 416 417 418 419 |
# File 'ext/oj/string_writer.c', line 415
static VALUE str_writer_pop_all(VALUE self) {
oj_str_writer_pop_all((StrWriter)DATA_PTR(self));
return Qnil;
}
|
#push_array(key = nil) ⇒ Object
Pushes an array onto the JSON document. Future pushes will be to this object until a pop() is called.
-
key [String] the key if adding to an object in the JSON document
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'ext/oj/string_writer.c', line 326
static VALUE str_writer_push_array(int argc, VALUE *argv, VALUE self) {
StrWriter sw = (StrWriter)DATA_PTR(self);
switch (argc) {
case 0: oj_str_writer_push_array(sw, 0); break;
case 1:
if (Qnil == argv[0]) {
oj_str_writer_push_array(sw, 0);
} else {
rb_check_type(argv[0], T_STRING);
oj_str_writer_push_array(sw, StringValuePtr(argv[0]));
}
break;
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
}
if (rb_block_given_p()) {
rb_yield(Qnil);
oj_str_writer_pop(sw);
}
return Qnil;
}
|
#push_json(value, key = nil) ⇒ Object
Pushes a string onto the JSON document. The String must be a valid JSON encoded string. No additional checking is done to verify the validity of the string.
-
value [Object] value to add to the JSON document
-
key [String] the key if adding to an object in the JSON document
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'ext/oj/string_writer.c', line 380
static VALUE str_writer_push_json(int argc, VALUE *argv, VALUE self) {
rb_check_type(argv[0], T_STRING);
switch (argc) {
case 1: oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0); break;
case 2:
if (Qnil == argv[1]) {
oj_str_writer_push_json((StrWriter)DATA_PTR(self), StringValuePtr(*argv), 0);
} else {
rb_check_type(argv[1], T_STRING);
oj_str_writer_push_json((StrWriter)DATA_PTR(self),
StringValuePtr(*argv),
StringValuePtr(argv[1]));
}
break;
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
}
return Qnil;
}
|
#push_key(key) ⇒ Object
Pushes a key onto the JSON document. The key will be used for the next push if currently in a JSON object and ignored otherwise. If a key is provided on the next push then that new key will be ignored.
-
key [String] the key pending for the next push
281 282 283 284 285 286 287 288 |
# File 'ext/oj/string_writer.c', line 281
static VALUE str_writer_push_key(VALUE self, VALUE key) {
StrWriter sw = (StrWriter)DATA_PTR(self);
rb_check_type(key, T_STRING);
oj_str_writer_push_key(sw, StringValuePtr(key));
return Qnil;
}
|
#push_object(key = nil) ⇒ Object
Pushes an object onto the JSON document. Future pushes will be to this object until a pop() is called.
-
key [String] the key if adding to an object in the JSON document
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'ext/oj/string_writer.c', line 297
static VALUE str_writer_push_object(int argc, VALUE *argv, VALUE self) {
StrWriter sw = (StrWriter)DATA_PTR(self);
switch (argc) {
case 0: oj_str_writer_push_object(sw, 0); break;
case 1:
if (Qnil == argv[0]) {
oj_str_writer_push_object(sw, 0);
} else {
rb_check_type(argv[0], T_STRING);
oj_str_writer_push_object(sw, StringValuePtr(argv[0]));
}
break;
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
}
if (rb_block_given_p()) {
rb_yield(Qnil);
oj_str_writer_pop(sw);
}
return Qnil;
}
|
#push_value(value, key = nil) ⇒ Object
Pushes a value onto the JSON document.
-
value [Object] value to add to the JSON document
-
key [String] the key if adding to an object in the JSON document
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'ext/oj/string_writer.c', line 355
static VALUE str_writer_push_value(int argc, VALUE *argv, VALUE self) {
switch (argc) {
case 1: oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0); break;
case 2:
if (Qnil == argv[1]) {
oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
} else {
rb_check_type(argv[1], T_STRING);
oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, StringValuePtr(argv[1]));
}
break;
default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
}
return Qnil;
}
|
#to_s ⇒ Object
Returns the JSON document string in what ever state the construction is at.
return [String]
445 446 447 448 449 450 |
# File 'ext/oj/string_writer.c', line 445
static VALUE str_writer_to_s(VALUE self) {
StrWriter sw = (StrWriter)DATA_PTR(self);
VALUE rstr = rb_str_new(sw->out.buf, sw->out.cur - sw->out.buf);
return oj_encode(rstr);
}
|
#reset ⇒ Object
Reset the writer back to the empty state.
426 427 428 429 430 431 432 433 434 435 436 |
# File 'ext/oj/string_writer.c', line 426
static VALUE str_writer_reset(VALUE self) {
StrWriter sw = (StrWriter)DATA_PTR(self);
sw->depth = 0;
*sw->types = '\0';
sw->keyWritten = 0;
sw->out.cur = sw->out.buf;
*sw->out.cur = '\0';
return Qnil;
}
|
#to_s ⇒ Object
Returns the JSON document string in what ever state the construction is at.
return [String]
445 446 447 448 449 450 |
# File 'ext/oj/string_writer.c', line 445
static VALUE str_writer_to_s(VALUE self) {
StrWriter sw = (StrWriter)DATA_PTR(self);
VALUE rstr = rb_str_new(sw->out.buf, sw->out.cur - sw->out.buf);
return oj_encode(rstr);
}
|