### Initialize Tokenizer and JSON Parser in JavaScript Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md Demonstrates the initialization of Tokenizer and TokenParser components and their chaining for JSON parsing in JavaScript. This setup is for basic component usage. ```javascript const tokenizer = new Tokenizer(opts); const tokenParser = new TokenParser(); const jsonParser = tokenizer.pipeTrough(tokenParser); ``` -------------------------------- ### Configure TokenParser Options (Node.js) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md This example outlines the configuration options for the TokenParser. Key options include 'paths' for selecting specific data to emit, 'keepStack' to control memory usage by managing intermediate object states, and 'separator' for handling delimited JSON objects. ```javascript { paths: , keepStack: , // whether to keep all the properties in the stack separator: , // separator between object. For example `\n` for nd-js. If left empty or set to undefined, the token parser will end after parsing the first object. To parse multiple object without any delimiter just set it to the empty string `''`. emitPartialValues: , // whether to emit values mid-parsing. } ``` -------------------------------- ### Stream Parsing Fetch Request (JSON Array) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/README.md Example of stream-parsing a fetch request returning a JSON array. It configures the JSONParser with specific options like paths and keepStack, then processes each element of the array. ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$.*'], keepStack: false }); const response = await fetch('http://example.com/'); const reader = response.body.pipeThrough(parser).getReader(); while(true) { const { done, value: parsedElementInfo } = await reader.read(); if (done) break; const { value, key, parent, stack } = parsedElementInfo; // TODO process element ``` -------------------------------- ### Stream Parsing JSON with JSONParser (Node.js) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/README.md Shows how to import and use the JSONParser from '@streamparser/json-node' for stream parsing. It includes an example of piping an input stream to the JSONParser and handling data, errors, and end events. ```javascript import { JSONParser } from '@streamparser/json-node'; const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$'] }); inputStream.pipe(jsonparser).pipe(destinationStream); // Or using events to get the values parser.on("data", (value) => { /* ... */ }); parser.on("error", err => { /* ... */ }); parser.on("end", () => { /* ... */ }); ``` -------------------------------- ### Stream Parsing Fetch Request (JSON Objects) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/README.md Example of stream-parsing a fetch request that returns multiple JSON objects sequentially. It pipes the response body through a JSONParser and reads the individual objects. ```javascript import { JSONParser} from '@streamparser/json-whatwg'; const parser = new JSONParser(); const response = await fetch('http://example.com/'); const reader = response.body.pipeThrough(parser).getReader(); while(true) { const { done, value } = await reader.read(); if (done) break; // TODO process element ``` -------------------------------- ### Configure Tokenizer Options (Node.js) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md This example details the available options for configuring the Tokenizer component. These options control buffering behavior for strings and numbers, define custom separators between JSON objects, and determine whether to emit partial tokens during parsing. ```javascript { stringBufferSize: , // set to 0 to don't buffer. Min valid value is 4. numberBufferSize: , // set to 0 to don't buffer. separator: , // separator between object. For example `\n` for nd-js. emitPartialTokens: // whether to emit tokens mid-parsing. } ``` -------------------------------- ### Parsing JSON Stream with JSONParser Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/README.md Provides an example of parsing a JSON stream using JSONParser. It enqueues data, pipes it through the parser, and reads the results. Errors during parsing are caught and logged. ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const inputStream = new ReadableStream({ async start(controller) { controller.enqueue(parser.write('"Hello world!"')); // will log "Hello world!" // Or passing the stream in several chunks parser.write('"'); parser.write('Hello'); parser.write(' '); parser.write('world!'); parser.write('"');// will log "Hello world!" controller.close(); }, }); const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$'] }); const reader = inputStream.pipeThrough(jsonparser).getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; console.log(value); } ``` ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const inputStream = new ReadableStream({ async start(controller) { controller.enqueue(parser.write('"""')); // will log "Hello world!" controller.close(); }, }); const parser = new JSONParser({ stringBufferSize: undefined }); try { const reader = inputStream.pipeThrough(parser).getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; console.log(value); } } catch (err) { console.log(err); // logs } ``` -------------------------------- ### Stream Parsing JSON Objects from Fetch API Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md An example of how to stream parse multiple JSON objects returned sequentially from a fetch request. It uses a reader to read chunks from the response body and writes them to the JSONParser. ```javascript import { JSONParser} from '@streamparser/json'; const parser = new JSONParser(); parser.onValue = (value, key, parent, stack) => { if (stack > 0) return; // ignore inner values // TODO process element }; const response = await fetch('http://example.com/'); const reader = response.body.getReader(); while(true) { const { done, value } = await reader.read(); if (done) break; jsonparser.write(value); } ``` -------------------------------- ### Assembling JSON Tokens into Values with TokenParser Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Demonstrates how to combine a Tokenizer and a TokenParser to transform a stream of tokens back into structured JavaScript values. The TokenParser can also utilize path selectors to filter which values are reconstructed and emitted. The example shows parsing a JSON string and collecting the reconstructed objects, with callbacks for value emission and end-of-parsing. ```javascript import { Tokenizer, TokenParser, TokenType } from '@streamparser/json'; const tokenizer = new Tokenizer(); const tokenParser = new TokenParser({ paths: ['$.data.*'], keepStack: true }); tokenizer.onToken = tokenParser.write.bind(tokenParser); const results = []; tokenParser.onValue = ({ value, key, parent }) => { results.push({ key, value }); }; tokenParser.onEnd = () => { console.log('Parsed items:', results); }; const json = '{"data":{"id":1,"name":"Test"},"meta":"info"}'; tokenizer.write(json); tokenizer.end(); // Output: // Parsed items: [ // { key: 'id', value: 1 }, // { key: 'name', value: 'Test' } // ] ``` -------------------------------- ### Stream Parse JSON Objects from Fetch Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/dist/deno/README.md Example of streaming parsing JSON objects from a fetch request. This is useful when an endpoint returns multiple JSON objects consecutively without delimiters. ```javascript import { JSONParser} from '@streamparser/json-whatwg'; const parser = new JSONParser(); const response = await fetch('http://example.com/'); const reader = response.body.pipeThrough(parser).getReader(); while(true) { const { done, value } = await reader.read(); if (done) break; // TODO process element } ``` -------------------------------- ### Stream-parsing JSON Objects from Fetch Request (Node.js) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md Example of stream-parsing individual JSON objects sent sequentially from a fetch request in Node.js. It uses the JSONParser and handles the 'data' event to process each parsed element. ```javascript import { JSONParser} from '@streamparser/json-node'; const parser = new JSONParser(); const response = await fetch('http://example.com/'); const reader = response.body.pipe(parser); reader.on('data', value => /* process element */); ``` -------------------------------- ### Stream Parsing JSON Objects with Fetch API in JavaScript Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Provides an example of how to stream-parse a series of JSON objects returned from a fetch request. It configures the JSONParser to process individual objects and includes logic to iterate through the response body using a ReadableStream reader. ```javascript import { JSONParser } from '@streamparser/json'; const parser = new JSONParser(); parser.onValue = (value, key, parent, stack) => { if (stack > 0) return; // ignore inner values // TODO process element }; const response = await fetch('http://example.com/'); const reader = response.body.getReader(); while(true) { const { done, value } = await reader.read(); if (done) break; jsonparser.write(value); } ``` -------------------------------- ### Stream Parse JSON Array from Fetch Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/dist/deno/README.md Example of streaming parsing a JSON array from a fetch request. This scenario handles endpoints that return a large JSON array, processing each element as it becomes available. ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$.*'], keepStack: false }); const response = await fetch('http://example.com/'); const reader = response.body.pipeThrough(parser).getReader(); while(true) { const { done, value: parsedElementInfo } = await reader.read(); if (done) break; const { value, key, parent, stack } = parsedElementInfo; // TODO process element } ``` -------------------------------- ### Low-Level JSON Tokenization with Tokenizer Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Shows how to use the Tokenizer class to break down a JSON string into individual tokens such as braces, brackets, strings, and numbers. This provides a low-level view of the JSON structure, useful for custom parsing logic. The example configures buffer sizes and logs each token's type, value, and offset as it's processed. ```javascript import { Tokenizer, TokenType } from '@streamparser/json'; const tokenizer = new Tokenizer({ stringBufferSize: 0, numberBufferSize: 0 }); const tokens = []; tokenizer.onToken = ({ token, value, offset }) => { const tokenName = Object.keys(TokenType).find(k => TokenType[k] === token); console.log(`Token: ${tokenName}, Value: ${JSON.stringify(value)}, Offset: ${offset}`); tokens.push({ token: tokenName, value }); }; tokenizer.onEnd = () => { console.log('Tokenization complete'); }; tokenizer.write('{"count":42}'); tokenizer.end(); // Output: // Token: LEFT_BRACE, Value: "{", Offset: 0 // Token: STRING, Value: "count", Offset: 1 // Token: COLON, Value: ":", Offset: 8 // Token: NUMBER, Value: 42, Offset: 9 // Token: RIGHT_BRACE, Value: "}", Offset: 11 ``` -------------------------------- ### Parse JSON Stream with JSONParser (Node.js) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md This example demonstrates how to use the JSONParser to parse a JSON stream in Node.js. It pipes the input stream through the parser and into a destination stream. Alternatively, event listeners can be used to process parsed data, errors, and stream completion. ```javascript import { JSONParser } from '@streamparser/json-node'; const parser = new JSONParser(); inputStream.pipe(jsonparser).pipe(destinationStream); // Or using events to get the values parser.on("data", (value) => { /* ... */ }); parser.on("error", err => { /* ... */ }); parser.on("end", () => { /* ... */ }); ``` -------------------------------- ### Stream-parsing Long String with Previews (Node.js) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md Example of stream-parsing a very long string sent from a fetch request in Node.js, allowing for previews of the string while it's being parsed. It utilizes the 'partial' flag in the 'data' event. ```javascript import { JSONParser } from '@streamparser/json-node'; const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$.*'], keepStack: false }); const response = await fetch('http://example.com/'); const reader = response.body.pipe(parse).getReader(); reader.on('data', ({ value, key, parent, stack, partial }) => { if (partial) { console.log(`Parsing value: ${value}... (still parsing)`); } else { console.log(`Value parsed: ${value}`); } }); ``` -------------------------------- ### Stream Parsing JSON Array from Fetch Request (JavaScript) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/README.md Demonstrates how to stream-parse a JSON array where objects are sent one after another. This example configures JSONParser with specific paths and options to correctly parse array elements. ```javascript import { JSONParser } from '@streamparser/json-node'; const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$.*'], keepStack: false }); const response = await fetch('http://example.com/'); const reader = response.body.pipe(parse).getReader(); reader.on('data', ({ value, key, parent, stack }) => /* process element */) ``` -------------------------------- ### JSON Parsing with Path Selectors using JSONParser Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Illustrates how to use JSONParser with path selectors to filter and emit specific values from a JSON stream. This method is memory-efficient as only the selected data is processed. The example shows how to target elements within an array using a JSONPath-like syntax and provides callbacks for value emission and end-of-parsing. ```javascript import { JSONParser } from '@streamparser/json'; // Only emit items from the "users" array const parser = new JSONParser({ paths: ['$.users.*'], keepStack: false // More memory efficient }); let userCount = 0; parser.onValue = ({ value, key }) => { console.log(`User ${key}:`, value.name, value.email); userCount++; }; parser.onEnd = () => { console.log(`Processed ${userCount} users`); }; const jsonData = JSON.stringify({ status: 'success', users: [ { name: 'Alice', email: 'alice@example.com' }, { name: 'Bob', email: 'bob@example.com' } ], meta: { count: 2 } }); parser.write(jsonData); parser.end(); // Output: // User 0: Alice alice@example.com // User 1: Bob bob@example.com // Processed 2 users ``` -------------------------------- ### Stream Parsing JSON Objects from Fetch Request (JavaScript) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/README.md An example of stream-parsing multiple JSON objects sent sequentially from a fetch request. It utilizes the JSONParser to process the response body and extracts individual data values. ```javascript import { JSONParser} from '@streamparser/json-node'; const parser = new JSONParser(); const response = await fetch('http://example.com/'); const reader = response.body.pipe(parser); reader.on('data', value => /* process element */); ``` -------------------------------- ### Node.js: Integrate JSON Parsing into Stream Pipelines Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Integrates JSON parsing into Node.js stream pipelines for file processing and data transformation. This example demonstrates piping a readable stream through a `JSONParser` and a custom `Transform` stream to filter and format records before writing to an output file. It uses `@streamparser/json-node` for Node.js specific stream handling. ```javascript import { createReadStream, createWriteStream } from 'fs'; import { Transform } from 'stream'; import { JSONParser } from '@streamparser/json-node'; // Parse large JSON file with array of objects const parser = new JSONParser({ paths: ['$.records.*'], keepStack: false }); // Transform stream to process each record const processor = new Transform({ objectMode: true, transform(chunk, encoding, callback) { const { value } = chunk; // Process and transform each record if (value.active && value.score > 50) { this.push(JSON.stringify(value) + '\n'); } callback(); } }); const inputStream = createReadStream('large-data.json'); const outputStream = createWriteStream('filtered-data.ndjson'); inputStream .pipe(parser) .pipe(processor) .pipe(outputStream) .on('finish', () => { console.log('Processing complete'); }) .on('error', (err) => { console.error('Pipeline error:', err); }); ``` -------------------------------- ### JSON Parsing Error Handling and Recovery in JavaScript Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Illustrates how to handle parsing errors gracefully using the `onError` event handler in JSONParser. When an error occurs, the `onError` callback is invoked with an error object, and the parser typically cannot continue processing. This example shows catching errors during `write` operations and inspecting the parser's state. Dependencies include JSONParser from '@streamparser/json'. ```javascript import { JSONParser } from '@streamparser/json'; const parser = new JSONParser(); const results = []; let parseError = null; parser.onValue = ({ value }) => { results.push(value); }; parser.onError = (err) => { parseError = err; console.error('Parse error occurred:', err.message); // Parser cannot continue after error console.error('Parser ended:', parser.isEnded); }; parser.onEnd = () => { console.log('Parsing ended normally'); }; try { parser.write('{"valid":true}'); console.log('First write successful'); // This will cause an error parser.write('{"invalid": "}'); console.log('Second write successful'); // Won't reach here } catch (err) { console.error('Caught error during write:', err.message); } console.log('Results before error:', results); console.log('Parse error:', parseError?.message); // Output: // First write successful // Parse error occurred: Unexpected "}" at position "11" in state STRING_DEFAULT // Parser ended: false // Caught error during write: Unexpected "}" at position "11" in state STRING_DEFAULT // Results before error: [ true ] ``` -------------------------------- ### Initialize Tokenizer with Options Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/dist/deno/README.md Shows the initialization of the `Tokenizer` component from `@streamparser/json-whatwg`. It accepts optional configuration for buffering, separators, and partial token emission, along with standard WHATWG Stream writable and readable strategies. ```javascript import { Tokenizer } from '@streamparser/json-whatwg'; const tokenizer = new Tokenizer(opts, writableStrategy, readableStrategy); ``` -------------------------------- ### Initialize JSONParser (JavaScript) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Demonstrates the basic initialization of the JSONParser, which acts as a convenient wrapper for combining Tokenizer and TokenParser functionalities. ```javascript import { JSONParser } from '@streamparser/json'; const parser = new JSONParser(); ``` -------------------------------- ### Initialize TokenParser with Options (JavaScript) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Illustrates the initialization of a TokenParser instance with configuration options. The options allow for filtering emitted paths, controlling stack memory usage, defining value separators, and enabling partial value emission. ```javascript import { TokenParser} from '@streamparser/json'; const tokenParser = new TokenParser(opts); ``` -------------------------------- ### Initialize JSONParser Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md Initializes a JSONParser instance, which acts as a convenient wrapper for combining Tokenizer and TokenParser. It accepts the same options as the Tokenizer. ```javascript import { JSONParser } from "https://deno.land/x/streamparser_json@v0.0.21/index.ts"; const parser = new JSONParser(); ``` -------------------------------- ### Create JSON Tokenizer with Options Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/README.md Shows how to instantiate the Tokenizer component, which parses a UTF-8 stream into JSON tokens. It details the available options for configuring buffering, separators, and partial token emission. ```javascript import { Tokenizer } from '@streamparser/json-whatwg'; const tokenizer = new Tokenizer(opts, writableStrategy, readableStrategy); ``` -------------------------------- ### Initialize JSONParser Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/dist/deno/README.md Initializes a JSONParser, which is a complete JSON parser that chains a Tokenizer and a TokenParser. This allows for comprehensive parsing of JSON data streams. ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const parser = new JSONParser(); ``` -------------------------------- ### Initialize JSONParser (Node.js) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md This snippet demonstrates the initialization of the main JSONParser class, which orchestrates the functionality of both the Tokenizer and TokenParser to provide a complete JSON stream parsing solution. ```javascript import { JSONParser } from '@streamparser/json-node'; const parser = new JSONParser(); ``` -------------------------------- ### Initialize JSONParser Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/README.md Initializes a full-blown JSONParser, which chains a Tokenizer and a TokenParser for comprehensive JSON stream parsing. ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const parser = new JSONParser(); ``` -------------------------------- ### Basic JSON Parsing with JSONParser in Deno Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md Illustrates the basic usage of the JSONParser in Deno for parsing a JSON string. It shows how to instantiate the parser, set the onValue callback, and write data to the parser. ```typescript import { JSONParser } from "https://deno.land/x/streamparser_json@v0.0.21/index.ts"; const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$'] }); parser.onValue = console.log; parser.write('"Hello world!"'); // logs "Hello world!" // Or passing the stream in several chunks parser.write('"'); parser.write('Hello'); parser.write(' '); parser.write('world!'); parser.write('"'); // logs "Hello world!" ``` -------------------------------- ### Initialize TokenParser with Options Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/README.md This snippet demonstrates how to create an instance of the TokenParser, which processes JSON tokens generated by the Tokenizer and emits complete JSON values. It accepts options for path filtering, stack management, custom separators, and partial value emission. ```javascript import { TokenParser} from '@streamparser/json-node'; const tokenParser = new TokenParser(opts, writableStrategy, readableStrategy); ``` -------------------------------- ### Basic JSONParser Usage in JavaScript Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Shows the fundamental usage of the JSONParser class, including instantiation, setting up event listeners (onValue), and writing data to the parser. It highlights how the parser handles string data, including data split across multiple chunks. ```javascript import { JSONParser } from '@streamparser/json'; const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$'] }); parser.onValue = console.log; parser.write("\"Hello world!\""); // logs "Hello world!" // Writing data in chunks: parser.write("\""); parser.write('Hello'); parser.write(' '); parser.write('world!'); parser.write("\""); // logs "Hello world!" ``` -------------------------------- ### Initialize JSONParser for Full JSON Parsing Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/README.md This snippet shows the simplest way to initialize the JSONParser, which combines both the Tokenizer and TokenParser to handle the complete JSON stream parsing process. No additional arguments are needed for basic functionality. ```javascript import { JSONParser } from '@streamparser/json-node'; const parser = new JSONParser(); ``` -------------------------------- ### Initialize Tokenizer with Options Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Shows how to initialize the Tokenizer class from the @streamparser/json library. The Tokenizer is responsible for parsing a UTF-8 stream into JSON tokens. It accepts an options object to configure buffering, separators, and partial token emission. ```javascript import { Tokenizer } from '@streamparser/json'; const tokenizer = new Tokenizer(opts); ``` -------------------------------- ### Initialize TokenParser Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/README.md Initializes a TokenParser for processing JSON tokens and emitting JSON values. It accepts options, writable strategy, and readable strategy which are standard WhatWG Stream settings. ```javascript import { TokenParser} from '@streamparser/json-whatwg'; const tokenParser = new TokenParser(opts, writableStrategy, readableStrategy); ``` -------------------------------- ### Manually Connect Tokenizer and TokenParser Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md Illustrates the manual connection between a Tokenizer and a TokenParser instance. This approach involves explicitly setting the `onToken` handler of the tokenizer to the `write` method of the token parser, and defining the `onValue` handler for the token parser. ```javascript const tokenizer = new Tokenizer(opts); const tokenParser = new TokenParser(); tokenizer.onToken = tokenParser.write.bind(tokenParser); tokenParser.onValue = (value) => { /* Process values */ } ``` -------------------------------- ### Tokenizer and TokenParser Integration in JavaScript Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Illustrates how the Tokenizer and TokenParser components can be used independently. It demonstrates connecting the output of the Tokenizer's `onToken` event to the input of the TokenParser's `write` method, enabling a modular approach to JSON parsing. ```javascript const tokenizer = new Tokenizer(opts); const tokenParser = new TokenParser(); tokenizer.onToken = tokenParser.write.bind(tokenParser); ``` -------------------------------- ### Stream-parse Fetch Response with JSON previews (JavaScript) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/dist/deno/README.md This snippet demonstrates how to use the JSONParser from @streamparser/json-whatwg to parse a streaming JSON response from a fetch request. It allows for real-time previews of parsed data, handling both partial and complete values. Dependencies include the @streamparser/json-whatwg library. Input is a fetch response body, and output is logged to the console. ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$.*'], keepStack: false }); const response = await fetch('http://example.com/'); const reader = response.body.pipeThrough(parser).getReader(); while(true) { const { done, value: parsedElementInfo } = await reader.read(); if (done) break; const { value, key, parent, stack, partial } = parsedElementInfo; if (partial) { console.log(`Parsing value: ${value}... (still parsing)`); } else { console.log(`Value parsed: ${value}`); } } ``` -------------------------------- ### Use Tokenizer for JSON Token Stream in JavaScript Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md Shows how to instantiate and use the Tokenizer component to parse a UTF-8 stream into JSON tokens. It also outlines the available configuration options for the tokenizer, including buffer sizes and separators. ```javascript import { Tokenizer } from "https://deno.land/x/streamparser_json@v0.0.21/index.ts"; const tokenizer = new Tokenizer(opts); ``` -------------------------------- ### Initialize Tokenizer (Node.js) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md This snippet shows how to create an instance of the Tokenizer component from @streamparser/json-node. The Tokenizer is responsible for parsing a UTF-8 stream into JSON tokens. It accepts optional configuration options for buffering and separators. ```javascript import { Tokenizer } from '@streamparser/json-node'; const tokenizer = new Tokenizer(opts, transformOpts); ``` -------------------------------- ### JSON Parsing with JSONParser in Node.js Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md Shows how to instantiate and use the JSONParser class from '@streamparser/json-node' in a Node.js environment. It includes piping the parser to streams and handling events like 'data', 'error', and 'end'. ```javascript import { JSONParser } from '@streamparser/json-node'; const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$'] }); inputStream.pipe(jsonparser).pipe(destinationStream); // Or using events to get the values parser.on("data", (value) => { /* ... */ }); parser.on("error", err => { /* ... */ }); parser.on("end", () => { /* ... */ }); ``` -------------------------------- ### Initialize TokenParser (Node.js) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/dist/deno/README.md This code initializes the TokenParser component, which processes JSON tokens generated by the Tokenizer and emits complete JSON values or objects. It accepts configuration options for specific paths to extract, stack management, and object separators. ```javascript import { TokenParser } from '@streamparser/json-node'; const tokenParser = new TokenParser(opts, writableStrategy, readableStrategy); ``` -------------------------------- ### Initialize Tokenizer with Options Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/node/README.md This snippet shows how to initialize the Tokenizer class, which is responsible for parsing a UTF-8 stream into JSON tokens. It accepts optional configuration parameters for buffering and custom separators. ```javascript import { Tokenizer } from '@streamparser/json-node'; const tokenizer = new Tokenizer(opts, transformOpts); ``` -------------------------------- ### Connect Tokenizer and TokenParser Manually (JavaScript) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Illustrates how to manually connect a Tokenizer instance with a TokenParser instance to process JSON streams. This involves setting the onToken callback of the tokenizer to the write method of the token parser and defining the onValue handler for the token parser. ```javascript const tokenizer = new Tokenizer(opts); const tokenParser = new TokenParser(); tokenizer.onToken = tokenParser.write.bind(tokenParser); tokenizer.onError = tokenParser.onError.bind(tokenParser); tokenParser.onValue = (value) => { /* Process values */ }; tokenParser.onError = (err) => { /* Handle errors */ }; ``` -------------------------------- ### JSONParser - Basic Streaming Parse Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Demonstrates how to use the JSONParser to parse JSON data in chunks using a callback-based API. It emits values as parsing progresses and handles errors and completion. ```APIDOC ## JSONParser - Basic Streaming Parse ### Description Parse JSON data in chunks using callback-based API with value emission as parsing progresses. ### Method N/A (Class instantiation and method calls) ### Endpoint N/A ### Parameters #### Constructor Options - **paths** (string[]) - Optional - JSONPath-style selectors to filter emitted values. - **keepStack** (boolean) - Optional - Whether to keep the parser's stack, useful for context but consumes more memory. ### Request Example ```javascript import { JSONParser } from '@streamparser/json'; const parser = new JSONParser(); parser.onValue = ({ value, key, parent, stack }) => { console.log('Parsed value:', value); console.log('Key:', key); console.log('Parent:', parent); console.log('Stack depth:', stack.length); }; parser.onError = (err) => { console.error('Parse error:', err.message); }; parser.onEnd = () => { console.log('Parsing complete'); }; try { parser.write('{"name":"John",'); parser.write('"age":30,'); parser.write('"active":true}'); parser.end(); } catch (err) { console.error('Error:', err); } ``` ### Response #### Events - **onValue**: ({ value, key, parent, stack }) - Emitted when a complete JSON value is parsed. - **onError**: (err) - Emitted when a parsing error occurs. - **onEnd**: () - Emitted when the entire stream has been parsed. ``` -------------------------------- ### Parse JSON Stream with JSONParser in JavaScript Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md Demonstrates how to use the JSONParser to parse a JSON stream by writing chunks of data to it. The `onValue` callback is invoked for each complete JSON value encountered. Error handling is also shown. ```javascript import { JSONParser } from "https://deno.land/x/streamparser_json@v0.0.21/index.ts"; const parser = new JSONParser(); parser.onValue = ({ value }) => { /* process data */ }; // Or passing the stream in several chunks try { parser.write('{ "test": ["a"] }'); // onValue will be called 3 times: // "a" // ["a"] // { test: ["a"] } } catch (err) { console.log(err); // handler errors } ``` -------------------------------- ### Basic Streaming JSON Parsing with JSONParser Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Demonstrates the basic usage of JSONParser to parse JSON data in chunks using a callback-based API. It emits values as parsing progresses and logs details like the value, key, parent, and stack depth. Error handling and end-of-parsing notifications are also included. This is useful for processing JSON streams without loading the entire data into memory. ```javascript import { JSONParser } from '@streamparser/json'; const parser = new JSONParser(); parser.onValue = ({ value, key, parent, stack }) => { console.log('Parsed value:', value); console.log('Key:', key); console.log('Parent:', parent); console.log('Stack depth:', stack.length); }; parser.onError = (err) => { console.error('Parse error:', err.message); }; parser.onEnd = () => { console.log('Parsing complete'); }; try { parser.write('{"name":"John",'); parser.write('"age":30,'); parser.write('"active":true}'); parser.end(); } catch (err) { console.error('Error:', err); } // Output: // Parsed value: John // Parsed value: 30 // Parsed value: true // Parsed value: { name: 'John', age: 30, active: true } ``` -------------------------------- ### Extend JSONParser in JavaScript Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md Demonstrates how to extend the JSONParser class in JavaScript to override specific event handler methods like onToken and onValue for custom processing. ```javascript class MyJsonParser extends JSONParser { onToken(value: any) { // ... } onValue(value: any) { // ... } } const myJsonParser = new MyJsonParser(); // or just overriding it const jsonParser = new JSONParser(); jsonparser.onToken = (token, value, offset) => { ... }; jsonparser.onValue = (value) => { ... }; ``` -------------------------------- ### Parse JSON Stream with WHATWG TransformStream Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/dist/deno/README.md Demonstrates how to use the `JSONParser` to process a ReadableStream and pipe it through a WHATWG TransformStream. It shows both piping to a destination stream and manually reading values from the stream. Values are processed as they become available. ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const inputStream = new ReadableStream({ async start(controller) { controller.enqueue('{ "test": ["a"] }'); controller.close(); }, }); const parser = new JSONParser(); const reader = inputStream.pipeThrough(parser).getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; processValue(value); // There will be 3 value: // "a" // ["a"] // { test: ["a"] } } ``` -------------------------------- ### Error Handling in JSONParser (Thrown Exception) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md Demonstrates how to handle errors during JSON stream parsing by using a try-catch block when calling the write method. Errors are thrown synchronously. ```typescript import { JSONParser } from "https://deno.land/x/streamparser_json@v0.0.21/index.ts"; const parser = new JSONParser({ stringBufferSize: undefined }); parser.onValue = console.log; try { parser.write('"""'); } catch (err) { console.log(err); // logs } ``` -------------------------------- ### Handle Parsing Errors with JSONParser Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/dist/deno/README.md Demonstrates how to handle potential errors during JSON parsing when using the JSONParser. It wraps the stream processing in a try-catch block to log any errors that occur. ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const inputStream = new ReadableStream({ async start(controller) { controller.enqueue(parser.write('"""')); // will log "Hello world!" controller.close(); }, }); const parser = new JSONParser({ stringBufferSize: undefined }); try { const reader = inputStream.pipeThrough(parser).getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; console.log(value); } } catch (err) { console.log(err); // logs } ``` -------------------------------- ### JSONParser with Path Selectors Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Shows how to configure JSONParser with path selectors to filter which parts of the JSON are emitted, optimizing for memory efficiency when only specific data is needed. ```APIDOC ## JSONParser with Path Selectors ### Description Filter which values are emitted during parsing using JSONPath-style selectors for memory-efficient processing. ### Method N/A (Class instantiation and method calls) ### Endpoint N/A ### Parameters #### Constructor Options - **paths** (string[]) - Required - JSONPath-style selectors to filter emitted values. Example: `['$.users.*']` to emit all items within the 'users' array. - **keepStack** (boolean) - Optional - Defaults to true. Set to `false` for more memory efficiency if the parsing context (parent/stack) is not needed. ### Request Example ```javascript import { JSONParser } from '@streamparser/json'; // Only emit items from the "users" array const parser = new JSONParser({ paths: ['$.users.*'], keepStack: false // More memory efficient }); let userCount = 0; parser.onValue = ({ value, key }) => { console.log(`User ${key}:`, value.name, value.email); userCount++; }; parser.onEnd = () => { console.log(`Processed ${userCount} users`); }; const jsonData = JSON.stringify({ status: 'success', users: [ { name: 'Alice', email: 'alice@example.com' }, { name: 'Bob', email: 'bob@example.com' } ], meta: { count: 2 } }); parser.write(jsonData); parser.end(); ``` ### Response #### Events - **onValue**: ({ value, key }) - Emitted only for values matching the specified `paths`. - **onError**: (err) - Emitted when a parsing error occurs. - **onEnd**: () - Emitted when the entire stream has been parsed. ``` -------------------------------- ### Stream Parsing JSON Array from Fetch API Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md Demonstrates how to stream parse a large JSON array returned from a fetch request. It configures the JSONParser to target array elements using a path and processes each element as it's parsed. ```typescript import { JSONParser } from "https://deno.land/x/streamparser_json@v0.0.21/index.ts"; const jsonparser = new JSONParser({ stringBufferSize: undefined, paths: ['$.*'] }); jsonparser.onValue = ({ value, key, parent, stack }) => { // TODO process element }; const response = await fetch('http://example.com/'); const reader = response.body.getReader(); while(true) { const { done, value } = await reader.read(); if (done) break; jsonparser.write(value); } ``` -------------------------------- ### Parse JSON Stream with JSONParser Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Demonstrates how to use the JSONParser class to parse a JSON stream in chunks. The `onValue` callback is triggered for each parsed JSON value, and errors can be caught using a try-catch block. ```javascript import { JSONParser } from '@streamparser/json'; const parser = new JSONParser(); parser.onValue = ({ value }) => { /* process data */ }; // Or passing the stream in several chunks try { parser.write('{ "test": ["a"] }'); // onValue will be called 3 times: // "a" // ["a"] // { test: ["a"] } } catch (err) { console.log(err); // handler errors } ``` -------------------------------- ### Extend Tokenizer for Custom Parsing Logic (JavaScript) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Demonstrates how to create a custom Tokenizer by extending the base class and overriding methods like parseNumber and onToken. This allows for modified number parsing and custom token emission logic. ```javascript class MyTokenizer extends Tokenizer { parseNumber(numberStr) { const number = super.parseNumber(numberStr); // if number is too large. Just keep the string. return Number.isFinite(numberStr) ? number : numberStr; } onToken({ token, value }) { if (token = TokenTypes.NUMBER && typeof value === 'string') { super(TokenTypes.STRING, value); } else { super(token, value); } } } const myTokenizer = new MyTokenizer(); ``` -------------------------------- ### Stream Parsing JSON Array with Fetch API in JavaScript Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Demonstrates how to stream-parse a JSON array from a fetch request. It utilizes the `paths` option in the JSONParser constructor to specify that array elements should be processed, and iterates through the response body chunks. ```javascript import { JSONParser } from '@streamparser/json'; const jsonparser = new JSONParser({ stringBufferSize: undefined, paths: ['$.*'] }); jsonparser.onValue = ({ value, key, parent, stack }) => { // TODO process element }; const response = await fetch('http://example.com/'); const reader = response.body.getReader(); while(true) { const { done, value } = await reader.read(); if (done) break; jsonparser.write(value); } ``` -------------------------------- ### Extending JSONParser in JavaScript Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Demonstrates how to extend the JSONParser class to override its event handler methods like onToken and onValue. This allows for custom logic when specific tokens or values are encountered during parsing. ```javascript class MyJsonParser extends JSONParser { onToken(value: any) { // ... custom logic } onValue(value: any) { // ... custom logic } } const myJsonParser = new MyJsonParser(); // Alternatively, overriding methods on an instance: const jsonParser = new JSONParser(); jsonParser.onToken = (token, value, offset) => { /* ... */ }; jsonParser.onValue = (value) => { /* ... */ }; ``` -------------------------------- ### Extend TokenParser for Custom Value Handling (JavaScript) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/README.md Demonstrates creating a custom TokenParser by extending the base class and overriding the onValue method. This allows for custom processing of parsed JSON values. ```javascript class MyTokenParser extends TokenParser { onValue(value) { // ... } } const myTokenParser = new MyTokenParser(); ``` -------------------------------- ### Multiple Path Selectors with Wildcards in JavaScript Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Demonstrates using multiple path selectors, including wildcards, to selectively extract specific data from complex JSON structures. The `paths` option in JSONParser allows defining patterns to match desired elements. The `keepStack` option is used to analyze the path of extracted values. Dependencies include JSONParser from '@streamparser/json'. ```javascript import { JSONParser } from '@streamparser/json'; const parser = new JSONParser({ paths: [ '$.users.*.email', // All user emails '$.metadata.version', // Specific metadata field '$.settings.*' // All settings ], keepStack: true }); const captured = { emails: [], version: null, settings: {} }; parser.onValue = ({ value, key, stack }) => { // Determine which path matched based on stack const path = stack.map(s => s.key).filter(k => k !== undefined); if (path[0] === 'users' && path[2] === 'email') { captured.emails.push(value); } else if (path[0] === 'metadata' && path[1] === 'version') { captured.version = value; } else if (path[0] === 'settings') { captured.settings[key] = value; } }; const json = JSON.stringify({ users: [ { name: 'Alice', email: 'alice@example.com', age: 30 }, { name: 'Bob', email: 'bob@example.com', age: 25 } ], metadata: { version: '1.0.0', created: '2024-01-01' }, settings: { theme: 'dark', notifications: true } }); parser.write(json); parser.end(); console.log('Captured data:', captured); // Output: // Captured data: { // emails: [ 'alice@example.com', 'bob@example.com' ], // version: '1.0.0', // settings: { theme: 'dark', notifications: true } // } ``` -------------------------------- ### Tokenizer - Low-Level Token Stream Source: https://context7.com/juanjodiaz/streamparser-json/llms.txt Provides a low-level interface to process JSON as individual tokens (strings, numbers, braces, brackets). Useful for custom parsing logic or debugging. ```APIDOC ## Tokenizer - Low-Level Token Stream ### Description Process JSON as individual tokens (strings, numbers, braces, brackets) for custom parsing logic. ### Method N/A (Class instantiation and method calls) ### Endpoint N/A ### Parameters #### Constructor Options - **stringBufferSize** (number) - Optional - Size of the buffer for accumulating string characters. Defaults to 0. - **numberBufferSize** (number) - Optional - Size of the buffer for accumulating number digits. Defaults to 0. ### Request Example ```javascript import { Tokenizer, TokenType } from '@streamparser/json'; const tokenizer = new Tokenizer({ stringBufferSize: 0, numberBufferSize: 0 }); const tokens = []; tokenizer.onToken = ({ token, value, offset }) => { const tokenName = Object.keys(TokenType).find(k => TokenType[k] === token); console.log(`Token: ${tokenName}, Value: ${JSON.stringify(value)}, Offset: ${offset}`); tokens.push({ token: tokenName, value }); }; tokenizer.onEnd = () => { console.log('Tokenization complete'); }; tokenizer.write('{"count":42}'); tokenizer.end(); ``` ### Response #### Events - **onToken**: ({ token, value, offset }) - Emitted for each recognized JSON token. `token` is a `TokenType` enum value, `value` is the token's content, and `offset` is its position in the input stream. - **onError**: (err) - Emitted when a tokenization error occurs. - **onEnd**: () - Emitted when the entire stream has been processed into tokens. ``` -------------------------------- ### Error Handling in JSONParser (Callback) Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/plainjs/dist/deno/README.md Shows how to handle errors during JSON stream parsing using the onError callback method provided by the JSONParser. ```typescript import { JSONParser } from "https://deno.land/x/streamparser_json@v0.0.21/index.ts"; const parser = new JSONParser({ stringBufferSize: undefined }); parser.onValue = console.log; parser.onError = console.error; parser.write('"""'); ``` -------------------------------- ### Parse JSON Stream with JSONParser Source: https://github.com/juanjodiaz/streamparser-json/blob/main/packages/whatwg/dist/deno/README.md Shows how to use the JSONParser to process a ReadableStream of JSON data. It enqueues data chunks using the parser's write method and reads the parsed values from the stream. ```javascript import { JSONParser } from '@streamparser/json-whatwg'; const inputStream = new ReadableStream({ async start(controller) { controller.enqueue(parser.write('"Hello world!"')); // will log "Hello world!" // Or passing the stream in several chunks parser.write('"'); parser.write('Hello'); parser.write(' '); parser.write('world!'); parser.write('"');// will log "Hello world!" controller.close(); }, }); const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$'] }); const reader = inputStream.pipeThrough(jsonparser).getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; console.log(value); } ```