/** * JSON Parsing Utilities * * Robust JSON parsing with support for streaming formats and error recovery. * Designed for adapter developers to handle various JSON response formats. * * @module parsers/json-parser */ /** * Options for JSON parsing behavior */ export interface JsonParseOptions { /** Allow newline-delimited JSON (NDJSON) */ allowNDJSON?: boolean; /** Allow multiple top-level JSON values */ allowMultiple?: boolean; /** On parse error, return raw string instead of throwing */ fallbackToString?: boolean; } /** * Parse JSON with support for multiple formats * * Handles: * - Standard JSON objects/arrays * - Newline-delimited JSON (NDJSON) * - Multiple JSON values in sequence * - Malformed JSON (with fallback) * * This function attempts standard JSON parsing first for optimal performance. * If that fails, it tries alternative formats based on the provided options. * * @param text - Raw JSON text to parse * @param options - Parsing options * @returns Parsed JavaScript value(s). For NDJSON or multiple values, returns an array. * @throws Error if parsing fails and fallbackToString is false * * @example * ```typescript * // Standard JSON * parseJSON('{"id": 1}') // Returns: {id: 1} * * // NDJSON * parseJSON('{"id": 1}\n{"id": 2}') // Returns: [{id: 1}, {id: 2}] * * // With options * parseJSON(text, { fallbackToString: true }) // Never throws * * // Disable NDJSON parsing * parseJSON(text, { allowNDJSON: false }) * ``` */ export declare function parseJSON(text: string, options?: JsonParseOptions): unknown; /** * Parse newline-delimited JSON (NDJSON) * * Each line is treated as a separate JSON object. Empty lines and * whitespace-only lines are automatically filtered out. * * This format is commonly used for: * - Streaming API responses * - Log files * - Bulk data exports * - Server-sent events * * @param text - NDJSON text * @returns Array of parsed JSON objects * @throws Error if any line contains invalid JSON * * @example * ```typescript * parseNDJSON('{"id": 1}\n{"id": 2}') * // Returns: [{id: 1}, {id: 2}] * * // Handles empty lines * parseNDJSON('{"id": 1}\n\n{"id": 2}') * // Returns: [{id: 1}, {id: 2}] * * // Handles whitespace * parseNDJSON(' {"id": 1} \n {"id": 2} ') * // Returns: [{id: 1}, {id: 2}] * ``` */ export declare function parseNDJSON(text: string): unknown[]; //# sourceMappingURL=json-parser.d.ts.map