如何使用循环引用保存对象?

时间:2022-08-23 00:23:28

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.

我的第一个想法是使用HTML5本地存储,但由于循环引用,我无法对此对象进行字符串化。

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

具体来说,我正在尝试保存当前选择的DOMSelection对象。

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:

我现在可以让stringify工作的唯一方法是忽略某些对象,如下所示:

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.

但这给我留下了一个相当空的DOMSelection对象,这对我所需要的还不够。

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

有没有其他方法可以保存这个对象?唯一的要求是它在移动游猎中运行,其他任何东西都是真的。解决方案可以是javascript或jquery(或任何其他js lib,如果需要)。

Thanks for any help you can provide.

感谢您的任何帮助,您可以提供。

2 个解决方案

#1


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.

看看Crockford的JSON-JS GitHub回购。它有一个文件cycle.js,它可以推测使用JSON循环引用转换对象并使用JSONPath返回。请参阅回购中的最后一段读我和文件的注释。

#2


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.

然后,在还原端,您将构建一个函数,以使用您存储的数据构建选择。它不像序列化/反序列化那么简单,但它会起作用。

#1


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.

看看Crockford的JSON-JS GitHub回购。它有一个文件cycle.js,它可以推测使用JSON循环引用转换对象并使用JSONPath返回。请参阅回购中的最后一段读我和文件的注释。

#2


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.

然后,在还原端,您将构建一个函数,以使用您存储的数据构建选择。它不像序列化/反序列化那么简单,但它会起作用。