Skip to main content

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



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 at the top of p. 197:







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





Source: Tips4all

Comments

  1. At all "security reason" answerer:
    Security isn't a valid point!
    If someone has access to your global code to to something like

    NaN={valueOf:function(){ do evil }};


    then he could also do things like

    JSON.parse = function(){ do evil };

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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.isNaN) {/* do that*/}
    else if (myObj.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.

    ReplyDelete
  4. Could it be because JSON is intended to be a data interchange format that can be used in a variety of platforms and allowing NaN/Infinity would make it less portable?

    ReplyDelete
  5. 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).

    ReplyDelete
  6. If you have access to the serialization code you might represent Infinity as 1.0e+1024. The exponent is too large to represent in a double and when deserialized this is represented as Infinity. Works on webkit, unsure about other json parsers!

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.