json引用和json指针实现
jref的Python项目详细描述
python jref
这个python包实现了json引用和json指针 处理json和yaml内容上下文中的规范,例如 swagger和openapi规范。
JSON引用
json引用定义了一种引用并包含来自 相同或单独的文件。这有助于维护结构化数据和 通过减少重复和允许组织更大的 跨多个文件的文档。
json指针
json指针定义用于标识json特定部分的语法 价值。
在文档中,json引用采用对象的形式,将'$ref'映射到 引用;忽略对象中的其他键。引用本身采用 uri的形式。例如:
{ "$ref": "http://example.com/example.json#/foo/bar" }
在这个实现中,uri可以引用json或yaml文件 本地文件系统,或通过http/https从网络提供服务。碎片 uri的一部分被解释为json指针。
用法示例
在下面的示例中也可以找到^{
importosimporttextwrapimportjref.contextimportjref.pointer# All references are evaluated in a context, so start with onectx=jref.context.RemoteContext()# Reference a remote documentspec=ctx.parse_reference('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml')# Reference portions of that documentspec_title=spec.context.parse_reference('#/info/title')spec_version=spec.context.parse_reference('#/info/version')# Print out the references, not the content (which hasn't been loaded)print(textwrap.dedent(''' * JSON References Spec: {} Title: {} Version: {} ''').lstrip().format(spec,spec_title,spec_version))# Expand references, triggering loading of contentprint(textwrap.dedent(''' * Evaluated references Title: {} Version: {} ''').lstrip().format(spec_title.expand(),spec_version.expand()))
预期输出:
* JSON References
Spec: https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml
Title: https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml#/info/title
Version: https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml#/info/version
* Evaluated references
Title: Swagger Petstore
Version: 1.0.0
参考文献
- JSON引用,Internet草稿, https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03
- json指针,rfc 6901,https://tools.ietf.org/html/rfc6901