JSON遗漏了Infinity和NaN; ECMAScript中的JSON状态? [英] JSON left out Infinity and NaN; JSON status in ECMAScript?

查看:208
本文介绍了JSON遗漏了Infinity和NaN; ECMAScript中的JSON状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

知道为什么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.

看起来这个已被强制转换石头:见 RFC4627 ECMA-262 (第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):


有限数字通过调用 ToString(数字)进行字符串化。 NaN 和无限无论符号都表示为字符串 null

Finite numbers are stringified as if by calling ToString(number). NaN and Infinity regardless of sign are represented as the String null.


推荐答案

Infinity NaN 不是关键字或任何特殊的东西,它们只是全局对象的属性(因为 undefined ),因此可以更改。出于这个原因,JSON不在规范中包含它们 - 本质上,如果你执行 eval(jsonString)或者<,那么任何真正的JSON字符串都应该在EcmaScript中具有相同的结果code> 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状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆