Skip to content
Eugene Lazutkin edited this page Jun 21, 2018 · 6 revisions

The main module returns a factory function, which produces instances of Parser decorated with emit().

Introduction

const makeParser = require('stream-json');
const fs = require('fs');

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

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

API

The returned factory function takes one optional argument: options, and returns a new instance of Parser.

The whole implementation of this function is very simple:

const make = options => emit(new Parser(options));

Static methods and properties

parser(options)

It is set to Parser.parser().

Note that instances produced with parser() are not decorated by emit(). You may want to use it in order to avoid a possible overhead.

const {parser} = require('stream-json');
const fs = require('fs');

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

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

Parser

It is set to Parser.

Note that instances produced with new Parser() are not decorated by emit(). You may want to use it in order to avoid a possible overhead.

const {Parser} = require('stream-json');
const fs = require('fs');

const pipeline = fs.createReadStream('sample.json').pipe(new Parser());

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