export type JSONType = T extends Array ? 'array' : T extends Record ? 'object' : T extends string ? 'string' : T extends number ? 'number' : T extends boolean ? 'boolean' : T extends null ? 'null' : 'array' | 'object' | 'string' | 'number' | 'boolean' | 'null'; export type JSONProxy = T extends Record ? { [P in keyof T]: JSONProxy; } & { [JSON.symbolToObject]: () => T; [JSON.symbolToObjectAsync]: () => Promise; [JSON.symbolType]: JSONType; } : T; export type RFC6901, PATH extends string> = PATH extends `/${infer PROP}/${infer SUB}` ? RFC6901 : PATH extends `/${infer PROP}` ? JSON : JSON; /** * A binary representation of a JSON element */ export class JSON { constructor(); /** * The underlying type of the JSON element. * * @type {'object' | 'array' | 'string' | 'number' | 'boolean' | 'null'} */ type: JSONType; /** * Parse a string and return its binary representation. * * Will block the event loop while it parses the JSON. Slightly * slower for small files but faster for larger files compared * to the built-in JSON parser. * * @param {string} text JSON to parse * @returns {JSON} */ static parse(text: string | Buffer): JSON; /** * Parse a string and return its binary representation. * * Unlike the built-in JSON parser, it does not incur any latency * on the event loop while it parses the JSON. Slightly slower for * small files but faster for larger files compared to the built-in * JSON parser. * * @param {string} text JSON to parse * @returns {Promise} */ static parseAsync(text: string | Buffer): Promise>; /** * Retrieve a subtree out of the binary JSON object. * * Subsequent requests for the same element will return a reference * to the same object for as long as the GC hasn't collected it. * * @returns {string | boolean | number | null | Array | Record} */ get(): T extends Record ? { [P in keyof T]: JSON; } : T; /** * Retrieve a subtree out of the binary JSON object * automatically expanding primitive values. * * Subsequent requests for the same element will return a reference * to the same object for as long as the GC hasn't collected it. * * @returns {(JSON | string | boolean | number | null) [] | Record | string | boolean | number | null} */ expand(): T extends Record ? { [P in keyof T]: T[P] extends Record ? JSON : T[P]; } : T; /** * Retrieves a deeply nested JSON element referenced by the RFC6901 JSON pointer. * * This is much faster than recursing down with .get()/.expand() but * it will still have an O(n) complexity relative to the arrays and objects * sizes since simdjson stores arrays and objects as lists. * * Subsequent requests for the same element will return a reference * to the same object for as long as the GC hasn't collected it. * * @param {string} rfc6901 RFC6901-conformant JSON pointer * @param {object} [opts={}] Options * @param {boolean} [opts.throwOnError=true] Throw on error when true, return undefined when false * @returns {any} */ path(rfc6901: PATH, opts?: { throwOnError?: boolean }): T extends Record ? RFC6901 : never; /** * Converts the binary representation to a JS object. * * Will block the event loop while the conversion is running. * Significantly slower than the built-in JSON parser but * allows to convert only a small subtree out of a larger * document. * * @returns {any} */ toObject(): T; /** * Converts the binary representation to a JS object. * * Uses the main thread, but periodically yields the CPU * to allow other tasks to run. * * Allows to convert only a small subtree out of a larger * document. * * @returns {Promise} */ toObjectAsync(): Promise; /** * Creates a Proxy object that gives the illusion of a real object. * * This is an instantaneous zero-latency method for creating a * `Proxy` object that works (almost) like a real object but * calls .expand() when needs to retrieve a property. * * @returns {any} */ proxify(): JSONProxy; /** * Allows to change the default latency limit. * * CPU will be yielded every `latency` milliseconds. * * @property {number} * @default 5 */ static latency: number; /** * The currently used simdjson version. * * @property {string} */ static readonly simdjson_version: string; /** * The currently used SIMD implementation. * * @property {string} */ static readonly simd: 'icelake' | 'haswell' | 'westmere' | 'arm64' | 'ppc64' | 'fallback'; /** * Symbol.toObject to be used for Proxies */ static readonly symbolToObject: unique symbol; /** * Symbol.toObjectAsync to be used for Proxies */ static readonly symbolToObjectAsync: unique symbol; /** * Symbol.type to be used for Proxies */ static readonly symbolType: unique symbol; }