JSON 整数:大小限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13502398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON integers: limit on size
提问by Ian Dickinson
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 整数有多大?我猜它们仅限于普通(32 位)整数,但我找不到写下来的任何地方。我需要对 Java 中的 long 标识符进行编码,因此我认为我需要将它们作为字符串存储在 JSON 中,以免冒溢出的风险。
回答by Tomalak
A JSON number is not limited by the spec.
JSON 编号不受规范限制。


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.
由于 JSON 是一种并非专门针对 JavaScript 的抽象格式,因此实际的目标环境决定了可以解释的内容的边界。
It's also worth noting that there are no "JSON Integers", they are a sub-set of the "Number" datatype.
还值得注意的是,没有“JSON 整数”,它们是“数字”数据类型的子集。
回答by Nolan
RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format
RFC 7159:JavaScript 对象表示法 (JSON) 数据交换格式
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.
该规范允许实现对接受的数字的范围和精度设置限制。由于实现 IEEE 754-2008 binary64(双精度)数字 [IEEE754] 的软件普遍可用并被广泛使用,因此可以通过不期望比这些提供的精度或范围更多的实现来实现良好的互操作性,因为实现将近似于 JSON预期精度内的数字。诸如 1E400 或 3.141592653589793238462643383279 之类的 JSON 数字可能表示潜在的互操作性问题,因为它表明创建它的软件希望接收软件具有比广泛可用的更大的数字幅度和精度功能。
回答by Tony Rad
I just did the following empirical test using Chrome (v.23 on Mac) Console:
我刚刚使用 Chrome(Mac 上的 v.23)控制台进行了以下实证测试:
> 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.
如果 JSON 通过 HTTP 传递,那么在任何情况下,数字都会从 Java 转换为字符串,然后问题可能只出现在 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.
4.3.19 数值
对应于双精度 64 位二进制格式 IEEE 754 值的原始值
注意 Number 值是 Number 类型的成员,是数字的直接表示。
Which is what defined in wikipedia Double-precision floating-point format.

