Skip to main content

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'

TODO

  • Support type Union using ‘oneOf’

  • Support NewType fields

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.0.0.tar.gz (4.0 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for dataclasses-jsonschema-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e7859ea448d639e483f271562dba849f47db37a00fbd57478f32ce3b51e44ba4
MD5 f2cc58acf2b6d7cb4b2830442445583e
BLAKE2b-256 e7610225262773ceac08ff2ab09c6c67417e076d6602a76618ee89a9e45a56f7

See more details on using hashes here.

Supported by

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