A serialization library for Python
Project description
Serialite
Serialite is a library serializing and deserializing arbitrarily complex objects in Python. You
apply the @serializable decorator to a dataclass to automatically create to_data and from_data
methods using the type annotations. Or for more control, inherit from the SerializableMixin and
implement the class attribute __fields_serializer__. For even more control, inherit from the
abstract base class Serializer and implement the to_data and from_data methods directly.
Basics
The abstract base class is Serializer:
class Serializer(Generic[Output]):
def from_data(self, data: Json) -> DeserializationSuccess[Output]: ...
def to_data(self, value: Output) -> Json: ...
The class is generic in the type of the object that it serializes. The two abstract methods
from_data and to_data are the key to the whole design, which revolves around getting objects to
and from JSON-serializable data, which are objects constructed entirely of bools, ints,
floats, lists, and dicts. Such structures can be consumed by json.dumps to produce a string
and produced by json.loads after consuming a string. By basing the serialization around JSON
serializable data, complex structures can be built up or torn down piece by piece while
alternatively building up complex error messages during deserialization which pinpoint the location
in the structure where the bad data exist.
For new classes, it is recommended that the Serializer be implemented on the class itself. There is
an abstract base class Serializable that classes can inherit from to indicate this. There is a mixin
SerializableMixin that provides an implementation of from_data and to_data for any class that
implements the __fields_serializer class attribute.
For dataclasses, it is even easier. There is a decorator serializable that inserts
SerializableMixin into the list of base classes after the dataclass decorator has run and also
generates __fields_serializer__ from the data class attributes.
Finding the correct serializer for each type can be a pain, so
serializer(cls: type) -> Serializer is provided as a convenience function. This is a single
dispatch function, which looks up the serializer registered for a particular type. For example,
serializer(list[float]) will return ListSerializer(FloatSerializer).
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 serialite-0.5.1.tar.gz.
File metadata
- Download URL: serialite-0.5.1.tar.gz
- Upload date:
- Size: 24.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c54c4992aa8e8ded15a7eea465871f44847e532e97dce337057207dee1b9e6ba
|
|
| MD5 |
d941687ae962955707308c21ff0b89c0
|
|
| BLAKE2b-256 |
a7cb986e4f4272017eb89dc55ae19d36d5bf21d3d27b710a5a5fcf4844ea5db9
|
Provenance
The following attestation bundles were made for serialite-0.5.1.tar.gz:
Publisher:
release.yml on drhagen/serialite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
serialite-0.5.1.tar.gz -
Subject digest:
c54c4992aa8e8ded15a7eea465871f44847e532e97dce337057207dee1b9e6ba - Sigstore transparency entry: 842505471
- Sigstore integration time:
-
Permalink:
drhagen/serialite@96c4bda5a442cdad64a5f08732473641a4200aa6 -
Branch / Tag:
refs/tags/v0.5.1 - Owner: https://github.com/drhagen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@96c4bda5a442cdad64a5f08732473641a4200aa6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file serialite-0.5.1-py3-none-any.whl.
File metadata
- Download URL: serialite-0.5.1-py3-none-any.whl
- Upload date:
- Size: 38.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97402280d38d7672e748e4cac59ef2c42ca0f114a83fa8be96a3422f1077909f
|
|
| MD5 |
56f91c8bb9d007c140e92a9cb5883d46
|
|
| BLAKE2b-256 |
22017bd0fdbf164c9008a5c01a350f6b1a32deb303b4785ed9bccdb877b7da89
|
Provenance
The following attestation bundles were made for serialite-0.5.1-py3-none-any.whl:
Publisher:
release.yml on drhagen/serialite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
serialite-0.5.1-py3-none-any.whl -
Subject digest:
97402280d38d7672e748e4cac59ef2c42ca0f114a83fa8be96a3422f1077909f - Sigstore transparency entry: 842505474
- Sigstore integration time:
-
Permalink:
drhagen/serialite@96c4bda5a442cdad64a5f08732473641a4200aa6 -
Branch / Tag:
refs/tags/v0.5.1 - Owner: https://github.com/drhagen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@96c4bda5a442cdad64a5f08732473641a4200aa6 -
Trigger Event:
push
-
Statement type: