Disassembler - uhop/stream-json GitHub Wiki

Disassembler receives a stream of JavaScript objects and produces a token stream.

Introduction

const {chain} = require('stream-chain');

const {disassembler} = require('stream-json/disassembler.js');
const {pick} = require('stream-json/filters/pick.js');
const {ignore} = require('stream-json/filters/ignore.js');
const {streamValues} = require('stream-json/streamers/stream-values.js');

const pipeline = chain([readObjectFromDatabase(), disassembler(), pick({filter: 'content'}), ignore({filter: /\b_/}), streamValues()]);

In this example, we read objects from a database, convert them to a stream of tokens, pick only values of top-level property content, remove all properties that start with _ (underscore), and assemble them back into a stream of objects.

(Since 1.5.0) Disassembler supports logic defined by JSON.stringify(): skipping non-JSON properties or replacing them with null when appropriate, supporting toJSON() method, and replacer.

API

disassembler() returns a function for use in a chain() pipeline. disassembler.asStream() wraps it as a Duplex stream. It operates in object mode and is modeled on Parser.

disassembler(options)

Like Parser, it supports all packing and streaming options described in Parser.

Additionally, it supports the following options:

  • (since 1.5.0) replacer can be one of two optional objects:
    • It can be a function, which takes two parameters and returns a value.
    • It can be an array of strings or numbers to define a whitelist for object properties.
    • See JSON.stringify() for more details.

Static methods and properties

disassembler.asStream(options)

Wraps the disassembler as a Duplex stream:

const {disassembler} = require('stream-json/disassembler.js');

const {Readable} = require('stream');
const {chain} = require('stream-chain');

const pipeline = chain([
  new Readable({
    objectMode: true,
    read(size) {
      database.readNext().then(data => this.push(data));
    }
  }),
  disassembler()
]);

Notes

Disassembler cannot accept top-level null due to restrictions of Node's stream system.