Skip to main content

JSON schema generation from dataclasses

Project description

https://travis-ci.org/s-knibbs/dataclasses-jsonschema.svg?branch=master https://badge.fury.io/py/dataclasses-jsonschema.svg

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


Download files

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

Source Distribution

dataclasses-jsonschema-1.5.2.tar.gz (9.4 kB view details)

Uploaded Source

File details

Details for the file dataclasses-jsonschema-1.5.2.tar.gz.

File metadata

File hashes

Hashes for dataclasses-jsonschema-1.5.2.tar.gz
Algorithm Hash digest
SHA256 be58ff55853dd9beb8fec8632fa5de0a038c4babfb909255f6ec1110bfe1cf1b
MD5 5059219058209a4ab1b6b4706b7f40f9
BLAKE2b-256 33e3b4f44e859fec69065e653b7aac66cd8169fd7fa66a0fe2df97be2fd97df5

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