{"id":13494641,"url":"https://github.com/s-knibbs/dataclasses-jsonschema","last_synced_at":"2025-09-27T00:31:34.244Z","repository":{"id":37561597,"uuid":"135908312","full_name":"s-knibbs/dataclasses-jsonschema","owner":"s-knibbs","description":"JSON schema generation from dataclasses","archived":true,"fork":false,"pushed_at":"2023-08-19T19:12:26.000Z","size":232,"stargazers_count":166,"open_issues_count":41,"forks_count":38,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-09-21T08:02:28.446Z","etag":null,"topics":["dataclasses","deserialization","jsonschema","openapi","python","serialization","type-hints","validation"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/s-knibbs.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-06-03T13:51:16.000Z","updated_at":"2024-08-09T08:56:37.000Z","dependencies_parsed_at":"2023-09-25T02:19:36.649Z","dependency_job_id":null,"html_url":"https://github.com/s-knibbs/dataclasses-jsonschema","commit_stats":{"total_commits":106,"total_committers":13,"mean_commits":8.153846153846153,"dds":"0.49056603773584906","last_synced_commit":"3c6122c0d8ce5f52ee7b1230f5c13c428e6b9924"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-knibbs%2Fdataclasses-jsonschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-knibbs%2Fdataclasses-jsonschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-knibbs%2Fdataclasses-jsonschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-knibbs%2Fdataclasses-jsonschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/s-knibbs","download_url":"https://codeload.github.com/s-knibbs/dataclasses-jsonschema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219871828,"owners_count":16554457,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dataclasses","deserialization","jsonschema","openapi","python","serialization","type-hints","validation"],"created_at":"2024-07-31T19:01:26.792Z","updated_at":"2025-09-27T00:31:33.887Z","avatar_url":"https://github.com/s-knibbs.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"Dataclasses JSON Schema\n=======================\n\n.. image:: https://github.com/s-knibbs/dataclasses-jsonschema/workflows/Tox%20tests/badge.svg?branch=master\n    :target: https://github.com/s-knibbs/dataclasses-jsonschema/actions\n\n.. image:: https://badge.fury.io/py/dataclasses-jsonschema.svg\n    :target: https://badge.fury.io/py/dataclasses-jsonschema\n\n**⚠️Please Note⚠️:** **Because of health reasons I'm not longer able to make changes to this project or make further releases via PyPI.**\n\nA library to generate JSON Schema from python 3.7 dataclasses. Python 3.6 is supported through the `dataclasses backport \u003chttps://github.com/ericvsmith/dataclasses\u003e`_. Aims to be a more lightweight alternative to similar projects such as `marshmallow \u003chttps://github.com/marshmallow-code/marshmallow\u003e`_ \u0026 `pydantic \u003chttps://github.com/samuelcolvin/pydantic\u003e`_.\n\nFeature Overview\n----------------\n\n* Support for draft-04, draft-06, Swagger 2.0 \u0026 OpenAPI 3 schema types\n* Serialisation and deserialisation\n* Data validation against the generated schema\n* `APISpec \u003chttps://github.com/marshmallow-code/apispec\u003e`_ support. Example below_:\n\nInstallation\n------------\n\n.. code:: bash\n\n    ~$ pip install dataclasses-jsonschema\n\nFor improved validation performance using `fastjsonschema \u003chttps://github.com/horejsek/python-fastjsonschema\u003e`_, install with:\n\n.. code:: bash\n\n    ~$ pip install dataclasses-jsonschema[fast-validation]\n\nFor improved uuid performance using `fastuuid \u003chttps://pypi.org/project/fastuuid/\u003e`_, install with:\n\n.. code:: bash\n\n    ~$ pip install dataclasses-jsonschema[fast-uuid]\n\nFor improved date and datetime parsing performance using `ciso8601 \u003chttps://pypi.org/project/ciso8601/\u003e`_, install with:\n\n.. code:: bash\n\n    ~$ pip install dataclasses-jsonschema[fast-dateparsing]\n\nBeware `ciso8601` doesn’t support the entirety of the ISO 8601 spec, only a popular subset.\n\n\nExamples\n--------\n\n.. code:: python\n\n    from dataclasses import dataclass\n\n    from dataclasses_jsonschema import JsonSchemaMixin\n\n\n    @dataclass\n    class Point(JsonSchemaMixin):\n        \"A 2D point\"\n        x: float\n        y: float\n\n\nSchema Generation\n^^^^^^^^^^^^^^^^^\n\n.. code:: python\n\n    \u003e\u003e\u003e pprint(Point.json_schema())\n    {\n        'description': 'A 2D point',\n        'type': 'object',\n        'properties': {\n            'x': {'format': 'float', 'type': 'number'},\n            'y': {'format': 'float', 'type': 'number'}\n        },\n        'required': ['x', 'y']\n    }\n\nData Serialisation\n^^^^^^^^^^^^^^^^^^\n.. code:: python\n\n    \u003e\u003e\u003e Point(x=3.5, y=10.1).to_dict()\n    {'x': 3.5, 'y': 10.1}\n\nDeserialisation\n^^^^^^^^^^^^^^^\n\n.. code:: python\n\n    \u003e\u003e\u003e Point.from_dict({'x': 3.14, 'y': 1.5})\n    Point(x=3.14, y=1.5)\n    \u003e\u003e\u003e Point.from_dict({'x': 3.14, y: 'wrong'})\n    dataclasses_jsonschema.ValidationError: 'wrong' is not of type 'number'\n\nGenerating multiple schemas\n^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n.. code:: python\n\n    from dataclasses_jsonschema import JsonSchemaMixin, SchemaType\n    \n    @dataclass\n    class Address(JsonSchemaMixin):\n        \"\"\"Postal Address\"\"\"\n        building: str\n        street: str\n        city: str\n    \n    @dataclass\n    class Company(JsonSchemaMixin):\n        \"\"\"Company Details\"\"\"\n        name: str\n        address: Address\n    \n    \u003e\u003e\u003e pprint(JsonSchemaMixin.all_json_schemas(schema_type=SchemaType.SWAGGER_V3))\n    {'Address': {'description': 'Postal Address',\n                 'properties': {'building': {'type': 'string'},\n                                'city': {'type': 'string'},\n                                'street': {'type': 'string'}},\n                 'required': ['building', 'street', 'city'],\n                 'type': 'object'},\n     'Company': {'description': 'Company Details',\n                 'properties': {'address': {'$ref': '#/components/schemas/Address'},\n                                'name': {'type': 'string'}},\n                 'required': ['name', 'address'],\n                 'type': 'object'}}\n        \n\nCustom validation using `NewType \u003chttps://docs.python.org/3/library/typing.html#newtype\u003e`_\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n.. code:: python\n\n    from dataclasses_jsonschema import JsonSchemaMixin, FieldEncoder\n\n    PhoneNumber = NewType('PhoneNumber', str)\n    \n    class PhoneNumberField(FieldEncoder):\n    \n        @property\n        def json_schema(self):\n            return {'type': 'string', 'pattern': r'^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$'}\n    \n    JsonSchemaMixin.register_field_encoders({PhoneNumber: PhoneNumberField()})\n    \n    @dataclass\n    class Person(JsonSchemaMixin):\n        name: str\n        phone_number: PhoneNumber\n\nFor more examples `see the tests \u003chttps://github.com/s-knibbs/dataclasses-jsonschema/blob/master/tests/conftest.py\u003e`_\n\n.. _below:\n\nAPISpec Plugin\n--------------\n**New in v2.5.0**\n\nOpenAPI \u0026 Swagger specs can be generated using the apispec plugin:\n\n.. code:: python\n\n    from typing import Optional, List\n    from dataclasses import dataclass\n\n    from apispec import APISpec\n    from apispec_webframeworks.flask import FlaskPlugin\n    from flask import Flask, jsonify\n    import pytest\n\n    from dataclasses_jsonschema.apispec import DataclassesPlugin\n    from dataclasses_jsonschema import JsonSchemaMixin\n\n\n    # Create an APISpec\n    spec = APISpec(\n        title=\"Swagger Petstore\",\n        version=\"1.0.0\",\n        openapi_version=\"3.0.2\",\n        plugins=[FlaskPlugin(), DataclassesPlugin()],\n    )\n    \n    \n    @dataclass\n    class Category(JsonSchemaMixin):\n        \"\"\"Pet category\"\"\"\n        name: str\n        id: Optional[int]\n\n    @dataclass\n    class Pet(JsonSchemaMixin):\n        \"\"\"A pet\"\"\"\n        categories: List[Category]\n        name: str\n\n\n    app = Flask(__name__)\n\n\n    @app.route(\"/random\")\n    def random_pet():\n        \"\"\"A cute furry animal endpoint.\n        ---\n        get:\n          description: Get a random pet\n          responses:\n            200:\n              content:\n                application/json:\n                  schema: Pet\n        \"\"\"\n        pet = get_random_pet()\n        return jsonify(pet.to_dict())\n \n    # Dependant schemas (e.g. 'Category') are added automatically\n    spec.components.schema(\"Pet\", schema=Pet)\n    with app.test_request_context():\n        spec.path(view=random_pet)\n\nTODO\n----\n\n* Add benchmarks against alternatives such as `pydantic \u003chttps://github.com/samuelcolvin/pydantic\u003e`_ and `marshmallow \u003chttps://github.com/marshmallow-code/marshmallow\u003e`_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-knibbs%2Fdataclasses-jsonschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fs-knibbs%2Fdataclasses-jsonschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-knibbs%2Fdataclasses-jsonschema/lists"}