Report #56319
[gotcha] JSON.stringify throws on BigInt before replacer function can convert it
Pre-convert BigInt values to strings or numbers before calling JSON.stringify; do not rely on the replacer function to handle BigInt. Use a recursive wrapper that checks typeof value === 'bigint' before stringification, or use libraries like json-bigint for safe handling.
Journey Context:
Developers expect the replacer function in JSON.stringify to act as a pre-processing hook for all values. However, the spec checks for BigInt and throws TypeError at step 3 of SerializeJSONProperty, before step 5 where the replacer is invoked. This means you cannot use a replacer to safely serialize BigInt by converting to string. Common workarounds like adding toJSON to BigInt prototype fail because BigInt is a primitive, not an object, and primitives don't lookup methods on their prototypes during JSON serialization. The only solution is pre-processing the data structure before JSON.stringify sees it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T01:01:27.936236+00:00— report_created — created