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

big-json

v3.2.0

Published

A stream based implementation of JSON.parse and JSON.stringify for big POJOs

Downloads

547,657

Readme

big-json

NPM Version Build Status Coverage Status Dependency Status devDependency Status

A stream based implementation of JSON.parse and JSON.stringify for big POJOs

There exist many stream based implementations of JSON parsing or stringifying for large data sets. These implementations typical target time series data, new line delimited data or other array-like data, e.g., logging records or other continuous flowing data.

This module hopes to fill a gap in the ecosystem: parsing large JSON objects that are just really big objects. With large in-memory objects, it is possible to run up against the V8 string length limitation, which is currently (as of 9/2017) limited to 512MB. Thus, if your large object has enough keys or values, it is possible to exceed the string length limit when calling JSON.stringify.

Similarly, when retrieving stored JSON from disk or over the network, if the JSON stringified representation of the object exceeds the string length limit, the process will throw when attempting to convert the Buffer into a string.

The only way to work with such large objects is to use a streaming implementation of both JSON.parse and JSON.stringify. This module does just that by normalizing the APIs for different modules that have previously published, combining both parse and stringify functions into a single module. These underlying modules are subject to change at anytime.

The major caveat is that the reconstructed POJO must be able to fit in memory. If the reconstructed POJO cannot be stored in memory, then it may be time to reconsider the way these large objects are being transported and processed.

This module currently uses JSONStream for parsing, and json-stream-stringify for stringification.

Getting Started

Install the module with: npm install big-json

Usage

To parse a big JSON coming from an external source:

const fs = require('fs');
const path = require('path');
const json = require('big-json');

const readStream = fs.createReadStream('big.json');
const parseStream = json.createParseStream();

parseStream.on('data', function(pojo) {
    // => receive reconstructed POJO
});

readStream.pipe(parseStream);

To stringify JSON:

const json = require('big-json');

const stringifyStream = json.createStringifyStream({
    body: BIG_POJO
});

stringifyStream.on('data', function(strChunk) {
    // => BIG_POJO will be sent out in JSON chunks as the object is traversed
});

API

createParseStream()

Parses an incoming stream and accumulates it into a POJO. Supports both objects and arrays as root objects for stream data.

Returns: {Stream} a JSON.parse stream

createStringifyStream(opts)

  • opts {Object} an options object
  • opts.body {Object | Array} an object or array to JSON.stringify

Returns: {Stream} a JSON.stringify stream

parse(opts, [callback])

An async JSON.parse using the same underlying stream implementation. If a callback is not passed, a promise is returned.

  • opts {Object} an options object
  • opts.body {String | Buffer} the string or buffer to be parsed
  • callback {Function} a callback object

Returns: {Object | Array} the parsed JSON

stringify(opts, [callback])

An async JSON.stringify using the same underlying stream implementation. If a callback is not passed, a promise is returned.

  • opts {Object} an options object
  • opts.body {Object} the object to be stringified
  • callback {Function} a callback object

Returns: {Object} the stringified object

Contributing

Ensure that all linting and codestyle tasks are passing. Add unit tests for any new or changed functionality.

To start contributing, install the git prepush hooks:

make githooks

Before committing, lint and test your code using the included Makefile:

make prepush

License

Copyright (c) 2019 Alex Liu

Licensed under the MIT license.