Skip to content

stableStringify

stableStringify(value): string

Defined in: stableStringify.ts:174

Deterministically serializes a value to a JSON string with lexicographically sorted object keys, for cache keys, idempotency fingerprints and snapshots.

Guarantees that structurally equal payloads produce byte-identical JSON strings regardless of property insertion order. The output forms stored keys, so it must never change: golden-output.spec.ts pins it.

Enforces strict JSON boundaries, as toStrictJsonValue does:

  • Primitives (string, boolean, null, finite number) are preserved.
  • Dates are explicitly converted to ISO-8601 strings (.toISOString()).
  • Custom objects with .toJSON() methods are validated through their returned representation; internal fields excluded by that method are not inspected.
  • Object keys are sorted recursively.
  • Unsupported types (undefined, bigint, symbols, functions, non-finite numbers, Map, Set, Error, RegExp, binary buffers, promises, symbol-keyed properties and sparse arrays) throw a TypeError.
  • Cyclic references are detected via a WeakSet and throw a TypeError.

unknown

The value to serialize.

string

The deterministic JSON string representation.

If the value is cyclic, contains non-serializable types, or cannot be represented in JSON. The precise reason is the error’s cause.

stableStringify({ z: 1, a: 2 });
// → '{"a":2,"z":1}'