safe-stable-stringify is a JavaScript utility that provides a deterministic and safe alternative to JSON.stringify. Currently at version 2.5.0, it offers consistent object key ordering, graceful handling of circular references, and configurable serialization of BigInt values, addressing common pitfalls of the native `JSON.stringify`. The library maintains a regular release cadence, frequently adding new options and performance improvements. Key differentiators include its configurable deterministic sorting using custom comparators, options to control maximum serialization depth and breadth, and the ability to define how circular references or BigInts are handled (e.g., replacement values, throwing errors, or omission). It ships with TypeScript types, supports both ESM and CommonJS modules, and has zero external dependencies, making it a robust choice for environments requiring reliable JSON serialization.
npm install safe-stable-stringifyVerified import paths — ran on the pinned version, not inferred.
Demonstrates configuring `safe-stable-stringify` to handle circular references, BigInts, and ensure deterministic key order, then serializing an object with these features.
Review BigInt handling; if you need to ignore BigInts as in v1, set the `bigint` option to `false`. Ensure your environment supports ES6 and adapt imports for ESM if migrating from CJS.
If non-deterministic (insertion order) serialization is desired, use `configure({ deterministic: false })`.Avoid using boxed primitives if native `JSON.stringify` unboxing behavior is expected, or manually unbox them before serialization.
Ensure all values are JSON-compatible or explicitly configure `bigint` and `circularValue` options if they might be present. Handle `NaN`/`Infinity` or use custom replacers. Be aware that `strict` does not detect Sets, Maps, or Symbol keys as incompatible.
Upgrade to `safe-stable-stringify` v2.3.1 or newer to resolve this issue. Ensure your environment supports modern RegExp features if staying on older versions.
Use `safe-stable-stringify` with the default `bigint: true` option (or explicitly set it), or provide a custom `replacer` function to handle BigInts. For `strict: true`, explicitly set `bigint: true` or `bigint: false` to ignore/convert.
Configure `safe-stable-stringify` with `circularValue` set to a string (e.g., `'[Circular]'`), `null`, or `undefined` to handle circular references gracefully instead of throwing an error.
No dependency data recorded yet.