Skip to content

mafintosh/json-format-stream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published