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.
Looks like this has been cast in stone: see RFC4627 and ECMA-262 (section 24.5.2, JSON.stringify, NOTE 4, page 683 of the ECMA-262 pdf at last edit):
Finite numbers are stringified as if by calling
ToString(number)
. NaN and Infinity regardless of sign are represented as the Stringnull
.
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 }};
into a forum (or whatever) and then any json usage on that site could be compromised.