6

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)?

Community
  • 1
  • 1
d4rty
  • 3,970
  • 5
  • 34
  • 73
  • you have the "deep clone" requirement, as in `original.foo.bar.baz===clone.foo.bar.baz`, if `...baz` is a primitive value (such as string or number); that kind of equality for paths (like `['foo','bar','baz']`) leading to a primitive value. It is possible to generate a deep clone. Then, you have "inner object equality requirement", which deals with circular structures. You want that if `original.foo.bar===original.lorem.ipsum.dolor` if and only if `cpy.foo.bar===cpy.lorem.ipsum.dolor`. It is possible to generate a perfect clone cpy with inner object equalities intact. – mathheadinclouds Jan 02 '20 at 08:06
  • This is what I think you mean with "the same object" - an object with "deep clone requirement" and "inner object equality requirement" both fulfilled. Because, having `obj1===obj2`, where `obj1` is the "original", and `obj2` is "reconstructed" from a "string version" of the original, that "===", that is not possible. "Exactly the same objects" sound a little as if you might have made this error (probably not, but maybe some inexperienced newby reader might think it), so to be on the safe side, mentioning it. – mathheadinclouds Jan 02 '20 at 08:11
  • it is not entirely trivial to even write the proper test function, returning a boolean indicating if one object is "the same" as another object (in the "deep clone" + "inner object equalities" sense, not the "===" sense). Mabye you made an error writing your version of that test function. That is really non-trivial, you don't need to be a newby to get that wrong! Why not write a new question in the hunt for a correct version of that function? That would be a place to start. – mathheadinclouds Jan 02 '20 at 08:20
  • "equal in the strongest sense of the word equal which can reasonably be applied to an object on one computer and another ('same') object on a different computer on another planet." That is the "equal" I'm talking about. "equal" as "stored in the same memory address", that is the "equal" which is simply not possible. – mathheadinclouds Jan 02 '20 at 08:25
  • two objects on 2 different computers being "equal" refers to equality of the mathematical idea behind the object. two objects (necessarily on the same computer) being "pointer equal" refers to something which has really nothing to do with the "math / idea / thought" behind the object. The container storing the information is supposed to be the same container in both cases. And that is "===" applied to objects in JS. You just said "the same". Both above concepts want to be called "the same". But you can only mean one of them, if you know what you are talking about. – mathheadinclouds Jan 02 '20 at 08:39
  • Does anyone has answer of this question ? – LOKESH Mar 23 '20 at 06:23

0 Answers0