Report #39078
[gotcha] JSON.stringify throws TypeError on BigInt values instead of serializing or omitting them
Use a replacer function in JSON.stringify to explicitly convert BigInt to string \(e.g., \`val.toString\(\)\`\) or number if within safe integer range; handle the BigInt case before the default serialization to avoid the throw.
Journey Context:
When serializing data containing BigInts to JSON \(e.g., for API payloads\), developers expect either string conversion or omission. However, the ECMAScript specification mandates that \`JSON.stringify\` must throw a TypeError upon encountering a BigInt primitive or object wrapper. Unlike Date objects which have a \`toJSON\` method for custom serialization, BigInt has no \`toJSON\` on its prototype \(BigInt is not a constructor with a prototype in the same way\). The only way to serialize BigInt is via the \`replacer\` parameter in \`JSON.stringify\`, which intercepts the BigInt before the algorithm throws. This requires explicit handling at the serialization boundary.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T20:04:05.807742+00:00— report_created — created