GrandSchemaThings
Repository: GitHub PyPI: GrandSchemaThings
The GrandSchemaThings Python package provides automatic loading and saving of JSON for Python objects, with validation against an automatically-generated schema. It is designed to make it easy to serialize and deserialize objects to and from JSON.
Why use GrandSchemaThings?
Compared to a simple dataclasses.asdict() approach, GrandSchemaThings:
- Reconstructs typed dataclass objects from JSON
- Automatically generates a matching JSON Schema
- Validates incoming data against that schema when loading
- Recursively handles nested GrandSchemaThings dataclasses, enums, lists and dictionaries
- Supported field types are: int, str, float, bool, Enum, list[T], dict[str, T], dict[Enum, T], and nested GrandSchemaThings subclasses. JSON-compatible restrictions apply.
- Enums are serialised using their name (for example,
Hobby.READINGbecomes"READING") and are represented in the schema as JSON schema enums
- Provides a single, consistent API for serialisation and deserialisation
User Guide
System Requirements
- Python >= 3.13
Installation
Install from PyPI using pip:
pip install grandschemathings
Or, if you use uv or Poetry:
uv add grandschemathings
poetry add grandschemathings
Usage
Simple Usage Example
Here's a simple example to illustrate how you can use GrandSchemaThings to serialize and deserialize an object.
from dataclasses import dataclass
from pathlib import Path
from grandschemathings import GrandSchemaThings
@dataclass
class MyData(GrandSchemaThings):
name: str
age: int
# Create an instance
data = MyData(name="Alice", age=30)
# Serialize to JSON file
data.to_file(Path("data.json"), pretty=True)
# Deserialize from JSON file
loaded_data = MyData.from_file(Path("data.json"))
print(loaded_data)
Complex Usage Example
This demonstrates how to handle nested objects and dictionaries with enum keys.
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from grandschemathings import GrandSchemaThings
class Hobby(Enum):
READING = "reading"
CYCLING = "cycling"
COOKING = "cooking"
GARDENING = "gardening"
@dataclass
class Address(GrandSchemaThings):
street: str
city: str
postcode: str
@dataclass
class User(GrandSchemaThings):
name: str
age: int
address: Address
hobbies: dict[Hobby, int]
# Create an instance
user = User(
name="Bob",
age=25,
address=Address(street="456 Elm St", city="Faketown", postcode="AB12 3CD"),
hobbies={Hobby.READING: 5, Hobby.CYCLING: 3, Hobby.COOKING: 4, Hobby.GARDENING: 1},
)
# Serialize to JSON file
user.to_file(Path("user.json"), pretty=True)
# Deserialize from JSON file
loaded_user = User.from_file(Path("user.json"))
print(loaded_user)
API Reference
The following instance and class methods are available for GrandSchemaThings objects.
| Method Name | Description | Type |
|---|---|---|
from_file(...) |
Creates an instance from a JSON file. | Class Method |
to_file(...) |
Writes the instance to a JSON file. | Instance Method |
from_json_dict(...) |
Creates an instance of the class from JSON data (in Python dict form). | Class Method |
to_json_dict() |
Creates a Python dict containing JSON-compatible data representing the instance. | Instance Method |
list_from_file(...) |
Loads a list of instances from a JSON file. | Class Method |
list_to_file(...) |
Writes a list of instances to a specified file. | Class Method |
schema(...) |
Generates a full schema for the class, optionally including the schema version. | Class Method |
schema_to_file(...) |
Saves the schema for the class to a specified file. | Class Method |
Developer Guide
Development Setup
To modify GrandSchemaThings, you can clone the repository and install the development dependencies using Poetry:
- Install Poetry: If you haven't already got Poetry installed, follow the instructions on their website.
- Clone the repository:
git clone https://github.com/cambridgeconsultants/GrandSchemaThings.git cd grandschemathings
- Install dependencies:
poetry install
Running Tests
Run the validation suite:
poetry run validate
License
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
Release files for grandschemathings 1.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| grandschemathings-1.1.8.tar.gz | 9.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| grandschemathings-1.1.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.0 kB
Release files / grandschemathings-1.1.8.tar.gz
| Download URL | grandschemathings-1.1.8.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c546b3ea3a478d73bb6a9cb9d299178041108a23f13b04a8aca37c0ad0bab88f
|
|
BLAKE2b-256 checksum How to use checksums |
88911b754202155d40e6bd76c8ada8795d4ba875be1861d099eef689004c58e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 4, 2026.
Transparency logRelease files / grandschemathings-1.1.8-py3-none-any.whl
| Download URL | grandschemathings-1.1.8-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2027f90c043c0fbbb3e2cc9c590be4b5df8b9b544843cd0aef2720005fa8a071
|
|
BLAKE2b-256 checksum How to use checksums |
22c4eefc5b89ad11132ff254f4c71090310c148f0e32539d3e914286a0140ee0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 4, 2026.
Transparency log