JSON Numbers Across Different Runtimes

JSON specifies a number syntax, while readers may store the parsed value in different numeric representations.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace an exact identifier
  2. Specify interoperability limits
  3. Test integer agreement between unlike consumers
  4. State which range and representation consumers support

Trace an exact identifier

The integer \(2^{53}+1\) can be held exactly by an arbitrary-precision reader. It is not an exact value in binary64. An interface that compares identifiers across runtimes can preserve its digits as text. See numeric conversion.

Specify interoperability limits

If the field really is numeric, document expected range and precision and test around those limits. A fixed-width comparison shows why the sender's ability to represent a number cannot alone guarantee what the receiver sees.[1]

Test integer agreement between unlike consumers

JSON syntax permits decimal integer text 9007199254740993. A binary64 consumer can round this to 9007199254740992. An arbitrary-precision integer consumer may preserve the original value. Such a mismatch matters when numbers identify records. See bounded and unbounded integers.

Source text: 9007199254740993
Exact integer consumer: 9007199254740993
Typical binary64 parse: 9007199254740992
An agreed string ID can preserve the digits

State which range and representation consumers support

Use a schema restricting integer range or representing large IDs as text. A round-trip test needs the actual client and server parsers. Include exponent notation and exact endpoints to reveal different conversions.

References

  1. IETF, RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format ↩