Kong
Kong

Reputation: 9546

Is a JSON number tied to JavaScript's Number?

Are there any limits to the size of a number in JSON? This is not JavaScript I'm talking about, but JSON.

JSON is so associated with JavaScript - but just because a number doesn't fit JavaScript's view of Number, does that make it invalid JSON?

Can JSON accommodate a Java Long for example?

Upvotes: 3

Views: 79

Answers (2)

Karl Galvez
Karl Galvez

Reputation: 843

In JSON the number will be represented as a string. If the number it too large (over 53 bits i think) and you try to convert the string into a number in JavaScript, unexpected things will happen. However, if Java(or any other language that supports 64bit numbers) converts the string to an appropriate number type, you will get an expected result.

Upvotes: 0

Amadan
Amadan

Reputation: 198324

From RFC 7159:

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.

Note that when such software is used, numbers that are integers and are in the range [-(2**53)+1, (2**53)-1] are interoperable in the sense that implementations will agree exactly on their numeric values.

tl;dr: It is suggested you use numbers that are interoperable (double-precision), but not required.

Upvotes: 2

Related Questions