fast-safe-stringify is a utility library designed for safely and quickly serializing JavaScript objects into JSON strings. It provides a robust alternative to the native `JSON.stringify`, specifically excelling at gracefully handling circular references within objects without throwing a `TypeError`. Instead, it intelligently replaces circular structures with a `[Circular]` string or `[...]` when configured limits are reached. The current stable version is 2.1.1, with recent updates (v2.1.0 and v2.1.1) introducing and refining `depthLimit` and `edgesLimit` options to prevent excessive recursion in complex objects. The library also offers a `stableStringify` function, which guarantees a deterministic output order for object keys, enhancing reproducibility. It is actively maintained and ships with TypeScript type definitions, making it suitable for modern JavaScript and TypeScript environments where predictable serialization of potentially self-referential data structures is crucial, such as in logging or API responses.
npm install fast-safe-stringifyVerified import paths — ran on the pinned version, not inferred.
Demonstrates `safeStringify` and `stableStringify` handling circular references and optional depth limits.
Review and potentially adjust `depthLimit` or `edgesLimit` in the options object passed to `safeStringify` or `stableStringify` if previous behavior is desired for extremely complex objects.
Design your `replacer` functions to handle the `"[Circular]"` string explicitly if you need to modify or omit circular references, rather than expecting the object itself.
Avoid side-effecting `toJSON` or `replacer` logic for circular references or for any part of the input object when using `stableStringify`. If such manipulation is critical, consider `safe-stable-stringify` (though noted as experimental) or manual pre-processing.
Replace `JSON.stringify` with `safeStringify` from `fast-safe-stringify`. Example: `const serialized = safeStringify(myObject);`
Check the `value` argument within your `replacer` for the literal string `"[Circular]"` to handle circular references. For example: `if (value === '[Circular]') return;`
No dependency data recorded yet.