Skip to main content

Simple, easy to use & transparent python objects serialization & deserialization.

Project description

EzSerialization

Ruff PyPI mypy License

ezserialization - Simple, easy to use & transparent python objects serialization & deserialization by reference.

About

EzSerialization is meant to be simple in features and usage. It follows these three ideas:

  • Python dicts based. This package only helps to serialize objects to dicts. Converting them to JSON, XML, etc. is left to the user.
  • Transparent serialization logic. It does not have automatic from_dict() & to_dict() methods that convert class instances of any kind to dicts. Implementing them is left to the end-user, thus being transparent with what actually happens with this data.
  • Thread-safe. Serialization, deserialization & its enabling/disabling is thread-safe.

All EzSerialization do is it wraps to_dict() & from_dict() methods for selected classes to inject, register and use class type information for deserialization.

Install

Simply install from PyPI via pip command:

pip install ezserialization

Or use Poetry:

poetry install

Usage

To use this package:

  • implement Serializable protocol for your classes by having defined to_dict() and from_dict() methods;
  • decorate your classes with @serializable.

During serialization, simply use your implemented to_dict() method, and it will return your defined dict {'some_value': 'wow', ...} injected with class type information {'_type_': 'example.module.Example', 'some_value': 'wow', ...}.

During de-serialization (via deserialize() method) the modified dict's _type_ property will be removed and used to import example.module module dynamically. Finally, the found Example class' from_dict() method will be used to create new object from the original dict.

Here's an example:

from pprint import pprint
from typing import Mapping
from ezserialization import serializable, deserialize, no_serialization

@serializable
class Example:
    def __init__(self, value: str):
        self.value = value

    def to_dict(self) -> dict:
        return {"some_value": self.value}

    @classmethod
    def from_dict(cls, src: Mapping):
        return cls(value=src["some_value"])


obj = Example("wow")

# Serialization without ability to automatically deserialize:
with no_serialization():
    raw_obj_dict = obj.to_dict()
pprint(raw_obj_dict, indent=2)
# Output:
# {'some_value': 'wow'}

# Serialization WITH automatic deserialization:
obj_dict = obj.to_dict()
pprint(obj_dict, indent=2)
# Output:
# {'_type_': '__main__.Example', 'some_value': 'wow'}

obj2 = deserialize(obj_dict)
print(obj.value == obj2.value)
# Output:
# True

Context managing

EzSerialization supports two context managers:

  • with no_serialization(): ... - disables injecting class type metadata into the result of to_dict() method. Leaves the result dict unfit to be deserialized automatically via deserialize();
  • with use_serialization(): ... - opposite of no_serialization(), enables class type metadata injection. Useful when using inside the disabled serialization scope.

Configuration

Currently only a single option is available for customizing ezserialization:

  • ezserialization.type_field_name - by default it is set to _type_, however if user's solution has to_dict() methods that already contain such field, an alternative field name can be set to override the default one.

Contribution

Want to contribute? Create an issue ticket at GitHub & let's discuss 🤗

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ezserialization-0.6.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ezserialization-0.6.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file ezserialization-0.6.1.tar.gz.

File metadata

  • Download URL: ezserialization-0.6.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.10.20 Linux/6.14.0-1017-azure

File hashes

Hashes for ezserialization-0.6.1.tar.gz
Algorithm Hash digest
SHA256 1ac529f38799794e2336977615e71cf894c0e41894fe535d8a368133a026bb40
MD5 19ea601b569226405c2eb00be67a1a8a
BLAKE2b-256 e9787267eae3be8a6c28be5ffc62a5957e63f72ea044d4f294766891f18219e9

See more details on using hashes here.

File details

Details for the file ezserialization-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: ezserialization-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.10.20 Linux/6.14.0-1017-azure

File hashes

Hashes for ezserialization-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 48895a2b9d1d89a5afdc3a53f6a26cb50d208a3f758b4ebb5c38bd26590a4ea9
MD5 4bdc4f4b3bd64ab2f4549bc87504b5f2
BLAKE2b-256 d898aa3cdb2d62e203e880bd3e2d2a01fbf6b094d6a2adada5c2b3b5392bce05

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page