通过标识引用对象的标准方法(如循环引用)?

时间:2022-08-23 00:27:57

Is there a standard way of referencing objects by identity in JSON? For example, so that graphs and other data structures with lots of (possibly circular) references can be sanely serialized/loaded?

是否有一种用JSON中的标识引用对象的标准方法?例如,为了使具有大量(可能是循环的)引用的图和其他数据结构能够被合理地序列化/加载?

Edit: I know that it's easy to do one-off solutions (“make a list of all the nodes in the graph, then …”). I'm wondering if there is a standard, generic, solution to this problem.

编辑:我知道做一次性的解决方案很容易(“把图表中的所有节点列出来,然后……”)。我想知道这个问题是否有一个标准的、通用的解决方案。

4 个解决方案

#1


11  

I was searching on this same feature recently. There does not seem to be a standard or ubiquitous implementation for referencing in JSON. I found a couple of resources that I can share:

我最近在搜索这个功能。在JSON中引用似乎没有标准的或无所不在的实现。我找到了一些可以分享的资源:

  • The Future for JSON Referencing
  • JSON引用的未来

http://groups.google.com/group/json-schema/browse_thread/thread/95fb4006f1f92a40 - This is just a discussion on id-based referencing.

http://groups.google.com/group/json-schema/browse_thread/thread/95fb4006f1f92a40——这只是基于idbased reference的讨论。

  • JSON Referencing in Dojo
  • JSON在Dojo引用

http://www.sitepen.com/blog/2008/06/17/json-referencing-in-dojo/ - An implementation in Dojox (extensions for the Dojo framework) - discusses id-based and path based referencing.

http://www.sitepen.com/blog/2008/06/17/json- referencingin -dojo/是Dojox (Dojo框架的扩展)中的一个实现,讨论基于id和基于路径的引用。

  • JSONPath - XPath for JSON
  • JSON的XPath

http://goessner.net/articles/JsonPath/ - This seems to be an attempt at establishing a standard for path based JSON referencing - maybe a small subset of XPath (?). There seems to be an implementation here but I kept getting errors on the download section - you might have better luck. But again this is no where close to a standard yet.

http://goessner.net/articles/JsonPath/——这似乎是试图为基于路径的JSON引用建立一个标准——可能是XPath(?)的一个小子集。这里似乎有一个实现,但我在下载部分不断出错——您可能会有更好的运气。但同样,这还没有接近标准。

#2


8  

There is the "JSON Reference" specification, but it seems it didn't got over the state of an expired Internet draft.

这里有“JSON引用”规范,但它似乎没有克服过期的Internet草案的状态。

Still, it seems to be used in JSON Schema and Swagger (now OpenAPI) (for reusing parts of an API description in other places of the same or another API description).

尽管如此,它似乎在JSON模式和Swagger(现在的OpenAPI)中使用(用于在相同或另一个API描述的其他地方重用API描述的部分)。

A reference to an object in the same file looks like this: { "$ref": "#/definitions/Problem" }.

对同一文件中的对象的引用如下:{“$ref”:“#/definition /Problem”}。

#3


4  

Douglas Crockford has a solution that uses JSONPath (an Xpath-like syntax for describing json paths). It seems fairly sane:

Douglas Crockford有一个使用JSONPath(一种描述json路径的类xpath语法)的解决方案。似乎相当理智的:

https://github.com/douglascrockford/JSON-js/blob/master/cycle.js

https://github.com/douglascrockford/JSON-js/blob/master/cycle.js

#4


1  

There is no canonical way to achieve that. JSON does not have a native support for references, so you have to invent your own scheme for unique identifiers which will act as pointers. If you really want to make it generic you could use the object identifiers provided by your programming language (eg. object_id in Ruby or id(obj) in Python).

没有一种规范的方法可以实现这一点。JSON没有对引用的本机支持,因此您必须为惟一标识符创建自己的方案,它将充当指针。如果你真的想让它通用,你可以使用你的编程语言提供的对象标识符。Ruby中的object_id或Python中的id(obj))。

#1


11  

I was searching on this same feature recently. There does not seem to be a standard or ubiquitous implementation for referencing in JSON. I found a couple of resources that I can share:

我最近在搜索这个功能。在JSON中引用似乎没有标准的或无所不在的实现。我找到了一些可以分享的资源:

  • The Future for JSON Referencing
  • JSON引用的未来

http://groups.google.com/group/json-schema/browse_thread/thread/95fb4006f1f92a40 - This is just a discussion on id-based referencing.

http://groups.google.com/group/json-schema/browse_thread/thread/95fb4006f1f92a40——这只是基于idbased reference的讨论。

  • JSON Referencing in Dojo
  • JSON在Dojo引用

http://www.sitepen.com/blog/2008/06/17/json-referencing-in-dojo/ - An implementation in Dojox (extensions for the Dojo framework) - discusses id-based and path based referencing.

http://www.sitepen.com/blog/2008/06/17/json- referencingin -dojo/是Dojox (Dojo框架的扩展)中的一个实现,讨论基于id和基于路径的引用。

  • JSONPath - XPath for JSON
  • JSON的XPath

http://goessner.net/articles/JsonPath/ - This seems to be an attempt at establishing a standard for path based JSON referencing - maybe a small subset of XPath (?). There seems to be an implementation here but I kept getting errors on the download section - you might have better luck. But again this is no where close to a standard yet.

http://goessner.net/articles/JsonPath/——这似乎是试图为基于路径的JSON引用建立一个标准——可能是XPath(?)的一个小子集。这里似乎有一个实现,但我在下载部分不断出错——您可能会有更好的运气。但同样,这还没有接近标准。

#2


8  

There is the "JSON Reference" specification, but it seems it didn't got over the state of an expired Internet draft.

这里有“JSON引用”规范,但它似乎没有克服过期的Internet草案的状态。

Still, it seems to be used in JSON Schema and Swagger (now OpenAPI) (for reusing parts of an API description in other places of the same or another API description).

尽管如此,它似乎在JSON模式和Swagger(现在的OpenAPI)中使用(用于在相同或另一个API描述的其他地方重用API描述的部分)。

A reference to an object in the same file looks like this: { "$ref": "#/definitions/Problem" }.

对同一文件中的对象的引用如下:{“$ref”:“#/definition /Problem”}。

#3


4  

Douglas Crockford has a solution that uses JSONPath (an Xpath-like syntax for describing json paths). It seems fairly sane:

Douglas Crockford有一个使用JSONPath(一种描述json路径的类xpath语法)的解决方案。似乎相当理智的:

https://github.com/douglascrockford/JSON-js/blob/master/cycle.js

https://github.com/douglascrockford/JSON-js/blob/master/cycle.js

#4


1  

There is no canonical way to achieve that. JSON does not have a native support for references, so you have to invent your own scheme for unique identifiers which will act as pointers. If you really want to make it generic you could use the object identifiers provided by your programming language (eg. object_id in Ruby or id(obj) in Python).

没有一种规范的方法可以实现这一点。JSON没有对引用的本机支持,因此您必须为惟一标识符创建自己的方案,它将充当指针。如果你真的想让它通用,你可以使用你的编程语言提供的对象标识符。Ruby中的object_id或Python中的id(obj))。