class Oj::StringWriter
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 tha calling #to_s() before construction is complete will return the document in it's current state.
Public Class Methods
Creates a new StringWriter. @param [Hash] options formating options
static VALUE str_writer_new(int argc, VALUE *argv, VALUE self) { StrWriter sw = ALLOC(struct _StrWriter); str_writer_init(sw); if (1 == argc) { oj_parse_options(argv[0], &sw->opts); } sw->out.indent = sw->opts.indent; return Data_Wrap_Struct(oj_string_writer_class, 0, str_writer_free, sw); }
Public Instance Methods
Pops up a level in the JSON document closing the array or object that is currently open.
static VALUE str_writer_pop(VALUE self) { oj_str_writer_pop((StrWriter)DATA_PTR(self)); return Qnil; }
Pops all level in the JSON document closing all the array or object that is currently open.
static VALUE str_writer_pop_all(VALUE self) { oj_str_writer_pop_all((StrWriter)DATA_PTR(self)); return Qnil; }
Pushes an array onto the JSON document. Future pushes will be to this object until a pop() is called. @param [String] key the key if adding to an object in the JSON document
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; }
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. @param [String] value JSON document to add to the JSON document @param [String] key the key if adding to an object in the JSON document
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; }
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. @param [String] key the key pending for the next push
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; }
Pushes an object onto the JSON document. Future pushes will be to this object until a pop() is called. @param [String] key the key if adding to an object in the JSON document
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; }
Pushes a value onto the JSON document. @param [Object] value value to add to the JSON document @param [String] key the key if adding to an object in the JSON document
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; }
Reset the writer back to the empty state.
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; }
Returns the JSON document string in what ever state the construction is at.
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); }