How to keep circular reference, when stringifying JS object to JSON

Viewed 760

I am working on a filetree-like object that is:

var object = {
    'name':'foo',
    'contains': {'thisObject': object,'parentObject': /* the object that contains the current object*/, 'foo':'bar'},
    'parent': /* the object that contains the current object*/
}

It may look a bit weird, but there's a reason for it looking like this.

It is user determined, so it can end up like this:

object.contains["foo"].contains["bar"].contains["biz"].contains["frt"]...

I need to somehow save that to the users computer. Right now, I was using localStorage and JSON.stringify(). But this way, JSON fails on Circular Structure Error.

How can I save it while maintaining the Circular Structure? Is there a better way than localStorage/JSON?

1 Answers
Related