Skip to content
JSON.Stringify as a readable stream
JavaScript Shell
Branch: master
Clone or download
Latest commit e8677a2 Nov 27, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis Merge branch 'master' of github.com:Faleij/json-stream-stringify Jun 27, 2018
.vscode Resolves #7 Oct 20, 2018
src Optimize processObject Nov 26, 2019
test-src Fix lint error Oct 20, 2018
.babelrc.js 2.0 (#6) Jun 27, 2018
.eslintignore 2.0 (#6) Jun 27, 2018
.eslintrc.js 2.0 (#6) Jun 27, 2018
.gitignore 2.0 (#6) Jun 27, 2018
.travis.yml restore travis stages Oct 20, 2018
LICENSE Refactoring for release. Added travis, README and LICENSE May 7, 2016
README.md Update README.md Jul 1, 2018
package-lock.json 2.0 (#6) Jun 27, 2018
package.json increment package version Nov 27, 2019
rollup.config.js 2.0 (#6) Jun 27, 2018

README.md

JSON Stream Stringify

NPM version NPM Downloads Build Status Coverage Status License Donate

JSON Stringify as a Readable Stream with rescursive resolving of any readable streams and Promises.

Important and Breaking Changes in v2

  • Completely rewritten from scratch
  • 100% Code Coverage! 🎉
  • Space argument finally implemented! 🎉
  • ⚠️ Cycling is off by default
  • ⚠️ JsonStreamStringify is now a constructor; use new operator
  • Removed dependency on global JSON.stringify, Async/Await and Generators
  • JsonStreamStringify is now compiled with babel to target ES5 (polyfills needed)
  • Rejected promises and input stream errors are now handled and emitted as errors
  • Added cyclic structure detection to prevent infinite recursion

Main Features

  • Promises are rescursively resolved and the result is piped through JsonStreamStringify
  • Streams (Object mode) are recursively read and output as arrays
  • Streams (Non-Object mode) are output as a single string
  • Output is streamed optimally with as small chunks as possible
  • Cycling of cyclical structures and dags using Douglas Crockfords cycle algorithm*
  • Great memory management with reference release after processing and WeakMap/Set reference handling
  • Optimal stream pressure handling
  • Tested and runs on ES5** and ES2015
  • Bundled as UMD and Module

* Off by default since v2
** With polyfills

Install

npm install --save json-stream-stringify

# Optional if you need polyfills
# Make sure to include these if you target NodeJS <=v6 or browsers
npm install --save @babel/polyfill @babel/runtime

Usage

Using Node v8 or later with ESM / Webpack / Browserify / Rollup

// No Polyfills
import JsonStreamStringify from 'json-stream-stringify';
// Polyfilled; loads only needed polyfills from @babel/polyfill @babel/runtime
import JsonStreamStringify from 'json-stream-stringify/module.polyfill';

Using Node >=8 / Other ES2015 environments

const JsonStreamStringify = require('json-stream-stringify');

Using Node <=6 / Other ES5 environments

var JsonStreamStringify = require('json-stream-stringify/umd.polyfill');

Note: This library is primarily written for LTS versions of NodeJS. Other environments are not tested.
Note on non-NodeJS usage: This module depends on node streams library. Any Streams3 compatible implementation should work - as long as it exports a Readable class, with instances that looks like readable streams.
Note on Polyfills: I have taken measures to minify global pollution of polyfills but this library does not load polyfills by default because the polyfills modify native object prototypes and it goes against the W3C recommendations.

API

new JsonStreamStringify(value[, replacer[, spaces[, cycle]]])

Streaming conversion of value to JSON string.

Parameters

  • value Any
    Data to convert to JSON.

  • replacer Optional Function(key, value) or Array
    As a function the returned value replaces the value associated with the key. Details
    As an array all other keys are filtered. Details

  • spaces Optional String or Number
    A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space. If this is a String, the string is used as white space. If this parameter is not recognized as a finite number or valid string, no white space is used.

  • cycle Optional Boolean
    true enables cycling of cyclical structures and dags.
    To restore cyclical structures; use Crockfords Retrocycle method on the parsed object (not included in this module).

Returns

jsonStreamStringify#path

Returns

  • Array[String, Number]
    Current path being serialized as an array of Strings (keys of objects) and Numbers (index into arrays).
    Can be transformed into an mpath with .join('.').
    Useful in conjunction with .on('error', ...), for figuring out what path may have caused the error.

Example Usage

const JsonStreamStringify = require('json-stream-stringify');

const jsonStream = new JsonStreamStringify({
    // Promises and Streams may resolve more promises and/or streams which will be consumed and processed into json output
    aPromise: Promise.resolve(Promise.resolve("text")),
    aStream: ReadableObjectStream({a:1}, 'str'),
    arr: [1, 2, Promise.resolve(3), Promise.resolve([4, 5]), ReadableStream('a', 'b', 'c')],
    date: new Date(2016, 0, 2)
});
jsonStream.once('error', () => console.log('Error at path', jsonStream.stack.join('.')));
jsonStream.pipe(process.stdout);

Output (each line represents a write from jsonStreamStringify)

{
"aPromise":
"text"
"aStream":
[
{
"a":
1
}
,
"str"
]
"arr":
[
1
,
2
,
3
,
[
4
,
5
]
,
"
a
b
c
"
],
"date":
"2016-01-01T23:00:00.000Z"
}

Practical Example with Express + Mongoose

app.get('/api/users', (req, res, next) => new JsonStreamStringify(Users.find().stream()).pipe(res));

License

MIT

Copyright (c) 2016 Faleij faleij@gmail.com

You can’t perform that action at this time.