Skip to main content

Tests PyPI PyPI downloads

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 positional argument to __init__
  • provide a user-defined to_dict() method which returns a dictionary whose keys match the arguments to __init__

Keyword-only arguments to __init__ (those after *) are left out of the default to_dict(), so they can be used for constructor options which aren't part of an object's serialized state.

Pickling goes through the same to_dict() / from_dict() pair, so pickles also survive renamed keywords (see below). Pickles written by versions before 1.2.0 can still be loaded.

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.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for serializable 1.2.0
File Size Uploaded
serializable-1.2.0.tar.gz 24.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for serializable 1.2.0
File Interpreter ABI Platform
serializable-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 44.9 kB

Release files / serializable-1.2.0.tar.gz

Download URL serializable-1.2.0.tar.gz
Size 24.5 kB
Tags Source
SHA-256 checksum
How to use checksums
45fd32dba2b9c2ca02298283ee6a2b0918708e6ba44bbae316b0326e1a811caf
BLAKE2b-256 checksum
How to use checksums
3744d5413748905f61f339cc59b1420577b388fba6a1770ff383d7b130037eca
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.2.0-py3-none-any.whl

Download URL serializable-1.2.0-py3-none-any.whl
Size 20.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02d05621331dcd5d64ebd468f83c38bfb43bbc30d5b6f08c8b575a83b7286cf2
BLAKE2b-256 checksum
How to use checksums
ce46a5648202bb0b100a9c947803e35960f5bc4a144443da82f18afe92c823ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.6

Release history Release notifications | RSS feed

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

This release

1.2.0 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page