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
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
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'})
MIT