Giter Site home page Giter Site logo

mafintosh / json-format-stream Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 1.0 183 KB

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

License: MIT License

JavaScript 100.00%

json-format-stream's Introduction

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

json-format-stream's People

Contributors

mafintosh avatar feross avatar timhudson avatar

Stargazers

Scott J avatar Cat  avatar  avatar  avatar Angus H. avatar Chris Hart avatar Tejesh Mehta avatar  avatar Seth Vincent avatar Aster Haven avatar Dmytro Voronianski avatar  avatar Marwan Hilmi avatar Jonas Hermsmeier avatar Athan avatar Johnie Hjelm avatar Fredrik Forsmo avatar Pelle Wessman avatar Keith Williams avatar Finn Pauls avatar  avatar Yosh avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

timhudson

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.