Base class with serialization helpers for user-defined Python objects
Project description
serializable
Base class with serialization methods for user-defined Python objects
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 _KEYWORD_ALIASES which maps old keywords to new names (or None if a keyword was removed).
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.
Limitations
-
Serializable objects must inherit from
Serializable, be tuples or namedtuples, be serializble primitive types such as dict, list, int, float, or str. -
The serialized representation of objects relies on reserved keywords (such as
"__name__", and"__class__"), so dictionaries are expected to not contain any keys which begin with two underscores.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file serializable-1.1.0.tar.gz.
File metadata
- Download URL: serializable-1.1.0.tar.gz
- Upload date:
- Size: 22.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65e05de319ab50aa4612ff0616384bbf8f53b38444b4bf9d8780db8cff6c1eac
|
|
| MD5 |
7a7b9ceded14f8a909383eb60dd9014f
|
|
| BLAKE2b-256 |
a8095d82e8f19db88014b7d4d0ed2d99cfa3967beb0d525f16e7a0982be1161a
|
File details
Details for the file serializable-1.1.0-py3-none-any.whl.
File metadata
- Download URL: serializable-1.1.0-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
122217f28d9cae17463ef909af94665e4ab2f9efdcb326870838382a1fd8101e
|
|
| MD5 |
c0eee45eaeda55106992335de76016bc
|
|
| BLAKE2b-256 |
9b3058b547fc9424ecda55d27a0b065d6495a93a1a350cf9d6b7a6d8f2c4dcfd
|