/** * @file Read-and-parse helpers for JSON files. Wraps fs reads in actionable * error messages keyed off `ENOENT` / `EACCES` / `EPERM` so callers see "JSON * file not found" / "Permission denied" rather than the bare errno. Both * variants honor `throws: false` to fall back to `undefined` on parse or read * failure. Both variants cache parse results by default — keyed on `path + * ino + size \+ mtimeMs`, with a defensive `structuredClone` on every hit so * callers can mutate the returned object freely. See `_read-json-cache.ts` * for the safety rationale + opt-out controls. */ import { clearReadJsonCache, getReadJsonCacheStats, setReadJsonCacheMax, setReadJsonCacheTtlMs } from './read-json-cache.js'; export { clearReadJsonCache, getReadJsonCacheStats, setReadJsonCacheMax, setReadJsonCacheTtlMs, }; import type { BigIntStats, PathLike, Stats } from 'node:fs'; import type { ReadJsonOptions } from './types.js'; /** * Read and parse a JSON file asynchronously. Reads the file as UTF-8 text and * parses it as JSON. Optionally accepts a reviver function to transform parsed * values. * * @example * ;```ts * // Read and parse package.json * const pkg = await readJson('./package.json') * * // Read JSON with custom reviver * const data = await readJson('./data.json', { * reviver: (key, value) => { * if (key === 'date') return new Date(value) * return value * }, * }) * * // Don't throw on parse errors * const config = await readJson('./config.json', { throws: false }) * if (config === undefined) { * console.log('Failed to parse config') * } * ``` * * @param filepath - Path to JSON file. * @param options - Read and parse options. * * @returns Promise resolving to parsed JSON value, or undefined if throws is * false and an error occurs. */ export declare function readJson(filepath: PathLike, options?: ReadJsonOptions | string | undefined): Promise; export declare function readJsonStatsMatch(before: Stats | BigIntStats, after: Stats | BigIntStats): boolean; /** * Read and parse a JSON file synchronously. Reads the file as UTF-8 text and * parses it as JSON. Optionally accepts a reviver function to transform parsed * values. * * @example * ;```ts * // Read and parse tsconfig.json * const tsconfig = readJsonSync('./tsconfig.json') * * // Read JSON with custom reviver * const data = readJsonSync('./data.json', { * reviver: (key, value) => { * if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}/.test(value)) { * return new Date(value) * } * return value * }, * }) * * // Don't throw on parse errors * const config = readJsonSync('./config.json', { throws: false }) * ``` * * @param filepath - Path to JSON file. * @param options - Read and parse options. * * @returns Parsed JSON value, or undefined if throws is false and an error * occurs. */ export declare function readJsonSync(filepath: PathLike, options?: ReadJsonOptions | string | undefined): import("../json/types.mts").JsonValue | undefined; export declare function throwReadJsonError(filepath: PathLike, error: unknown): never;