Giter VIP home page Giter VIP logo

clarinet-object-stream's Introduction

Clarinet Object Stream

Clarinet is a streaming JSON parser, but it is only streaming when reading in and writing out JSON string data. The very useful parse events are emitted only, not streamed - so it if you want a stream of parse events then you'll have to assemble that yourself.

Hence this small package: Clarinet Object Stream is a stream that lets you pipe JSON in and pipe Clarinet parse events out.

Installation

npm install clarinet-object-stream

Usage

var ClarinetObjectStream = require('clarinet-object-stream');
var clarinetObjectStream = new ClarinetObjectStream({
  // The number of parse events to accumulate in the buffer before ceasing
  // to read piped-in JSON.
  highWaterMark: 16
});
var readStream = fs.createReadStream('path/to/my.json');
readStream.pipe(clarinetObjectStream);

In non-flowing mode:

clarinetObjectStream.on('readable', function () {
  // Start reading. e.g.:
  var parseEvent;
  do {
    parseEvent = clarinetObjectStream.read();
    if(parseEvent) {
      // doSomethingWith(parseEvent);
    }
  } while (parseEvent);
});

In flowing mode:

clarinetObjectStream.on('data', function (parseEvent) {
  // doSomethingWith(parseEvent);
});

Either way, an end event is emitted once the stream is complete:

clarinetObjectStream.on('end', function () {
  // Done!
});

Events

Events returned from clarinetObjectStream.read() have the form:

{ type: 'closearray' }
{ type: 'closeobject' }
{ type: 'error', data: new Error('parse error description') }
{ type: 'key', data: 'string' }
{ type: 'openarray' }
{ type: 'openobject', data: 'first key name' }
{ type: 'value', data: 'string|number|boolean' }

See the Clarinet documentation for more on the events that can be generated by the stream.

Error Events

Syntax errors in JSON will add error events to the object stream - usually quite a large number of error events.

It is up to you as to what is done when an error occurs - either stop the stream and abandon parsing, or continue. If there is syntactically correct JSON further on in the stream, then it will be parsed following the errors and useful parse events will be emitted once again.

clarinet-object-stream's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ledbit

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.