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
}
},
"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.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
Built Distribution
File details
Details for the file python_jsonschema_objects-0.0.1.tar.gz
.
File metadata
- Download URL: python_jsonschema_objects-0.0.1.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5acfa00cf1caa6241349247e67a2c6ca95cad48515a5ae9e2d2ecab67793c6f |
|
MD5 | d0f2f3047513369a7bcf358b07d69f72 |
|
BLAKE2b-256 | 5f5c4e87fda1155a8f1a4d48a844312aa4cbcb90c7a7bff04a0af2ec37224be2 |
File details
Details for the file python_jsonschema_objects-0.0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: python_jsonschema_objects-0.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7e3774eb884905c11c65d5345ec9dbb174e3e7f2355eac4b8ac461ac12a3be9 |
|
MD5 | 7894fab7568518f63750c497f1dbe996 |
|
BLAKE2b-256 | a4f1d2e852746e3c627ad807a0f6d2f35ca72d9d298c39669ad0bfcf7ea59f0e |