serializable
Base class with serialization methods for user-defined Python objects
Install
python -m pip install serializable
DataclassSerializable for @dataclass subclasses
If you're using @dataclass (e.g. in vaxrank, pyensembl, or varcode), inherit from DataclassSerializable instead of Serializable. It provides the same serialization surface — to_dict / from_dict / to_json / from_json — but leaves __init__, __eq__, __repr__, and __hash__ to @dataclass, so you get dataclass-native equality and repr without conflicts.
from dataclasses import dataclass
from serializable import DataclassSerializable
@dataclass
class Point(DataclassSerializable):
x: float
y: float
p = Point(1.0, 2.0)
assert Point.from_json(p.to_json()) == p
The on-wire JSON format is identical to Serializable, so mixed codebases interoperate: a DataclassSerializable instance can reference a legacy Serializable object (and vice versa) and still round-trip cleanly. The _SERIALIZABLE_KEYWORD_ALIASES hook works the same way for migrating field names across releases.
Usage
Classes which inherit from Serializable are enabled with default implementations of
to_json, from_json, __reduce__ (for pickling), and other serialization
helpers.
A derived class must either:
- have a member data matching the name of each argument to
__init__ - provide a user-defined
to_dict()method which returns a dictionary whose keys match the arguments to__init__
If you change the keyword arguments to a class which derives from Serializable but would like to be able to deserialize older JSON representations then you can define a class-level dictionary called _SERIALIZABLE_KEYWORD_ALIASES which maps old keywords to new names (or None if a keyword was removed).
Limitations
The format records Python classes and modules, which must remain importable when loading data. Keys starting with two underscores are reserved. Nested Python values require the serialization helpers rather than ordinary JSON encoding.
Documentation
The site builds with python -m pip install -r requirements-docs.txt followed
by ./docs.sh. Run python scripts/check_docs_examples.py to check the examples.
Metadata
Release files for serializable 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| serializable-1.1.2.tar.gz | 22.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| serializable-1.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 42.3 kB
Release files / serializable-1.1.2.tar.gz
| Download URL | serializable-1.1.2.tar.gz |
|---|---|
| Size | 22.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
91f273aa722c9948bd5ae84f86c7b6b7bdfe5bfb81c884904cac538af467e980
|
|
BLAKE2b-256 checksum How to use checksums |
cc85f0bedea9c501391e0c4de414fdcde52da52785cd8532d76c169e9736f1b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.6
|
Release files / serializable-1.1.2-py3-none-any.whl
| Download URL | serializable-1.1.2-py3-none-any.whl |
|---|---|
| Size | 19.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
45189e3ad191416d441584a8ac86dea9b4f52a4312c369a31d5e30b87ab0f75b
|
|
BLAKE2b-256 checksum How to use checksums |
dba5a18b3dc00b8ad5b3355eee0cc277fb9edfe17a74831b65bbb40b3eb2a36f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.6
|