@init-kz/jsonparse-ts
Version:
Library for parsing partial jsons inspired by original jsonparse written on js
89 lines (59 loc) • 2.71 kB
Markdown
<p align="center">
<img src="assets/favicon.svg" width="100" height="100" alt="Logo">
</p>
This is a fully rewritten **TypeScript** version of the `jsonparse` library.
Originally created by Tim Caswell in 2012, this version modernizes the approach by following **OOP principles** and introducing additional functionality.
Unlike the original, this version fully supports **browser environments** and provides enhanced features for better control over JSON parsing.
- **Streaming JSON parsing**: Handles JSON data in chunks.
- **Full browser support**: Works both in Node.js and browser environments.
- **Improved API**: Additional methods for better parsing control.
- **Event-driven architecture**: Supports event emitters for handling tokens and values.
- **Better error handling**: Capture parsing errors more efficiently.
```sh
npm install @init-kz/jsonparse-ts
```
```ts
import { Parser } from '@init-kz/jsonparse-ts';
const parser = new Parser();
parser.write('[{"name": "A');
console.log(parser.getLastIncompleteValue()); // { name: "A" }
parser.write('"}, {"na');
console.log(parser.getLastIncompleteValue()); // { name: "A" }, { }
parser.write('me": "Occaecat aliq');
console.log(parser.getLastIncompleteValue()); // { name: "A" }, { name: "Occaecat aliq" }
parser.write('uip id officia ipsum deserunt ea id labore magna qui elit dolore consectetur dolore.');
console.log(parser.getLastValue()); // Full JSON structure
```
Returns the event emitter used internally by the parser.
Registers a callback for when a token is parsed.
Registers a callback for when a complete value is parsed.
Registers a callback for handling errors.
Retrieves the last fully parsed value.
Retrieves the last partially parsed value, except numbers.
Gets the current key being parsed in an object.
Returns the internal parsing stack.
Returns the current byte offset in the stream.
Processes a chunk of JSON data.
Contributions are welcome! Feel free to submit issues or open pull requests to improve the project.
```
The MIT License
Copyright (c) 2012 Tim Caswell
Copyright (c) 2025 INIT.KZ
```