Class: Oj::StringWriter

Inherits:
Object
  • Object
show all
Defined in:
ext/oj/oj.c,
ext/oj/oj.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 tha calling to_s() before construction is complete will return the document in it's current state.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(options) ⇒ Object

Creates a new StringWriter.

Parameters:

  • options (Hash)

    formating options



1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'ext/oj/oj.c', line 1007

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);
}

Instance Method Details

#popObject

Pops up a level in the JSON document closing the array or object that is currently open.



1165
1166
1167
1168
1169
# File 'ext/oj/oj.c', line 1165

static VALUE
str_writer_pop(VALUE self) {
    oj_str_writer_pop((StrWriter)DATA_PTR(self));
    return Qnil;
}

#pop_allObject

Pops all level in the JSON document closing all the array or object that is currently open.



1176
1177
1178
1179
1180
1181
# File 'ext/oj/oj.c', line 1176

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.

Parameters:

  • key (String)

    the key if adding to an object in the JSON document



1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
# File 'ext/oj/oj.c', line 1076

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.

Parameters:

  • value (String)

    JSON document to add to the JSON document

  • key (String)

    the key if adding to an object in the JSON document



1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
# File 'ext/oj/oj.c', line 1138

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.

Parameters:

  • key (String)

    the key pending for the next push



1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'ext/oj/oj.c', line 1027

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.

Parameters:

  • key (String)

    the key if adding to an object in the JSON document



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
# File 'ext/oj/oj.c', line 1043

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.

Parameters:

  • value (Object)

    value to add to the JSON document

  • key (String)

    the key if adding to an object in the JSON document



1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'ext/oj/oj.c', line 1109

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;
}

#resetObject

Reset the writer back to the empty state.



1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
# File 'ext/oj/oj.c', line 1187

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_sObject

Returns the JSON document string in what ever state the construction is at.



1204
1205
1206
1207
1208
1209
1210
# File 'ext/oj/oj.c', line 1204

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);
}