JSON Reference and JSON Pointer implementations
Project description
python-jref
This python package implements the JSON Reference and JSON Pointer specifications in the context of processing JSON and YAML content, such as Swagger and OpenAPI specifications.
JSON Reference
JSON Reference defines a mechanism to reference and include content from the same or separate documents. This helps with maintenance of structured data and documents by reducing repetition, and allowing for the organization of larger documents across multiple files.
JSON Pointer
JSON Pointer defines a syntax for identifying specific portions of a JSON value.
In a document, a JSON Reference takes the form of an object, mapping '$ref' to a reference; other keys in the object are ignored. The reference itself takes the form of a URI. As an example:
{ "$ref": "http://example.com/example.json#/foo/bar" }
In this implementation, the URI may reference a JSON or YAML file available in the local filesystem, or served from the network over HTTP/HTTPS. The fragment portion of the URI is interpreted as a JSON Pointer.
Usage example
Find the example below also as usage-example.py
in the
source repository.
import os
import textwrap
import jref.context
import jref.pointer
# All references are evaluated in a context, so start with one
ctx = jref.context.RemoteContext()
# Reference a remote document
spec = ctx.parse_reference('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml')
# Reference portions of that document
spec_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 content
print(textwrap.dedent('''
* Evaluated references
Title: {}
Version: {}
''')
.lstrip()
.format(spec_title.expand(), spec_version.expand()))
Expected output:
* 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
References
- JSON Reference, Internet Draft, https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03
- JSON Pointer, RFC 6901, https://tools.ietf.org/html/rfc6901
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jref-0.4.1.tar.gz
.
File metadata
- Download URL: jref-0.4.1.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea69859bdaf768feb34b1eb46ae52aefc90a4b436a6602ce7a106376373be3e8 |
|
MD5 | 686fb54db73a873077050bd5e288bdb5 |
|
BLAKE2b-256 | baea3a973f03432453b40f22356be39ae983156ea73cf637629b93fac605f555 |
File details
Details for the file jref-0.4.1-py2.py3-none-any.whl
.
File metadata
- Download URL: jref-0.4.1-py2.py3-none-any.whl
- Upload date:
- Size: 23.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 272db831f52bc461de5b32a1c9d69d9fa9bd2b6de437174c57beea8178ad4215 |
|
MD5 | b30912a6cf0680850aa99f2ff797d0d7 |
|
BLAKE2b-256 | 7bf3b2c5092d06f48c5eb9c509a6d9d173ebca60905136e3ef723c1bb4109e01 |