Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON left out Infinity and NaN; JSON status in ECMAScript?

People also ask

How are numeric values like infinity and NaN handled in JSON?

How does JSON handle numeric values that cannot be represented by a sequence of digits (like Infiniti and Nan)? They are not permitted. They are stored as strings and then converted when parsed. They are stored fine but it's the parsers job to convert them to numeric values.

Is JSON compatible with ECMAScript?

Embedding JSON into JavaScript programsstringify can now be used to generate valid ECMAScript string literals, object literals, and array literals. And because of the separate well-formed JSON.

What are NaN Infinity and Infinity serialized to in JSON?

NaN , Infinity , and -Infinity are serialized to null . Date objects are serialized to ISO-formatted date strings (see the Date.

Does JSON support infinity?

If you are not sure how deep your content structure is, you can use infinity. json to get data at all level. It looks like current implementation returns all level response instead of actual data (Which could be huge). If you are not sure how deep your content structure is, you can use infinity.


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.


On the original question: I agree with user "cbare" in that this is an unfortunate omission in JSON. IEEE754 defines these as three special values of a floating point number. So JSON cannot fully represent IEEE754 floating point numbers. It is in fact even worse, since JSON as defined in ECMA262 5.1 does not even define whether its numbers are based on IEEE754. Since the design flow described for the stringify() function in ECMA262 does mention the three special IEEE values, one can suspect that the intention was in fact to support IEEE754 floating point numbers.

As one other data point, unrelated to the question: XML datatypes xs:float and xs:double do state that they are based on IEEE754 floating point numbers, and they do support the representation of these three special values (See W3C XSD 1.0 Part 2, Datatypes).


Could you adapt the null object pattern, and in your JSON represent such values as

"myNum" : {
   "isNaN" :false,
   "isInfinity" :true
}

Then when checking, you can check for the type

if (typeof(myObj.myNum) == 'number') {/* do this */}
else if (myObj.myNum.isNaN) {/* do that*/}
else if (myObj.myNum.isInfinity) {/* Do another thing */}

I know in Java you can override serialization methods in order to implement such a thing. Not sure where your serializing from, so I can't give details on how to implement it in the serialization methods.


The strings "Infinity", "-Infinity", and "NaN" all coerce to the expected values in JS. So I'd argue the right way to represent these values in JSON is as strings.

> +"Infinity"
Infinity

> +"-Infinity"
-Infinity

> +"NaN"
NaN

It's just a shame JSON.stringify doesn't do this by default. But there is a way:

> JSON.stringify({ x: Infinity }, function (k,v) { return v === Infinity ? "Infinity" : v; })
"{"x":"Infinity"}"