Preventing 64-Bit Integer Precision Loss in Browser JSON Parsers
JavaScript numbers follow IEEE 754 double-precision floating-point format, offering 53 bits of mantissa precision. Integers exceeding Number.MAX_SAFE_INTEGER (9,007,199,254,740,991) lose precision silently when parsed with standard JSON.parse().
1. The IEEE 754 Safe Integer Boundary
Any 64-bit integer—such as Twitter IDs, Discord Snowflake IDs, or PostgreSQL BIGINT primary keys (which range up to 9,223,372,036,854,775,807)—will be truncated to the nearest representable float value.
For example, parsing { "id": 9007199254740993 } results in 9007199254740992, silently mutating your primary key!
// Standard JSON.parse silent corruption:
JSON.parse('{"id": 9007199254740993}').id === 9007199254740992 // true!
// Safe BigInt handling preserves identity:
parseLosslessJson('{"id": 9007199254740993}').id // 9007199254740993n2. Lossless Lexing Strategy
To preserve exact values without backend assistance, our client-side formatter identifies numeric tokens before invoking standard serialization.
Numbers exceeding 15 decimal digits without exponent notation are safely converted to BigInt instances or quoted strings before formatting.
Summary & Best Practices
Always check numeric literals in incoming JSON against 2^53 - 1 to protect mission-critical IDs from floating-point rounding errors.
