7

I want to save locally an object which has circular references. What are my options?

My first thought was using HTML5 local storage but I can't stringify this object due to the circular references.

Specifically I'm trying to save the DOMSelection object of the current selection.

Example:

  var sel = window.getSelection();
  var selstring = JSON.stringify(sel); // Breaks here ...
  localStorage.setItem("selection",selstring);

The only way I could get the stringify to work now is by ignoring certain objects like so:

var selstring = JSON.stringify(sel,function(k,v){
    if( k=="anchorNode" ||
        k=="baseNode" ||
        k=="extentNode" ||
        k=="focusNode") return undefined;

    return v;
});

But this leaves me with a rather empty DOMSelection object which isn't enough for what I need.

Is there any other way I can save this object? The only requirement is that it runs in mobile safari, anything else goes really. The solution can be either in javascript or jquery (or any other js lib if need be).

Thanks for any help you can provide.

nebs
  • 4,939
  • 9
  • 41
  • 70
  • See http://stackoverflow.com/questions/4001474/json-standard-way-of-referencing-an-object-by-identity-for-eg-circular-refere – Matt Ball Sep 14 '11 at 17:49

2 Answers2

3

The answer here lies in understanding what data you really need to store persistently and minimizing that to only what is needed and then adding a method or function to get just that information and then saving that. I don't know your application but for a text selection, you would probably just need a persistent indication of which object it was and the start and end points of the text selection.

Then, on the restore side, you would build a function to build a selection using the data you store. It's not as simple a serialize/deserialize, but it will work.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Yeah this is what I wanted to leave as the last resort as the DOMSelection object seemed fairly complex. In fact I realized that I don't need to store the DOMSelection after all and I found a workaround for my needs. – nebs Sep 14 '11 at 19:43
3

Check out Crockford's JSON-JS GitHub repo. It has a file, cycle.js, which can supposedly convert objects with circular references to JSON and back using JSONPath. See the last paragraph in the repo's read me and the file's comments.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • I will mark this as answered as it most directly answers my initial question. Unfortunately I can't test if it works with storing a DOMSelection object as it turns out I don't need it for this project, but this was a specific example anyways. – nebs Sep 14 '11 at 19:45
  • How can I put cycle.js in my code? I have a similar problem too. – corazza Sep 27 '11 at 16:06
  • By including the file in a script tag. Note however, that this is far from the best solution. You should instead ensure your objects have no circular references, and never JSON encode DOM nodes unless absolutely needed. – Alex Turpin Sep 27 '11 at 16:08