Report #11848
[gotcha] JSON.stringify throws TypeError when encountering BigInt values, breaking serialization
Implement a replacer function: \`JSON.stringify\(obj, \(k, v\) => typeof v === 'bigint' ? v.toString\(\) : v\)\`. For revival, use a reviver: \`JSON.parse\(str, \(k, v\) => typeof v === 'string' && /^-?\\d\+n$/.test\(v\) ? BigInt\(v.slice\(0, -1\)\) : v\)\` \(or similar convention\). Never pass BigInt directly to APIs expecting JSON.
Journey Context:
BigInt is necessary for 64-bit integers \(IDs, crypto\), but JSON spec has no BigInt type. TC39 explicitly mandated that JSON.stringify throw rather than silently lose precision \(unlike Number\). This causes crashes in logging \(console.log with JSON\), localStorage setItem, and fetch POST bodies when BigInt is present. Common wrong fix: casting to Number \(loses precision >2^53\). Correct fix requires custom replacer/reviver pair or libraries like 'json-bigint' that parse numbers as BigInt.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:24:19.646624+00:00— report_created — created