Skip to main content

type-serialize

PyPI PyPI - Python Version GitHub

Serialize with type annotations

Features

  • Supported in Python 3.9 and later.
  • Serialize classes without additional code.
  • Deserialization using type annotations.
  • No dependencies
  • Compress serialization results: bz2, gzip, lzma, zlib - Removed to focus on 'serialization' itself.

Overview

To pass a custom object to the json.dumps and json.loads functions, there is the following method.

Both methods require additional code and have some problems.

  • Problem of not checking symbols when manipulating strings.
  • When adding/deleting/editing a property, all related codes must be changed together.
  • Painful typecasting (as the biggest problem).

As a way to hide these problems with a library and use serialization and deserialization, I chose type annotations. (Although the library is complicated; haha...) There are some additional advantages to using this.

  • Static type checking using mypy.
  • Autocomplete in IDE like PyCharm.

Things to know

  • All public fields are serialized.
  • Methods are not serialized.
  • Private fields that start with an underscore (_) are not serialized.
  • Members specified with the @property decorator are not serialized.
  • When deserializing, all fields must be type-annotated.
  • A value of None is ignored by the serialization target.
  • When deserializing, the __init__ function must have NO required arguments.
  • Implement __serialize__ to override the serialization method.
  • Implement __deserialize__ to override the deserialization method.

Installation

pip install type-serialize

Usage

Serializable python object

from dataclasses import dataclass
from type_serialize import deserialize, serialize


@dataclass
class Sample:
    field1: str
    field2: int


data = Sample(field1="a", field2=100)
obj = serialize(data)
assert isinstance(obj, dict)
assert obj["field1"] == "a"
assert obj["field2"] == 100
print(obj)

result = deserialize(obj, Sample)
assert isinstance(result, Sample)
assert data == result
print(result)

Override serialize and deserialize

from dataclasses import dataclass
from type_serialize import deserialize, serialize


@dataclass
class Sample:
    value: int

    def __serialize__(self):
        return {"a": self.value}

    def __deserialize__(self, data) -> None:
        self.value = data["a"]

    def __init__(self, value=100):
        self.value = value


test = Sample(value=200)
obj = serialize(test)
assert isinstance(obj, dict)
assert obj["a"] == 200
print(obj)

result = deserialize(obj, Sample)
assert isinstance(result, Sample)
assert test == result
print(result)

License

See the LICENSE file for details. In summary, type-serialize is licensed under the MIT license.

Metadata

Release files for type-serialize 2.1.0

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

Built distribution (wheel)

Table of built distributions (wheels) for type-serialize 2.1.0
File Interpreter ABI Platform
type_serialize-2.1.0-py3-none-any.whl Python 3 none any Details

Release files / type_serialize-2.1.0-py3-none-any.whl

Download URL type_serialize-2.1.0-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
787a6f6af9ba0d99adeea2509caa6e150acd7cc723171e6c2489194a53fd9007
BLAKE2b-256 checksum
How to use checksums
15d4764068e815e7400c571c528abbb33966a951bd541fa733d16c91742bd582
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Security for type-serialize 2.1.0

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.

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