title: JSON.stringify deep objects
body: <p>I need a function building a JSON valid string from any argument but :</p>
  
  <ul>
  <li>avoiding recursivity problem by not adding objects twice</li>
  <li>avoiding call stack size problem by truncating past a given depth</li>
  </ul>
  
  <p>Generally it should be able to process big objects, at the cost of truncating them.</p>
  
  <p>As reference, this code fails :</p>
  
  <pre><code>var json = JSON.stringify(window);
  </code></pre>
  
  <p>Avoiding recursivity problem is simple enough :</p>
  
  <pre><code>var seen = [];
  return JSON.stringify(o, function(_, value) {
      if (typeof value === 'object' &amp;&amp; value !== null) {
          if (seen.indexOf(value) !== -1) return;
          else seen.push(value);
      }
      return value;
  });
  </code></pre>
  
  <p>But for now, apart copying and changing <a href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js" rel="noreferrer">Douglas Crockford's code</a> to keep track of the depth, I didn't find any way to avoid stack overflow on very deep objects like <code>window</code> or any <code>event</code>. Is there a simple solution ?</p>