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. to_json raises ValueError for NaN or infinite floats, which standard JSON can't represent; from_json still reads the NaN / Infinity literals which older versions wrote.

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

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.3.2
File Size Uploaded Provenance
serializable-1.3.2.tar.gz 25.7 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 46.8 kB

Release files / serializable-1.3.2.tar.gz

Download URL serializable-1.3.2.tar.gz
Size 25.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1bf9d2b3c3ea7a3bf49116c8f80b0426ae85ede2be7ee1bed06e5d51df34d6ce
BLAKE2b-256 checksum
How to use checksums
6dcc5afa47105705da59f646c9a921fb596dce9f1020a7f6446e182620d109fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.12

Release files / serializable-1.3.2-py3-none-any.whl

Download URL serializable-1.3.2-py3-none-any.whl
Size 21.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23f131eecc759751f8af3932b05833c130e0d476eba09056893b9d35887c7d15
BLAKE2b-256 checksum
How to use checksums
65f30a6407f6f59c65788369eb5f9f9913572bcb23cfc01f5f17a8984fe02844
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.12

Security for serializable 1.3.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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