问题描述
知道为什么JSON遗漏了NaN和+/- Infinity?如果它们包含NaN或+/-无穷大值,它会将Javascript放在奇怪的情况下,否则可以序列化的对象不会。
Any idea why JSON left out NaN and +/- Infinity? It puts Javascript in the strange situation where objects that would otherwise be serializable, are not, if they contain NaN or +/- infinity values.
看起来这个已被强制转换石头:见和(第24.3.2节,JSON.stringify,第4页,第507页,最后编辑):
Looks like this has been cast in stone: see RFC4627 and ECMA-262 (section 24.3.2, JSON.stringify, NOTE 4, page 507 at last edit):
推荐答案
Infinity
和 NaN
不是关键字或任何特殊的东西,它们只是全局对象的属性(因为 undefined
),因此可以更改。出于这个原因,JSON不在规范中包含它们 - 本质上,如果你执行 eval(jsonString)
或者 JSON.parse(jsonString)。
Infinity
and NaN
aren't keywords or anything special, they are just properties on the global object (as is undefined
) and as such can be changed. It's for that reason JSON doesn't include them in the spec -- in essence any true JSON string should have the same result in EcmaScript if you do eval(jsonString)
or JSON.parse(jsonString)
.
如果允许那么有人可以注入类似于
If it were allowed then someone could inject code akin to
NaN={valueOf:function(){ do evil }};
Infinity={valueOf:function(){ do evil }};
进入论坛(或其他),然后该网站上的任何json使用都可能受到损害。
into a forum (or whatever) and then any json usage on that site could be compromised.
这篇关于JSON遗漏了Infinity和NaN; ECMAScript中的JSON状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!