IE8本机JSON.parse错误导致堆栈溢出 [英] IE8 native JSON.parse bug causes stack overflow

查看:182
本文介绍了IE8本机JSON.parse错误导致堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR:将任何非内置函数添加到Array.prototype和Function.prototype将导致IE8本机JSON解析器在解析包含数组的任何JSON时获得堆栈溢出,但仅限于你也将一个reviver函数传递给JSON.parse()。

这开始是一个问题,但我回答了我自己的原始问题,所以现在我请问:有没有人能想到解决这个IE8错误的问题,这个问题不涉及删除修改Array.prototype和Function.prototype的所有JS库?

This started out as a question, but I answered my own original question, so now I'll ask: can anyone think of a work-around for this IE8 bug that doesn't involve eliminating all JS libraries that modify Array.prototype and Function.prototype?

原始问题:

我有大约13k的JSON数据需要解析。数据的结构是一个具有单个值的对象,它是一个嵌套数组。

I have about 13k of JSON data to parse. The structure of the data is an object with a single value that is a nested array.

{ 'value':[[ stuff ], [ more stuff], [ etc ]] }

我正在使用json2.js,在可用时遵循浏览器本机JSON.parse。我正在将一个reviver函数传递给JSON.parse来正确处理日期。当IE8处于IE7仿真模式(导致它使用基于脚本的json2.js解析器)时,一切正常。当IE8处于IE8模式(导致它使用浏览器本机JSON解析器)时,它会因堆栈空间不足错误而崩溃。当然,Firefox和Chrome可以很好地使用他们的浏览器本地JSON解析器。

I'm using json2.js, which defers to the browser native JSON.parse when available. I'm passing a reviver function into JSON.parse to handle dates properly. When IE8 is in IE7 emulation mode (which causes it to use the script-based json2.js parser) everything works fine. When IE8 is in IE8 mode (which causes it to use the browser-native JSON parser) it blows up with an "out of stack space" error. Firefox and Chrome, of course, work just fine with their browser-native JSON parsers.

我把它缩小到这个范围:如果我甚至不做任何事情将函数恢复为JSON.parse,IE8本机解析器获取堆栈溢出。如果我传入没有reviver函数,IE8本机解析器工作正常,除了它不能正确解析日期。

I've narrowed it down to this: if I pass even a do-nothing reviver function into JSON.parse, the IE8 native parser gets the stack overflow. If I pass in no reviver function, the IE8 native parser works fine, except it doesn't parse dates correctly.

// no error:
JSON.parse(stuff);

// "out of stack space" error:
JSON.parse(stuff, function(key, val) { return val; });

我将使用我的JSON数据,看看是否有更少的数据或更少的嵌套数据可以避免错误,但我想知道是否有人之前看过这个,或者有任何其他建议的解决方法。 IE8已经足够慢,因为这个错误而禁用该浏览器的原生JSON将是一种耻辱。

I'm going to play with my JSON data, to see if less data or less nesting of the data can avoid the error, but I was wondering if anyone had seen this before, or had any other suggested work-arounds. IE8 is slow enough already, it would be a shame to disable native JSON for that browser because of this bug.

更新:在其他情况下,使用不同的JSON数据,我当我使用具有reviver功能的IE8本机解析器时,得到javascript错误$ lineinfo未定义,如果我不使用reviver函数则没有错误。字符串$ lineinfo没有出现在我的任何源代码中。

UPDATE: In other cases, with different JSON data, I'm getting a javascript error "$lineinfo is undefined" when I use the IE8 native parser with a reviver function, and no error if I use no reviver function. The string "$lineinfo" does not appear anywhere in any of my source code.

更新2:实际上,这个问题似乎是由Prototype 1.6.0.3引起的。在我添加到Prototype库之前,我无法在隔离的测试页面中重现它。

UPDATE 2: Actually, this problem seems to be caused by Prototype 1.6.0.3. I was unable to reproduce it in an isolated test page until I added in the Prototype library.

更新3:

prototype.js打破IE8原生JSON解析器的原因是:将任何非内置函数添加到Array.prototype和Function.prototype将导致IE8本机JSON解析器在解析包含的任何JSON时获得堆栈溢出一个数组,但只有当你还将一个reviver函数传递给JSON.parse()时。

The reason prototype.js breaks the IE8 native JSON parser is this: Adding any non-built-in functions to Array.prototype AND Function.prototype will cause the IE8 native JSON parser to get a stack overflow when parsing any JSON that contains an array, but only when you also pass a reviver function into JSON.parse().

Prototype库为Array.prototype和Function.prototype添加了函数,但是这同样适用于执行相同操作的任何其他库。 IE JSON解析器中的这个错误由​​Prototype和Ext公开,但不是jQuery。我还没有测试任何其他框架。

The Prototype library adds functions to both Array.prototype and Function.prototype, but this applies equally to any other library that does the same thing. This bug in the IE JSON parser is exposed by Prototype and Ext, but not jQuery. I haven't tested any other frameworks.

这是一个完全独立的问题再现。如果删除Function.prototype行或Array.prototype行,或从JSON字符串中删除该数组,则不会出现堆栈空间不足错误。

Here is a completely stand-alone reproduction of the problem. If you remove the Function.prototype line, or the Array.prototype line, or remove the array from the JSON string, you won't get the "out of stack space" error.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<script type="text/javascript">

Function.prototype.test1 = function() { };
Array.prototype.test2 = function() { };

window.onload = function()
{
    alert(JSON.parse('{ "foo": [1,2,3] }', function(k,v) { return v; }));
}

</script>
</head>
<body>

</body>
</html>


推荐答案

这只是补丁。
http://support.microsoft.com/kb/976662

http:/ /msdn.microsoft.com/en-us/library/cc836466(VS.85).aspx

这篇关于IE8本机JSON.parse错误导致堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆