Skip to content

Instantly share code, notes, and snippets.

@zmmbreeze
Created March 7, 2014 09:13
Show Gist options
  • Select an option

  • Save zmmbreeze/9408172 to your computer and use it in GitHub Desktop.

Select an option

Save zmmbreeze/9408172 to your computer and use it in GitHub Desktop.

Revisions

  1. zmmbreeze created this gist Mar 7, 2014.
    17 changes: 17 additions & 0 deletions stringify.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    // http://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json/11616993#11616993
    var o = {};
    o.o = o;

    var cache = [];
    JSON.stringify(o, function(key, value) {
    if (typeof value === 'object' && value !== null) {
    if (cache.indexOf(value) !== -1) {
    // Circular reference found, discard key
    return;
    }
    // Store value in our collection
    cache.push(value);
    }
    return value;
    });
    cache = null; // Enable garbage collection