The Precision Trap: BigInt and Number Serialization in JSON
In the world of standard JavaScript, numbers are stored as 64-bit floats (IEEE 754). This is fine for most applications, but for high-precision financial systems or database identifiers larger than 2^53 - 1, the "Number" type fails.
The Safe Integer Limit
The constant Number.MAX_SAFE_INTEGER (9007199254740991) is the ceiling. Beyond this, adding 1 to a number often results in the same number due to floating-point rounding errors.
The JSON Paradox
The JSON specification does not limit number size. However, JSON.parse() follows the language's native number implementation. If an API sends a 64-bit integer, and you use standard parsing, the browser will silently "round" your ID or your balance, leading to catastrophic bugs.
Modern Solutions
To handle these values securely:
- Stringification: Send large integers as strings in JSON.
- BigInt: Convert strings to the BigInt type for arithmetic.
- Custom Parsers: Use a loss-less JSON parser that detects large numbers and converts them to BigInt automatically.
Our tools are built to handle these edge cases, ensuring that the precision you paste is the precision you get.