Store a JS object with circular structures on loca

2020-06-23 05:41发布

问题:

I want to store (Local Storage HTML5) JS objects. To do this I have to apply JSON.stringify(obj) to the JS object I want to store. After this I am able to store the object localStorage.obj=JSON.stringify(obj);

But some of the JS objects are very large and contain circular structures. To handle circular structures I found the following two approaches:

  1. Using a replacer, see stackoverflow. But this method only removes the circular structures.
  2. Use a custom function, which tries first to remove circular structures and then to reconstruct circular structures, see stackoverflow. I tried this method, but the object I got from the local Storage wasn't the same as the one I stored before.

Because both approches doesn't fulfill my requirements I ask this frequently asked question again. Does anybody know a method to save JS objects containing circular structures and load these objects, so that they are exactly the same objects as before when they were stored (with all the circular structures)?