jfs

json-format-stream

Streaming JSON serializer that allows you to add metadata and will forward an error message if the stream is destroyed prematurely

Showing:

Popularity

Downloads/wk

1

GitHub Stars

22

Maintenance

Last Commit

7yrs ago

Contributors

2

Package

Dependencies

3

Size (min+gzip)

15.4KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

json-format-stream

Streaming JSON serializer that allows you to add metadata and will forward an error message if the stream is destroyed prematurely

npm install json-format-stream

build status

Usage

var format = require('json-format-stream')

var stream = format({some: 'metadata'})

stream.write({some: 'data'})
stream.write({more: 'data'})
stream.destroy(new Error('an error occurred'))

stream.pipe(process.stdout)

Running the above will print out

{
  "some": "metadata",
  "result": [
    {
      "some": "data"
    },
    {
      "more": "data"
    }
  ],
  "error": "an error occurred"
}

If you don't call destroy the error property in the result will be set to null when the stream finishes. The main result is streamed using JSONStream which makes this memory efficient

API

stream = format(metadata, options)

Creates a new JSON formatter. Any metadata properties you provide in the constructor will be set in the beginning of the JSON response.

Pass options.outputKey to specify which key data is added to. Defaults to result.

var stream = format(null, {outputKey: 'data'})

License

MIT

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100