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

参考文献

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
junit有没有办法在Java中重新初始化静态类?   在浏览器中点击应用程序时java Play框架挂起   文件Java错误中的NullPointerException   使用Java中的SNMP查找网络中计算机的登录名   java包装服务器引导程序已弃用,有什么替代方案?   当客户在等待理发时,java信号量值是否存在问题?   java如何使用JavaMail仅下载特定类型的附件   如何在java中将十进制转换为十六进制   java Slick2D粒子系统不会生成粒子   java检测更改事件来自何处   将Java集合类型参数类设置为数组   java如何从eclipse导出为可运行JAR文件?   java EntityManager对象未注入Glassfish和Spring   swing从actionPerformed和actionListener Java返回字符串   java在给定另一个等价键对象的情况下获取映射项的当前键   无论输入如何,java网络都会产生相同的输出