Class: Oj::StreamWriter

Inherits:
Object
  • Object
show all
Defined in:
ext/oj/oj.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(options) ⇒ Object

Creates a new StreamWriter.


1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
# File 'ext/oj/oj.c', line 1264

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.


1451
1452
1453
1454
1455
1456
1457
1458
1459
# File 'ext/oj/oj.c', line 1451

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.


1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
# File 'ext/oj/oj.c', line 1466

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.


1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
# File 'ext/oj/oj.c', line 1356

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_value(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.


1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
# File 'ext/oj/oj.c', line 1420

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.


1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
# File 'ext/oj/oj.c', line 1308

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.


1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
# File 'ext/oj/oj.c', line 1325

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.


1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
# File 'ext/oj/oj.c', line 1387

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