Skip to content

stream-utils/streaming-json-stringify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Streaming JSON.stringify()

NPM version Build status Test coverage Dependency Status License Downloads Gittip

Similar to JSONStream.stringify() except it is, by default, a binary stream, and it is a streams2 implementation.

Example

The main use case for this is to stream a database query to a web client. This is meant to be used only with arrays, not objects.

var Stringify = require('streaming-json-stringify')

app.get('/things', function (req, res, next) {
  res.setHeader('Content-Type', 'application/json; charset=utf-8')

  db.things.find()
  .stream()
  .pipe(Stringify())
  .pipe(res)
})

will yield something like

[
{"_id":"123412341234123412341234"}
,
{"_id":"123412341234123412341234"}
]

Separators

  • The stream always starts with '[\n'.
  • Documents are separated by '\n,\n'.
  • The stream is terminated with '\n]\n'.

API

Stringify([options])

Returns a Transform stream. The options are passed to the Transform constructor.

JSON.stringify options

You can override these:

var stringify = Stringify()
stringify.replacer = function () {}
stringify.space = 2

About

Streaming JSON.stringify()

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •