Stringer - uhop/stream-json GitHub Wiki

Stringer consumes a token stream and converts it to text representing a JSON object. stringer() returns a function for use in chain(). stringer.asStream() wraps it as a Duplex stream. It is very useful when you want to edit a stream with filters and custom code, and save it back to a file.

If Parser corresponds to JSON.parse(), then Stringer corresponds to JSON.stringify(). However, it will only convert a stream of tokens generated by Parser. Feeding raw objects into Stringer will output empty strings.

(Since 1.6.0) If you need to output JSONL instead of a regular JSON, take a look at jsonl/Stringer.

Introduction

const {stringer} = require('stream-json/stringer.js');
const {parser} = require('stream-json');
const {chain} = require('stream-chain');
const {pick} = require('stream-json/filters/pick.js');

const fs = require('fs');
const zlib = require('zlib');

chain([
  fs.createReadStream('data.json.gz'),
  zlib.createGunzip(),
  parser(),
  pick({filter: 'data'}),
  stringer(),
  zlib.createGzip(),
  fs.createWriteStream('edited.json.gz')
]);

API

Stringer has no special API. The writable side operates in object mode and the readable side produces text.

stringer(options)

options is an optional object. The following custom flags are recognized (all truthy/falsy, default false):

  • useValues serves as the initial value for selecting packed or streamed values of strings, numbers, and keys.
  • useKeyValues specifies, if we need to use packed keys or streamed ones.
  • useStringValues specifies, if we need to use packed strings or streamed ones.
  • useNumberValues specifies, if we need to use packed numbers or streamed ones.
  • makeArray (since 1.4.0) forces all incoming JSON objects to be wrapped in an array.
    • If no input, an empty array will be produced.

By default Stringer converts chunks to strings and outputs them. But it is possible to force it to use packed values instead of streamed chunks, providing that an upstream sends packed values. In some cases, when no streamed chunks are present in a token stream, we have to force the use of values.

Internally each type of value is controlled by a flag:

  • By default, this flag is false.
  • If useValues is set, it is assigned to each flag.
  • If an individual option is set, it is assigned to the flag.

Examples:

Supplied options useKeyValues useStringValues useNumberValues
{} false false false
{useValues: true} true true true
{useValues: true, useKeyValues: false} false true true
{useKeyValues: false, useValues: true} false true true
{useStringValues: true} false true false
{useKeyValues: true, useStringValues: false, useNumberValues: true} true false true

Static methods and properties

stringer.stringer(options)

Alias of the factory function.

stringer.asStream(options)

Returns a Duplex stream suitable for .pipe() usage.

const stringer = require('stream-json/stringer.js');
const {chain} = require('stream-chain');
const {parser} = require('stream-json');
const {pick} = require('stream-json/filters/pick.js');

const fs = require('fs');

const pipeline = chain([fs.createReadStream('sample.json'), parser(), pick({filter: 'data'}), stringer()]);