Skip to main content

An implementation of JSON Reference for Python

Project description

https://readthedocs.org/projects/jsonref/badge/?version=latest https://travis-ci.org/gazpachoking/jsonref.png?branch=master https://coveralls.io/repos/gazpachoking/jsonref/badge.png?branch=master

jsonref is a library for automatic dereferencing of JSON Reference objects for Python (supporting Python 2.6+ and 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.2.tar.gz (13.0 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.2-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jsonref-0.2.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for jsonref-0.2.tar.gz
Algorithm Hash digest
SHA256 f3c45b121cf6257eafabdc3a8008763aed1cd7da06dbabc59a9e4d2a5e4e6697
MD5 42b518b9ccd6852d1d709749bc96cb70
BLAKE2b-256 b3cf93d4f34d76863d4fb995cb8e3e4f29908304065ce6381e0349700c44ad0c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jsonref-0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for jsonref-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b1e82fa0b62e2c2796a13e5401fe51790b248f6d9bf9d7212a3e31a3501b291f
MD5 a422046ec61ba88d2ffb281f0d730536
BLAKE2b-256 0792f8e4ac824b14af77e613984e480fa818397c72d4141fc466decb26752749

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