Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

IE8 JSON.stringify gives 'null' instead of '""' for document.createElement("input").value #44

Closed
Krinkle opened this issue May 23, 2014 · 2 comments

Comments

@Krinkle
Copy link
Owner

Krinkle commented May 23, 2014

From l.giraudel on November 23, 2010 16:37:46

There is a bug in the JSON.stringify() method of some versions of IE8 : cf http://blogs.msdn.com/b/jscript/archive/2009/06/23/serializing-the-value-of-empty-dom-elements-using-native-json-in-ie8.aspx The following assertion is supposed to be true :
JSON.stringify(document.createElement("input").value)==='""'

But in some IE8 versions this assertion is false because JSON.stringify(document.createElement("input").value) returns '"null"'.

I found this bug in in IE8 on Windows Server 2008 but I don't have the bug on Windows 7.

I edited my jquery.json.js file :

(function($){
    $.toJSON = function(o) {
        if (typeof(JSON) == 'object' && JSON.stringify) {
            if ($.browser.msie && $.browser.version == "8.0" && JSON.stringify(document.createElement("input").value)==='"null"') {
                return JSON.stringify(o, function(k, v) { return v === "" ? "" : v; });
            } else {
                return JSON.stringify(o);
            }
        }

        var type = typeof(o);
        [...]

Original issue: http://code.google.com/p/jquery-json/issues/detail?id=44

@Krinkle
Copy link
Owner Author

Krinkle commented May 23, 2014

From bhavin.ibm on August 24, 2011 16:48:34

I am using IE and it gives me an error at JSON.parse(). Please help me out. Its hard to find any solution. I have deadline this week.

Any help appreciated.... email: bhavin.ibm@gmail.com
Thank you in advance
Bhavin Patel

@Krinkle
Copy link
Owner Author

Krinkle commented May 23, 2014

From krinklemail on October 04, 2011 16:03:04

Hi,

I've read your bug report but it seems to be that this is not a bug in jquery.json, nether is it a bug in JSON.stringify of IE8.

The bug is that the 'value' property of new elements really is set to null in IE8 for new elements. It is not JSON.stringify that wrongly converts "" to "null", it is document.createElement behaving differently in IE8.

If you use alert or console.log it will give the same result, it is not related to JSON.stringify or jquery.json.

I suggest you use a wrapper for interacting with elements such as jQuery.

JSON.stringify( '' ); gives the correct result in IE8: '""'

JSON.stringiy( null ); gives the correct result in IE8: 'null'

In IE8 document.createElement("input") is an Element object with a property key 'value' of value null.

Marking this report as invalid.

Status: Invalid

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant