JSON integers: limit on size

Json

Json Problem Overview


Is it specified anywhere how big JSON integers can be? I'm guessing that they're limited to normal (32 bit) ints, but I can't find anywhere that that's written down. I need to encode identifiers that are longs in Java, so I presume I need to store those as strings in JSON so as not to risk overflow.

Json Solutions


Solution 1 - Json

A JSON number is not limited by the spec.

JSON number grammar

Since JSON is an abstract format that is not exclusively targeted at JavaScript, the actual target environment determines the boundaries of what can be interpreted.

It's also worth noting that there are no "JSON Integers", they are a sub-set of the "Number" datatype.

Solution 2 - Json

RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format > This specification allows implementations to set limits on the range and precision of numbers accepted. Since software that implements IEEE 754-2008 binary64 (double precision) numbers [IEEE754] is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision. A JSON number such as 1E400 or 3.141592653589793238462643383279 may indicate potential interoperability problems, since it suggests that the software that created it expects receiving software to have greater capabilities for numeric magnitude and precision than is widely available.

Solution 3 - Json

I just did the following empirical test using Chrome (v.23 on Mac) Console:

> var j = JSON.parse("[999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999]")
undefined

> j[0]
1e+228

If JSON is passed through HTTP then the number will be converted in String from Java in any case and then the issue could be only in Javascript.

From ECMAScript Language Specification 4.3.19:

> 4.3.19 Number value > > primitive value corresponding to a double-precision 64-bit binary > format IEEE 754 value > > NOTE A Number value is a member of the Number type and is a direct > representation of a number.

Which is what defined in wikipedia Double-precision floating-point format.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIan DickinsonView Question on Stackoverflow
Solution 1 - JsonTomalakView Answer on Stackoverflow
Solution 2 - JsonNolanView Answer on Stackoverflow
Solution 3 - JsonTony RadView Answer on Stackoverflow