npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-json-stream

v0.2.6

Published

JSON stream parsing library.

Downloads

102

Readme

node-json-stream

npm version npm monthly downloads node-json-stream

Introducing a rapid, lightweight, and self-contained JSON parsing library specifically designed for stream-based processing. This library adheres completely to the JSON specification.

Usage

import { Parser, Tokenizer } from 'node-json-stream';
import { PassThrough } from 'stream';
import through from 'through2';

const pass = new PassThrough();
const sink = through.obj(function (item, e, next) {
  console.log('received', item);
  this.push(item);
  next();
});

pass
  .pipe(new Tokenizer())
  .pipe(new Parser())
  .pipe(sink);

pass.write(JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}));

Parser options

new Parser({
  /**
    Define maximum payload size, if size is
    exceeded parser will emit a parsing-error event.

    By default uses 4096.
  */
  maxPayloadByteSize: 1000,
  /**
    Define types that are allowed for the root element,
    if the root element is not in the list it'll emit
    a parsing-error event.

    By default contains Array, Object, String, Number, Boolean
  */
  allowedRootElements: [ConsumerType.Object],
  /**
    Define if parser should recognize the separator
    token send by the tokenizer, if set to true the
    parser will always wait for the separator token
    after parsing of an object is done.

    By default set to false.
  */
  usesSeparator: true,
  /**
    Define which path the parser should return. Asterisk
    can be used as wildcard.

    By default set to null.
  */
  resolvePath: ['myPath', '*']
})

Tokenizer options

new Tokenizer({
  /**
    Define maximum number length, if the number length
    is exceeded the toknizer will push a invalid token
    type to the parser.

    Uses 20 by default.
  */
  maxNumberLength: 5,
  /**
    Define maximum string length, if the string length
    is exceeded the toknizer will push a invalid token
    type to the parser.

    Uses 1000 by default.
  */
  maxStringLength: 10,
  /**
    Define separator character for tokenizer to forward to parser.
    Uses \n by default.
  */
  separatorCharacter: '%'
})

Examples

One message after another without separator

import { Parser, Tokenizer } from 'node-json-stream';
import { chain } from 'stream-chain';

const myStream = chain([
  new Tokenizer(),
  new Parser()
]);

myStream.on('data', (payload) => {
  /* will print both objects */
  console.log('myData', payload);
});

myStream.write(JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}));

myStream.write(JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}));

A invalid JSON is sent but recover and progress to parse next item

import { Parser, Tokenizer } from 'node-json-stream';
import { chain } from 'stream-chain';

const myStream = chain([
  new Tokenizer(),
  new Parser()
]);

myStream.on('data', (payload) => {
  /* will print last object since it's valid */
  console.log('myData', payload);
});

myStream.write('{ } w }}}');

myStream.write(JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}));

Send multiple messages in one

import { Parser, Tokenizer } from 'node-json-stream';
import { chain } from 'stream-chain';

const myStream = chain([
  new Tokenizer(),
  new Parser()
]);

myStream.on('data', (payload) => {
  /* will print both objects */
  console.log('myData', payload);
});

myStream.write(JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}) + JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}));

Send multiple messages in one and use separator

import { Parser, Tokenizer } from 'node-json-stream';
import { chain } from 'stream-chain';

const myStream = chain([
  new Tokenizer(),
  new Parser({ usesSeparator: true })
]);

myStream.on('data', (payload) => {
  /**
    only prints the first object and last
    object due to missing a newline after
    the first object
  */
  console.log('myData', payload);
});

myStream.write(JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}) + JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}) + '\n' + JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}));

Send multiple different JSONs but only allow object

import { Parser, Tokenizer, ConsumerType } from 'node-json-stream';
import { chain } from 'stream-chain';

const myStream = chain([
  new Tokenizer(),
  new Parser({ allowedRootElements: [ConsumerType.Object] })
]);

myStream.on('data', (payload) => {
  /*
    only prints the objects and ignores
    the strings and number
  */
  console.log('myData', payload);
});

myStream.write(JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}));

myStream.write(JSON.stringify("Hello world!"));
myStream.write(JSON.stringify("Hello world!"));
myStream.write(JSON.stringify("Hello world!"));

myStream.write(JSON.stringify(42));

myStream.write(JSON.stringify({
  method: 'test',
  data: {
    message: 'Hello world!',
  }
}));

Send JSON but only resolve data in message path

import { Parser, Tokenizer } from 'node-json-stream';
import { chain } from 'stream-chain';

const myStream = chain([
  new Tokenizer(),
  new Parser({ resolvePath: ['data', '*', 'message'] })
]);

myStream.on('data', (payload) => {
  /* will print "hello world!" and "another hello world!" */
  console.log('myData', payload);
});

myStream.write(JSON.stringify({
  method: 'test',
  data: [{
    'message': "hello world!"
  }, {
    'message': "another hello world!"
  }]
}));