json 通过身份引用对象的标准方法(例如,循环引用)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4001474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Standard way of referencing an object by identity (for, eg, circular references)?
提问by David Wolever
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.
编辑:我知道一次性解决方案很容易(“列出图中的所有节点,然后……”)。我想知道这个问题是否有标准的、通用的解决方案。
采纳答案by Basel Shishani
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- 这只是关于基于 id 的引用的讨论。
- JSON Referencing in Dojo
- Dojo 中的 JSON 引用
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-referencing-in-dojo/- Dojox 中的一个实现(Dojo 框架的扩展) - 讨论了基于 id 和基于路径的引用。
- JSONPath - XPath for JSON
- JSONPath - 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 (?) 的一个小子集。这里似乎有一个实现,但我在下载部分不断收到错误 - 你可能有更好的运气。但同样,这还没有接近标准。
回答by Pa?lo Ebermann
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 Schemaand Swagger(now OpenAPI) (for reusing parts of an API description in other places of the same or another API description).
尽管如此,它似乎在JSON Schema和Swagger(现在是 OpenAPI)中使用(用于在相同或另一个 API 描述的其他地方重用 API 描述的部分)。
A reference to an object in the same file looks like this: { "$ref": "#/definitions/Problem" }.
在同一个文件中的对象的引用如下:{ "$ref": "#/definitions/Problem" }。
回答by Andrew Shooner
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
回答by Adam Byrtek
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_idin Ruby or id(obj)in Python).
没有规范的方法可以实现这一目标。JSON 没有对引用的本机支持,因此您必须为充当指针的唯一标识符发明自己的方案。如果你真的想让它通用,你可以使用你的编程语言(例如object_id在 Ruby 或id(obj)Python 中)提供的对象标识符。

