How JSON.parse silently corrupts large integers and how to keep their digits
JSON.parse turns integers beyond Number.MAX_SAFE_INTEGER into approximate values, so 12345678901234567890 becomes 12345678901234567000 and 9007199254740993 collapses to 9007199254740992. The loss happens silently, with no error or warning, and re-serializing the result writes the altered digits back out. A reviver's context.source argument can recover the original text, letting large integers be converted to BigInt, though BigInt cannot be serialized by JSON.stringify without extra handling. JSON.rawJSON offers a way to emit exact number literals. The safest fix is to have the API send large ids as strings, with the author's tests run on Node 22 and no browser checks performed.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in