Class: Oj::StringWriter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.new(*args) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'ext/oj/string_writer.c', line 290

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);
    }
    oj_options_take_ownership(&sw->opts);
    sw->out.argc   = argc - 1;
    sw->out.argv   = argv + 1;
    sw->out.indent = sw->opts.indent;

    return TypedData_Wrap_Struct(oj_string_writer_class, &oj_string_writer_type, sw);
}

Instance Method Details

#as_json(*args) ⇒ Object



501
502
503
504
505
506
# File 'ext/oj/string_writer.c', line 501

static VALUE str_writer_as_json(int argc, VALUE *argv, VALUE self) {
    if (string_writer_optimized) {
        return self;
    }
    return rb_hash_new();
}

#popObject



437
438
439
440
441
442
443
# File 'ext/oj/string_writer.c', line 437

static VALUE str_writer_pop(VALUE self) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);

    oj_str_writer_pop(sw);
    return Qnil;
}

#pop_allObject



451
452
453
454
455
456
457
458
# File 'ext/oj/string_writer.c', line 451

static VALUE str_writer_pop_all(VALUE self) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);

    oj_str_writer_pop_all(sw);

    return Qnil;
}

#push_array(*args) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'ext/oj/string_writer.c', line 358

static VALUE str_writer_push_array(int argc, VALUE *argv, VALUE self) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);

    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 {
            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(*args) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'ext/oj/string_writer.c', line 414

static VALUE str_writer_push_json(int argc, VALUE *argv, VALUE self) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);

    switch (argc) {
    case 1: oj_str_writer_push_json(sw, StringValuePtr(*argv), 0); break;
    case 2:
        if (Qnil == argv[1]) {
            oj_str_writer_push_json(sw, StringValuePtr(*argv), 0);
        } else {
            oj_str_writer_push_json(sw, 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



313
314
315
316
317
318
319
320
# File 'ext/oj/string_writer.c', line 313

static VALUE str_writer_push_key(VALUE self, VALUE key) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);

    oj_str_writer_push_key(sw, StringValuePtr(key));

    return Qnil;
}

#push_object(*args) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'ext/oj/string_writer.c', line 329

static VALUE str_writer_push_object(int argc, VALUE *argv, VALUE self) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);

    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 {
            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(*args) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'ext/oj/string_writer.c', line 387

static VALUE str_writer_push_value(int argc, VALUE *argv, VALUE self) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);

    switch (argc) {
    case 1: oj_str_writer_push_value(sw, *argv, 0); break;
    case 2:
        if (Qnil == argv[1]) {
            oj_str_writer_push_value(sw, *argv, 0);
        } else {
            oj_str_writer_push_value(sw, *argv, StringValuePtr(argv[1]));
        }
        break;
    default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
    }
    return Qnil;
}

#raw_jsonObject



485
486
487
488
489
# File 'ext/oj/string_writer.c', line 485

static VALUE str_writer_to_s(VALUE self) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
    return rb_utf8_str_new(sw->out.buf, sw->out.cur - sw->out.buf);
}

#resetObject



465
466
467
468
469
470
471
472
473
474
475
476
# File 'ext/oj/string_writer.c', line 465

static VALUE str_writer_reset(VALUE self) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);

    sw->depth      = 0;
    *sw->types     = '\0';
    sw->keyWritten = 0;
    sw->out.cur    = sw->out.buf;
    *sw->out.cur   = '\0';

    return Qnil;
}

#to_sObject



485
486
487
488
489
# File 'ext/oj/string_writer.c', line 485

static VALUE str_writer_to_s(VALUE self) {
    StrWriter sw;
    TypedData_Get_Struct(self, struct _strWriter, &oj_string_writer_type, sw);
    return rb_utf8_str_new(sw->out.buf, sw->out.cur - sw->out.buf);
}