Skip to content

toStrictJsonValue

toStrictJsonValue(value, sortKeys?): StrictJsonValue

Defined in: stableStringify.ts:39

Normalizes a value to the strict, portable JSON domain used for deterministic hashing, idempotency fingerprints, and cache key generation.

Enforces strict JSON boundaries:

  • 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 optionally sorted recursively for deterministic serialization.
  • Unsupported types (non-finite numbers, Map, Set, Error, RegExp, binary buffers, promises, symbols, and sparse arrays) throw a TypeError.
  • Cyclic references are detected via a WeakSet and throw a TypeError.

unknown

The value to normalize.

boolean = false

Whether to sort object keys recursively in lexicographical order.

StrictJsonValue

The strictly normalized JSON-compatible value.

If the value contains cycles or elements outside the JSON domain.

const normalized = toStrictJsonValue({ b: 2, a: 1 }, true);
// → { a: 1, b: 2 }