Class: Oj::StreamWriter

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

Class Method Details

.new(io, options) ⇒ Object

Creates a new StreamWriter.

Parameters:

  • io (IO)

    stream to write to

  • options (Hash)

    formating options



1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
# File 'ext/oj/oj.c', line 1386

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 = ALLOC(struct _StreamWriter);
    str_writer_init(&sw->sw);
    if (2 == argc) {
	oj_parse_options(argv[1], &sw->sw.opts);
    }
    sw->sw.out.indent = sw->sw.opts.indent;
    sw->stream = stream;
    sw->type = type;
    sw->fd = fd;

    return Data_Wrap_Struct(oj_stream_writer_class, 0, stream_writer_free, sw);
}

Instance Method Details

#popObject

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



1573
1574
1575
1576
1577
1578
1579
1580
1581
# File 'ext/oj/oj.c', line 1573

static VALUE
stream_writer_pop(VALUE self) {
    StreamWriter	sw = (StreamWriter)DATA_PTR(self);

    stream_writer_reset_buf(sw);
    oj_str_writer_pop(&sw->sw);
    stream_writer_write(sw);
    return Qnil;
}

#pop_allObject

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



1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
# File 'ext/oj/oj.c', line 1588

static VALUE
stream_writer_pop_all(VALUE self) {
    StreamWriter	sw = (StreamWriter)DATA_PTR(self);

    stream_writer_reset_buf(sw);
    oj_str_writer_pop_all(&sw->sw);
    stream_writer_write(sw);

    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



1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
# File 'ext/oj/oj.c', line 1478

static VALUE
stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
    StreamWriter	sw = (StreamWriter)DATA_PTR(self);

    stream_writer_reset_buf(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 {
	    rb_check_type(argv[0], T_STRING);
	    oj_str_writer_push_array(&sw->sw, StringValuePtr(argv[0]));
	}
	break;
    default:
	rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
	break;
    }
    stream_writer_write(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 (Object)

    value to add to the JSON document

  • key (String)

    the key if adding to an object in the JSON document



1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
# File 'ext/oj/oj.c', line 1542

static VALUE
stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
    StreamWriter	sw = (StreamWriter)DATA_PTR(self);

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



1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
# File 'ext/oj/oj.c', line 1430

static VALUE
stream_writer_push_key(VALUE self, VALUE key) {
    StreamWriter	sw = (StreamWriter)DATA_PTR(self);

    rb_check_type(key, T_STRING);
    stream_writer_reset_buf(sw);
    oj_str_writer_push_key(&sw->sw, StringValuePtr(key));
    stream_writer_write(sw);
    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



1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
# File 'ext/oj/oj.c', line 1447

static VALUE
stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
    StreamWriter	sw = (StreamWriter)DATA_PTR(self);

    stream_writer_reset_buf(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 {
	    rb_check_type(argv[0], T_STRING);
	    oj_str_writer_push_object(&sw->sw, StringValuePtr(argv[0]));
	}
	break;
    default:
	rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'.");
	break;
    }
    stream_writer_write(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



1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
# File 'ext/oj/oj.c', line 1509

static VALUE
stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
    StreamWriter	sw = (StreamWriter)DATA_PTR(self);

    stream_writer_reset_buf(sw);
    switch (argc) {
    case 1:
	oj_str_writer_push_value((StrWriter)DATA_PTR(self), *argv, 0);
	break;
    case 2:
	if (Qnil == argv[0]) {
	    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;
    }
    stream_writer_write(sw);
    return Qnil;
}