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

incomplete-json-parser

v1.1.1

Published

A JSON parser that can parse incomplete JSON strings.

Downloads

189

Readme

incomplete-json-parser

A JSON parser that can parse incomplete JSON strings.

What is incomplete-json-parser?

incomplete-json-parser is a TypeScript module that provides a streaming JSON parser. It can handle incomplete or chunked JSON data, making it useful for parsing JSON data that arrives in multiple parts or when dealing with large JSON files.

The parser is designed to be flexible and can handle various scenarios, such as:

  • Incomplete JSON objects or arrays
  • JSON data split across multiple chunks
  • Incomplete string values

Installation

To install incomplete-json-parser, use the following command:

npm install incomplete-json-parser
yarn add incomplete-json-parser

Usage

Here's an example of how to use incomplete-json-parser:

import { IncompleteJsonParser } from 'incomplete-json-parser';

const parser = new IncompleteJsonParser();

// Write incomplete JSON data to the parser
parser.write('{"name": "John", "age": 30, "city": "New');
parser.write(' York", "hobbies": ["reading", "gaming"');

// Get the parsed JavaScript object
const result = parser.getObjects();
console.log(result);
// Output: { name: 'John', age: 30, city: 'New York', hobbies: ['reading', 'gaming'] }

In this example, we create an instance of the IncompleteJsonParser and write incomplete JSON data to it using the write method. We can write data in multiple chunks, simulating a streaming scenario.

Once we have written all the necessary data, we call the getObjects method to parse the accumulated JSON data and retrieve the resulting JavaScript object.

API

new IncompleteJsonParser()

Creates a new instance of the IncompleteJsonParser.

static parse(chunk: string): any

A static method that allows parsing JSON data in a single step. It creates a new instance of IncompleteJsonParser, writes the provided chunk to it, and returns the parsed JavaScript object.

Using the parse Static Method

const json = '{"name": "Alice", "age": 25, "city": "London"';
const result = IncompleteJsonParser.parse(json);
console.log(result);
// Output: { name: 'Alice', age: 25, city: 'London' }

reset(): void

Resets the parser's internal state, clearing the buffer, accumulator, pointer, and path. This method is useful when you want to reuse the same parser instance for parsing multiple JSON objects.

write(chunk: string): void

Writes a chunk of JSON data to the parser's internal buffer.

getObjects(): any

Parses the accumulated JSON data and returns the parsed JavaScript object.

Examples

Here are a few more examples demonstrating the capabilities of incomplete-json-parser:

Handling Incomplete JSON Objects

const parser = new IncompleteJsonParser();

parser.write('{"name": "Alice", "age": 25, "city": "London"');
const result = parser.getObjects();
console.log(result);
// Output: { name: 'Alice', age: 25, city: 'London' }

Handling Incomplete JSON Arrays

const parser = new IncompleteJsonParser();

parser.write('["apple", "banana", "orange"');
const result = parser.getObjects();
console.log(result);
// Output: ['apple', 'banana', 'orange']

Handling Incomplete String Values

const parser = new IncompleteJsonParser();

parser.write('{"message": "Hello, world!');
const result = parser.getObjects();
console.log(result);
// Output: { message: 'Hello, world!' }

Handling null Values with Different Lengths

const parser = new IncompleteJsonParser();

parser.write('{"value": n');
const result1 = parser.getObjects();
console.log(result1);
// Output: { value: null }

parser.write('{"value": nu');
const result2 = parser.getObjects();
console.log(result2);
// Output: { value: null }

Author

👤 Dante Chun

  • Website: https://dante.company
  • Github: @1000ship

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2024 Dante Chun. This project is MIT licensed.