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
- Recursively handles nested objects when deserialising as well as serialising (such as enums, lists, dictionaries, and other GrandSchemaThings objects)
What types does GrandSchemaThings support?
Supported field types are:
int,str,floatandboolEnumlist[T]dict[str, T]anddict[Enum, T]- nested GrandSchemaThings subclasses
The following constraints are enforced:
- GrandSchemaThings follows a strict, fully explicit JSON model: all dataclass fields
must have fully concrete, non-nullable types
- So,
Optional[T],T | None, and otherUniontypes are intentionally disallowed - Dataclass field defaults are permitted as a Python-side convenience, but are still marked as required in generated JSON schemas
- So,
tupleandsetare not supported because they do not have a JSON equivalent- Enums are serialised using their name (for example, in the example below, Hobby.READING becomes "READING", not "reading") and are represented in the schema as JSON Schema enums
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 Person(GrandSchemaThings):
name: str
age: int
# Create an instance
data = Person(name="Alice", age=30)
# Serialize to JSON file
data.to_file(Path("data.json"), pretty=True)
# Deserialize from JSON file
loaded_data = Person.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="Robert",
age=42,
address=Address(street="28 Alderwick Road", city="London", postcode="SW14 7QJ"),
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.9
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.9.tar.gz | 10.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| grandschemathings-1.1.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.7 kB
Release files / grandschemathings-1.1.9.tar.gz
| Download URL | grandschemathings-1.1.9.tar.gz |
|---|---|
| Size | 10.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9320c699a6e7135135ecdd5a4c46163ae351b869102894a4c0d3427ff983203c
|
|
BLAKE2b-256 checksum How to use checksums |
017d20139976a26a9d57b852229021d529ac734523d1d3be905fa8b47e64589c
|
| 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 25, 2026.
Transparency logRelease files / grandschemathings-1.1.9-py3-none-any.whl
| Download URL | grandschemathings-1.1.9-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d8130a7211039d2c1ed7e05de059d09ce0c8fe520b58f4404447ecf05ead2f2d
|
|
BLAKE2b-256 checksum How to use checksums |
44ad27678d4dbd9352ef2fa16f82e6f3050dc858a2f0cd8ac75deb62965074f4
|
| 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 25, 2026.
Transparency log