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.0.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.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ezserialization-0.6.0.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.0.tar.gz
Algorithm Hash digest
SHA256 e74b8fc763bbb1a5b711859ee433ca5ec38ff360d3b6b0eac153a2697966df5e
MD5 7eae119e7afbdaeb32b4515f7262d088
BLAKE2b-256 1baf7d2d4e37c6701f03418ff8c679b11b8b90e22041c5501393fe30dfaa0947

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ezserialization-0.6.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f77c0f5c7b84c5837738bfef6a21a9718e380ff7ec66f8d55bdddeb398e9a790
MD5 0a185a19809fd5a1b29e5d4a0d0a12b8
BLAKE2b-256 7f4e536c47fe0deec71cd450806b13cc8db3afb0eb73899a197c87c8a43981fe

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