Loading ...
Sorry, an error occurred while loading the content.

1271Re: IE8 Native JSON Bug

Expand Messages
  • Stephen M. McKamey
    Jun 2, 2009
    • 0 Attachment
      Below is the answer from Microsoft about this bug in JSON.stringify. Thanks Douglas Crockford for forwarding on to the appropriate contact at Microsoft.

      It appears the short answer is that you have to test for empty strings coming off DOM elements and replace with a real empty string. That can be done via the replacer function or at the time of retrieving the value.

      ---------- Forwarded message ----------
      From: Allen Wirfs-Brock
      Date: Tue, Jun 2, 2009 at 08:15
      Subject: RE: [Fwd: [json] IE8 Native JSON Bug]


      This is a bug in the initial production version of IE8 that we were already aware of. The problem is that within the DOM a special encoding is used to represent a missing string value. Even though this special value is different from the encoding of the JavaScript literal "", throughout the JScript implementation the value is treated as being === to "" ... except for one oversight in JSON.stringify.

      Since this special value only originates from accesses to DOM objects one workaround is to explicitly censor them on every DOM access that might return one. For example,
      var good, possiblyBad = good =document.createElement("input").value;
      if (possiblyBad === "") good = ""; //ensure possibly bogus "" is replaced with a real ""
      In particular, this should be done when accessing the value of an input element if that value is going to be assigned to a structure that will be passed through stringify.

      Since the difference is only observable via stringify, another alternative is to use the replacer function to perform the substitution:
      JSON.stringify(document.createElement("input").value,
      function(k,v) { return v==="" ? "" : v});
      //the above will return "", not "null"

      A post describing this problem and the workaround should appear on the JScript blog (http://blogs.msdn.com/jscript/ ) sometime this week.

      Allen
    • Show all 7 messages in this topic