Skip to main content

An object wrapper for JSON Schema definitions

Project description

What

python-jsonschema-objects provides an automatic class-based binding to JSON schemas for use in python.

For example, given the following schema:

{
    "title": "Example Schema",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        },
        "dogs": {
            "type": "array",
            "items": {"type": "string"},
            "maxItems": 4
        }
    },
    "required": ["firstName", "lastName"]
}

jsonschema-objects can generate a class based binding. Assume here that the schema above has been loaded in a variable called schema:

>>> import python_jsonschema_objects as pjs
>>> builder = pjs.ObjectBuilder(schema)
>>> ns = builder.build_classes()
>>> Person = ns.ExampleSchema
>>> james = Person(firstName="James", lastName="Bond")
>>> james.lastName
u'Bond'
>>> james
<example_schema lastName=Bond age=None firstName=James>

Validations will also be applied as the object is manipulated.

>>> james.age = -2
python_jsonschema_objects.validators.ValidationError: -4 was less
or equal to than 0

The object can be serialized out to JSON:

>>> james.serialize()
'{"lastName": "Bond", "age": null, "firstName": "James"}'

Why

Ever struggled with how to define message formats? Been frustrated by the difficulty of keeping documentation and message definition in lockstep? Me too.

There are lots of tools designed to help define JSON object formats, foremost among them JSON Schema. JSON Schema allows you to define JSON object formats, complete with validations.

However, JSON Schema is language agnostic. It validates encoded JSON directly - using it still requires an object binding in whatever language we use. Often writing the binding is just as tedious as writing the schema itself.

This avoids that problem by auto-generating classes, complete with validation, directly from an input JSON schema. These classes can seamlessly encode back and forth to JSON valid according to the schema.

Installation

pip install python_jsonschema_objects

Tests

Tests are managed using the excellent Tox. Simply pip install tox, then tox.

Changelog

0.0.3b - Fixed ReStructuredText generation

0.0.3 - Added support for other array validations (minItems, maxItems, uniqueItems).

0.0.2 - Array item type validation now works. Specifying ‘items’, will now enforce types, both in the tuple and list syntaxes.

0.0.1 - Class generation works, including ‘oneOf’ and ‘allOf’ relationships. All basic validations work.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_jsonschema_objects-0.0.3b.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

python_jsonschema_objects-0.0.3b-py2.py3-none-any.whl (16.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file python_jsonschema_objects-0.0.3b.tar.gz.

File metadata

File hashes

Hashes for python_jsonschema_objects-0.0.3b.tar.gz
Algorithm Hash digest
SHA256 3a05e85e211cabc7938c9665ef764836d40f39659c5dc757750a61a518dc4f49
MD5 0565ca9ccf76beed8cc2890707fce0c7
BLAKE2b-256 03ab686c2d0fc95da2a8649d9f151860fca7c8c3a1c852cc16684ce8a815022b

See more details on using hashes here.

File details

Details for the file python_jsonschema_objects-0.0.3b-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for python_jsonschema_objects-0.0.3b-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d32eed55373db2d85116f021ad4a8f5fa88b27843b4e2392f0cd251f9a03018a
MD5 4614b5e421601d62b1f93876ca10be38
BLAKE2b-256 6bd9a48a6835df706f48dd45124800023513d1b73d555960226360fb5b586f90

See more details on using hashes here.

Supported by

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