Skip to main content

jsonref is a library for automatic dereferencing of JSON Reference objects for Python.

Project description

jsonref

image image image image

jsonref is a library for automatic dereferencing of JSON Reference objects for Python (supporting Python 3.3+).

This library lets you use a data structure with JSON reference objects, as if the references had been replaced with the referent data.

>>> from pprint import pprint
>>> import jsonref

>>> # An example json document
>>> json_str = """{"real": [1, 2, 3, 4], "ref": {"$ref": "#/real"}}"""
>>> data = jsonref.loads(json_str)
>>> pprint(data)  # Reference is not evaluated until here
{'real': [1, 2, 3, 4], 'ref': [1, 2, 3, 4]}

Features

  • References are evaluated lazily. Nothing is dereferenced until it is used.
  • Recursive references are supported, and create recursive python data structures.

References objects are actually replaced by lazy lookup proxy objects which are almost completely transparent.

>>> data = jsonref.loads('{"real": [1, 2, 3, 4], "ref": {"$ref": "#/real"}}')
>>> # You can tell it is a proxy by using the type function
>>> type(data["real"]), type(data["ref"])
(<class 'list'>, <class 'jsonref.JsonRef'>)
>>> # You have direct access to the referent data with the __subject__
>>> # attribute
>>> type(data["ref"].__subject__)
<class 'list'>
>>> # If you need to get at the reference object
>>> data["ref"].__reference__
{'$ref': '#/real'}
>>> # Other than that you can use the proxy just like the underlying object
>>> ref = data["ref"]
>>> isinstance(ref, list)
True
>>> data["real"] == ref
True
>>> ref.append(5)
>>> del ref[0]
>>> # Actions on the reference affect the real data (if it is mutable)
>>> pprint(data)
{'real': [2, 3, 4, 5], 'ref': [2, 3, 4, 5]}

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

jsonref-0.3.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonref-0.3.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file jsonref-0.3.0.tar.gz.

File metadata

  • Download URL: jsonref-0.3.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.4 Windows/10

File hashes

Hashes for jsonref-0.3.0.tar.gz
Algorithm Hash digest
SHA256 68b330c6815dc0d490dbb3d65ccda265ddde9f7856fd2f3322f971d456ea7549
MD5 a251a9f31d761f664f70794b3f7c51d8
BLAKE2b-256 2fd08b97b067d5083812bd1b0facebe39c2b392e5e2d95d9caadc2bb1104fce4

See more details on using hashes here.

File details

Details for the file jsonref-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: jsonref-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.4 Windows/10

File hashes

Hashes for jsonref-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9480ad1b500f7e795daeb0ef29f9c55ae3a9ab38fb8d6659b6f4868acb5a5bc8
MD5 90a986d11fd34580f2121270e0422365
BLAKE2b-256 852349374e463cec3cf87154b7f53bd8c7df894802b820e123b0a7e0822d0af1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page