Clarinet is a SAX-like streaming JSON parser for JavaScript, designed for efficient processing of JSON data in both browser and Node.js environments. Inspired by `sax-js` and `yajl`, it provides an evented API that allows developers to process JSON incrementally without needing to load the entire document into memory. This makes it particularly suitable for tasks such as indexing large JSON files or handling continuous streams of JSON where a full DOM-like object model is unnecessary. The package is currently at version 0.12.6, with recent releases (v0.12.x) focusing on minor bug fixes, performance optimizations, and dependency updates, indicating a maintenance cadence rather than rapid feature development. Its key differentiators include portability, robust error reporting with context (line/column numbers), the ability to parse JSON data off a stream incrementally, and a lightweight, simple-to-use API. It explicitly clarifies that it is not a direct replacement for `JSON.parse` but rather a tool for event-driven JSON consumption.
npm install clarinetVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic event-driven parsing of a JSON string, showing how to handle object, array, key, and value events, as well as errors and stream completion for incremental processing.
Evaluate if an evented streaming parser is truly required for your use case. If you need a complete JavaScript object from your JSON, use `JSON.parse()` or a library that builds a DOM-like structure.
Always attach `onerror` handler to the parser or an `error` event listener to the stream. After handling an error, you may need to call `this._parser.error = null; this._parser.resume();` (using internal properties with caution) to continue parsing if the error is recoverable.
Ensure that subsequent logic that depends on full JSON processing is placed within or triggered by the `parser.onend` event handler, not immediately after `parser.close()`.
Implement an `onerror` handler for the parser instance or an `error` event listener for the stream, as shown in the usage examples. Remember to potentially `resume()` the parser after clearing the error if you want to continue.
Ensure you are using `const clarinet = require('clarinet');` and then `const parser = clarinet.parser();`. Do not use `const { parser } = require('clarinet');` for the parser function.No dependency data recorded yet.