import { IConfig, IParseOptions, IReadOptions, IStringifyOptions, IWriteOptions, Replacer, Reviver } from './interfaces'; /** * Class that provides safe versions of `jsonc` methods. Safe methods provide a * way to easily handle errors without throwing; so that you don't need to use * try/catch blocks. * * Each method (except a few such as `.isJSON`), will return an array with the * first item being the `Error` instance caught. If successful, second item * will be the result. * @name jsonc.safe * @class * * @example * const { safe } = require('jsonc'); * // or * import { safe as jsonc } from 'jsonc'; * * const [err, result] = jsonc.parse('[invalid JSON}'); * if (err) { * console.log(`Failed to parse JSON: ${err.message}`); * } else { * console.log(result); * } */ declare class jsoncSafe { /** * Configures `jsonc` object. * *
This method is added for convenience. Works the same as `jsonc.config()`.* * @name jsonc.safe.config * @function * * @param {IConfig} cfg - Configurations. * @param {NodeJS.WriteStream} [stream] - Stream to write logs to. This is * used with `.log()` and `.logp()` methods. * @param {NodeJS.WriteStream} [streamErr] - Stream to write error logs to. * This is used with `.log()` and `.logp()` methods. * * @example * import { safe as jsonc } from 'jsonc'; * // Output logs to stdout but logs containing errors to a file. * jsonc.config({ * stream: process.stdout, * streamErr: fs.createWriteStream('path/to/log.txt') * }); * jsonc.log({ info: 'this is logged to console' }); * jsonc.log(new Error('this is logged to file')); */ static config(cfg: IConfig): void; /** * Stringifies and logs the given arguments to console. This will * automatically handle circular references; so it won't throw. * * If an `Error` instance is passed, it will log the `.stack` property on * the instance, without stringifying the object. * *
This method is added for convenience. Works the same as `jsonc.log()`.* @name jsonc.safe.log * @function * * @param {...any[]} [args] - Arguments to be logged. * @returns {void} */ static log(...args: any[]): void; /** * Pretty version of `log()` method. Stringifies and logs the given * arguments to console, with indents. This will automatically handle * circular references; so it won't throw. * * If an `Error` instance is passed, it will log the `.stack` property on * the instance, without stringifying the object. * *
This method is added for convenience. Works the same as `jsonc.logp()`.* @name jsonc.safe.logp * @function * * @param {...any[]} [args] - Arguments to be logged. * @returns {void} */ static logp(...args: any[]): void; /** * Safe version of `jsonc.parse()`. Parses the given string into a * JavaScript object. * @name jsonc.safe.parse * @function * * @param {string} str - JSON string to be parsed. * @param {IParseOptions|Reviver} [options] - Either a parse options * object or a reviver function. * @param {Reviver} [options.reviver] - A function that can filter and * transform the results. It receives each of the keys and values, and * its return value is used instead of the original value. If it * returns what it received, then the structure is not modified. If it * returns `undefined` then the member is deleted. * @param {boolean} [options.stripComments=true] - Whether to strip * comments from the JSON string. Note that it will return the first * parameter as an error if this is set to `false` and the string * includes comments. * * @returns {Array} - Safe methods return an array with the * first item being the `Error` instance caught. If successful, second * item will be the result: `[Error, any]` * * @example * import { safe as jsonc } from 'jsonc'; * const [err, result] = jsonc.parse('--invalid JSON--'); * if (err) { * console.log('Failed to parse JSON: ' + err.message); * } else { * console.log(result); * } */ static parse(str: string, options?: IParseOptions | Reviver): [Error, undefined] | [null, any]; /** * Safe version of `jsonc.stringify()`. Stringifies the given * JavaScript object. The input object can have circular references * which will return the string `"[Circular]"` for each circular * reference, by default. You can use a replacer function to replace or * remove circular references instead. * @name jsonc.safe.stringify * @function * * @param {*} value - JavaScript value to be stringified. * @param {IStringifyOptions|Replacer} [options] - Stringify options or * a replacer. * @param {Replacer} [options.replacer] - Determines how object values * are stringified for objects. It can be an array of strings or * numbers; or a function with the following signature: `(key: string, * value: any) => any`. * @param {string|number} [options.space] - Specifies the indentation * of nested structures. If it is omitted, the text will be packed * without extra whitespace. If it is a number, it will specify the * number of spaces to indent at each level. If it is a string (such as * `"\t"` or `" "`), it contains the characters used to indent at * each level. * @param {boolean} [options.handleCircular=true] - Whether to handle * circular references (if any) by replacing their values with the * string `"[Circular]"`. You can also use a replacer function to * replace or remove circular references instead. * @param {string|number} [space] - This takes effect if second * argument is the `replacer` or a falsy value. Included for supporting * the signature of native `JSON.stringify()` method. * * @returns {Array} - Safe methods return an array with the * first item being the `Error` instance caught. If successful, second * item will be the result: `[Error, string]` * * @example * import { safe as jsonc } from 'jsonc'; * const obj = { key: 'value' }; * let [err, str] = jsonc.stringify(obj); * if (!err) console.log(str); // '{"key":"value"}' * * // pretty output with indents * let [err, pretty] = jsonc.stringify(obj, null, 2); * // equivalent to: * [err, pretty] = jsonc.stringify(obj, { reviver: null, space: 2 }); * if (!err) console.log(pretty); * // { * // "key": "value" * // } */ static stringify(value: any, option?: IStringifyOptions): [Error, undefined] | [null, string]; static stringify(value: any, replacer: Replacer, space?: string | number): [Error, undefined] | [null, string]; /** * Specifies whether the given string has well-formed JSON structure. * * Note that, not all JSON-parsable strings are considered well-formed JSON * structures. JSON is built on two structures; a collection of name/value * pairs (object) or an ordered list of values (array). * * For example, `JSON.parse('true')` will parse successfully but * `jsonc.isJSON('true')` will return `false` since it has no object or * array structure. * *
This method is added for convenience. Works the same as * `jsonc.isJSON()`.* @name jsonc.safe.isJSON * @function * * @param {string} str - String to be validated. * @param {boolean} [allowComments=false] - Whether comments should be * considered valid. * * @returns {boolean} * * @example * import { safe as jsonc } from 'jsonc'; * jsonc.isJSON('{"x":1}'); // true * jsonc.isJSON('true'); // false * jsonc.isJSON('[1, false, null]'); // true * jsonc.isJSON('string'); // false * jsonc.isJSON('null'); // false */ static isJSON(str: string, allowComments?: boolean): boolean; /** * Strips comments from the given JSON string. * @name jsonc.safe.stripComments * @function * * @param {string} str - JSON string. * @param {boolean} [whitespace=false] - Whether to replace comments * with whitespace instead of stripping them entirely. * * @returns {Array} - Safe methods return an array with the * first item being the `Error` instance caught. If successful, second * item will be the result: `[Error, string]` * * @example * import { safe as jsonc } from 'jsonc'; * const [err, str] = jsonc.stripComments('// comments\n{"key":"value"}'); * if (!err) console.log(str); // '\n{"key":"value"}' */ static stripComments(str: string, whitespace?: boolean): [Error, undefined] | [null, string]; /** * Safe version of `jsonc.uglify()`. Uglifies the given JSON string. * @name jsonc.safe.uglify * @function * * @param {string} str - JSON string to be uglified. * * @returns {Array} - Safe methods return an array with the * first item being the `Error` instance caught. If successful, second * item will be the result: `[Error, string]` * * @example * import { safe as jsonc } from 'jsonc'; * const pretty = ` * { * // comments... * "key": "value" * } * `; * const [err, ugly] = jsonc.uglify(pretty); * if (!err) console.log(ugly); // '{"key":"value"}' */ static uglify(str: string): [Error, undefined] | [null, string]; /** * Safe version of `jsonc.beautify()`. Beautifies the given JSON * string. Note that this will remove comments, if any. * @name jsonc.safe.beautify * @function * * @param {string} str - JSON string to be beautified. * @param {string|number} [space=2] Specifies the indentation of nested * structures. If it is omitted, the text will be packed without extra * whitespace. If it is a number, it will specify the number of spaces * to indent at each level. If it is a string (such as "\t" or * " "), it contains the characters used to indent at each level. * * @returns {Array} - Safe methods return an array with the * first item being the `Error` instance caught. If successful, second * item will be the result: `[Error, string]` * * @example * import { safe as jsonc } from 'jsonc'; * const ugly = '{"key":"value"}'; * const [err, pretty] = jsonc.beautify(ugly); * if (!err) console.log(pretty); * // { * // "key": "value" * // } */ static beautify(str: string, space?: string | number): [Error, undefined] | [null, string]; /** * Safe version of `jsonc.normalize()`. Normalizes the given value by * stringifying and parsing it back to a Javascript object. * @name jsonc.safe.normalize * @function * * @param {any} value * @param {Replacer} [replacer] - Determines how object values are * normalized for objects. It can be a function or an array of strings. * * @returns {Array} - Safe methods return an array with the * first item being the `Error` instance caught. If successful, second * item will be the result: `[Error, any]` * * @example * import { safe as jsonc } from 'jsonc'; * const c = new SomeClass(); * console.log(c.constructor.name); // "SomeClass" * const [err, normalized] = jsonc.normalize(c); * if (err) { * console.log('Failed to normalize: ' + err.message); * } else { * console.log(normalized.constructor.name); // "Object" * } */ static normalize(value: any, replacer?: Replacer): [Error, undefined] | [null, any]; /** * Safe version of `jsonc.read()`. Asynchronously reads a JSON file, * strips comments and UTF-8 BOM and parses the JSON content. * @name jsonc.safe.read * @function * * @param {string} filePath - Path to JSON file. * @param {Function|IReadOptions} [options] - Read options. * @param {Function} [options.reviver] - A function that can filter and * transform the results. It receives each of the keys and values, and * its return value is used instead of the original value. If it * returns what it received, then the structure is not modified. If it * returns undefined then the member is deleted. * @param {boolean} [options.stripComments=true] - Whether to strip * comments from the JSON string. Note that it will fail if this is * set to `false` and the string includes comments. * * @returns {Promise