An object wrapper for JSON Schema definitions
Project description
# python-jsonschema-objects
## What
python-jsonschema-objects provides an *automatic* class-based
binding to JSON schemas for use in python.
For example, given the following schema:
``` 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`:
``` python
>>> 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.
``` python
>>> james.age = -2
python_jsonschema_objects.validators.ValidationError: -4 was less
or equal to than 0
```
The object can be serialized out to JSON:
``` python
>>> 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](http://json-schema.org).
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.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.
## What
python-jsonschema-objects provides an *automatic* class-based
binding to JSON schemas for use in python.
For example, given the following schema:
``` 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`:
``` python
>>> 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.
``` python
>>> james.age = -2
python_jsonschema_objects.validators.ValidationError: -4 was less
or equal to than 0
```
The object can be serialized out to JSON:
``` python
>>> 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](http://json-schema.org).
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.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
Built Distribution
File details
Details for the file python_jsonschema_objects-0.0.3.tar.gz
.
File metadata
- Download URL: python_jsonschema_objects-0.0.3.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c65afda537f65c10442712745fa597fb21730af3d1585d9f02615682ccf1ff1 |
|
MD5 | 8c800f7acfe110289e2f05269926230c |
|
BLAKE2b-256 | 6b6251e58ca2af6e4d2131a4d8f9a5583683d933fd6f5fe9104df5065c337415 |
File details
Details for the file python_jsonschema_objects-0.0.3-py2.py3-none-any.whl
.
File metadata
- Download URL: python_jsonschema_objects-0.0.3-py2.py3-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d78516ba56979de9309c6548105ac80ef7ad7850881df143bbe6554bf4f3a95d |
|
MD5 | b503b6e696722cdd53edd52515f69fe9 |
|
BLAKE2b-256 | a0f7a8caa302ccb32b9937cf3c5dc9244b6c588922afee7d429e265c06bdc6be |