Skip to content
Eugene Lazutkin edited this page Jul 25, 2018 · 5 revisions

StreamObject assumes that a token stream represents an object and streams out its top-level properties as assembled JavaScript objects.

{"a": 1, "b": "a", "c": [], "d": {}, "e": true}
// StreamObject will produce an object stream:
{key: 'a', value: 1}
{key: 'b', value: 'a'}
{key: 'c', value: []}
{key: 'd', value: {}}
{key: 'e', value: true}

As every streamer, it assumes that individual objects can fit in memory, but the whole file, or any other source, should be streamed.

Warning: it cannot be used with a stream of objects produced by a JSON Streaming source, or Pick if it picks more than one object.

Introduction

const StreamObject = require('stream-json/streamers/StreamObject');
const fs = require('fs');

const pipeline = fs.createReadStream('sample.json')
  .pipe(StreamObject.withParser());

pipeline.on('data', data => console.log(data));

API

Being based on StreamBase, StreamObject has no special API.

Static methods and properties

streamObject(options) and make(options)

make() and streamObject() are two aliases of the factory function. It takes options described above, and return a new instance of StreamObject. streamObject() helps to reduce a boilerplate when creating data processing pipelines:

const {chain}  = require('stream-chain');
const {parser} = require('stream-json');
const {streamObject} = require('stream-json/streamers/StreamObject');

const fs = require('fs');

const pipeline = chain([
  fs.createReadStream('sample.json'),
  parser(),
  streamObject()
]);

let objectCounter = 0;
pipeline.on('data', () => ++objectCounter);
pipeline.on('end', console.log(`Found ${objectCounter} objects.`));

make.Constructor

Constructor property of make() (and streamObject()) is set to StreamObject. It can be used for indirect creating of streamers or metaprogramming if needed.

withParser()

withParser() takes one argument:

  • options is an object described in Parser's options. It is used to initialize both streams (a Parser instance and a stream returned by make()).

It returns a stream produced by stream-chain, which wraps the pipeline. The most important utility of withParser() is that it correctly sets object modes of the returned stream: object mode for the Readable part and text mode for the Writable part.

This static method is created using withParser() utility. It simplifies a case when a stream should be immediately preceded by a parser.

const StreamObject = require('stream-json/streamers/StreamObject');
const fs = require('fs');

const pipeline = fs.createReadStream('sample.json')
  .pipe(StreamObject.withParser());

let objectCounter = 0;
pipeline.on('data', () => ++objectCounter);
pipeline.on('end', console.log(`Found ${objectCounter} objects.`));
Clone this wiki locally