safe-stringify
Version:
Serialize objects to JSON with handling for circular references
30 lines (22 loc) • 632 B
TypeScript
export type Options = {
/**
The indentation of the JSON.
By default, the JSON is not indented.
Set it to `'\t'` for tab indentation or the number of spaces you want.
*/
readonly indentation?: string | number;
};
/**
Serialize objects to JSON with handling for circular references.
@example
```
import safeStringify from 'safe-stringify';
const foo = {a: true};
foo.b = foo;
console.log(safeStringify(foo));
//=> '{"a":true,"b":"[Circular]"}'
console.log(JSON.stringify(foo));
//=> TypeError: Converting circular structure to JSON
```
*/
export default function safeStringify(value: unknown, options?: Options): string;