<p>Ran into an “Out of Stack Space” error trying to serialize an ASP.Net AJAX Array object. </p>

<p>Here is the scenario with simplified code:</p>

<ol>
<li><p><code>Default.aspx</code></p></li>
<li><p><code>MainScript.js</code></p>

<pre><code>function getObject(){
    return new Array();
}

function function1(obj){
    var s=Sys.Serialization.JavaScriptSerializer.serialize(obj);
    alert(s);
}

function function2(){
    var obj=getObject();
    var s=Sys.Serialization.JavaScriptSerializer.serialize(obj);
    alert(s);
}
</code></pre></li>
<li><p><code>Content.aspx</code></p></li>
<li><p><code>ContentScript.js</code></p>

<pre><code>function serializeObject(){
    var obj=window.top.getObject();
    window.top.function1(obj); // &lt;– This works fine

    obj=new Array();
    window.top.function1(obj); // &lt;– this causes an Out of Stack Space error
}
</code></pre></li>
</ol>

<p>The code for the sample pages and JavaScript is <a href="http://braincells2pixels.wordpress.com/2008/02/14/aspnet-ajax-javascript-serialization-error/" rel="nofollow noreferrer">here</a>.</p>

<p>Posting the code for the aspx pages here posed a problem. So please check the above link to see the code for the aspx pages.</p>

<p>A web page (default.aspx) with an IFrame on that hosts a content page (content.aspx). </p>

<p>Clicking the “Serialize Object” button calls the JavaScript function serializeObject(). The serialization works fine for Array objects created in the top window (outside the frame). However if the array object is created in the IFrame, serialization bombs with an out of stack space error. I stepped through ASP.Net AJAX JS files and what I discovered is, the process goes into an endless loop trying to figure out the type of the array object. Endless call to Number.IsInstanceOf and pretty soon you get an out of stack error.</p>

<p>Any ideas?</p>

[{'answer_id': 57433, 'author': 'd91-jal', 'author_id': 5085, 'author_profile': 'https://Stackoverflow.com/users/5085', 'pm_score': 0, 'selected': False, 'text': "<p>I have no way of testing your code right now, but it looks like a bug in JavaScriptSerializer.serialize to me. My guess is that it tries to do some kind of type checking on the array via the CLR and that it doesn't handle an empty array properly. </p>\n\n<p>Have you tried to add an item of a serializable type to the array in your code? If so, what happens?</p>\n"}, {'answer_id': 194249, 'author': 'Kevin Hakanson', 'author_id': 22514, 'author_profile': 'https://Stackoverflow.com/users/22514', 'pm_score': 1, 'selected': False, 'text': '<p>I converted your example to a set of static html files, and dowloaded the standalone <a href="http://msdn.microsoft.com/en-us/asp.net/bb944808.aspx" rel="nofollow noreferrer">Microsoft Ajax Library 3.5</a> to test with.  It didn\'t have issue on either Firefox 3 or IE 7, but I did notice the first alert box displayed [] (an array) and the second {} (an object).</p>\n\n<p>Then, I converted your new Array() code to:</p>\n\n<pre><code>  var obj = [];\n  obj.push(1);\n</code></pre>\n\n<p>and after that, I got [1] and {"0", 1} is the alert boxes.  I don\'t think the bug is with JavaScriptSerializer, but something to do with passing objects across frames.</p>\n'}, {'answer_id': 2561528, 'author': 'GotDotCom', 'author_id': 307026, 'author_profile': 'https://Stackoverflow.com/users/307026', 'pm_score': 3, 'selected': True, 'text': '<p>This problem happens because Sys.Serialization.JavaScriptSerializer can\'t serialize objects from others frames, but only those objects which where instantiated in the current window (which calls serialize() method). The only workaround which is known for me it\'s making clone of the object from other frame before calling serialize() method.</p>\n\n<p>Example of the clone() methode you can find here (comments in Russian):\n<a href="http://snowcore.net/clone-javascript-object" rel="nofollow noreferrer">link text</a></p>\n'}]
['https://Stackoverflow.com/questions/56905', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3635/']