JSON schema generation from dataclasses
Project description
JSON schema generation from python 3.7 dataclasses. Python 3.6 is supported through the dataclasses backport. Also provides serialisation to and from JSON data with JSON schema validation.
Examples
from dataclasses import dataclass
from dataclasses_jsonschema import JsonSchemaMixin
@dataclass
class Point(JsonSchemaMixin):
x: float
y: float
Generate the schema:
>>> pprint(Point.json_schema())
{
'description': 'Point(x:float, y:float)',
'type': 'object',
'properties': {
'x': {'format': 'float', 'type': 'number'},
'y': {'format': 'float', 'type': 'number'}
},
'required': ['x', 'y']
}
Deserialise data:
>>> Point.from_dict({'x': 3.14, 'y': 1.5})
Point(x=3.14, y=1.5)
>>> Point.from_dict({'x': 3.14, y: 'wrong'})
jsonschema.exceptions.ValidationError: 'wrong' is not of type 'number'
For more examples see the tests
TODO
Support type Union using ‘oneOf’
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
File details
Details for the file dataclasses-jsonschema-1.5.1.tar.gz
.
File metadata
- Download URL: dataclasses-jsonschema-1.5.1.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 958b280949790659b9f38c29e0657f046e71c39b1e16016bc7fb98c081ad999a |
|
MD5 | 8adb7e13f123559db75baaac6847db4c |
|
BLAKE2b-256 | 5daf71f203694d2ddc347a7aaa8806760c29dc21835d84cb0dff4acee1e37f76 |