Simple, easy to use & transparent python objects serialization & deserialization.
Project description
EzSerialization
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
Serializableprotocol for your classes by having definedto_dict()andfrom_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 ofto_dict()method. Leaves the result dict unfit to be deserialized automatically viadeserialize();with use_serialization(): ...- opposite ofno_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 hasto_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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e74b8fc763bbb1a5b711859ee433ca5ec38ff360d3b6b0eac153a2697966df5e
|
|
| MD5 |
7eae119e7afbdaeb32b4515f7262d088
|
|
| BLAKE2b-256 |
1baf7d2d4e37c6701f03418ff8c679b11b8b90e22041c5501393fe30dfaa0947
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f77c0f5c7b84c5837738bfef6a21a9718e380ff7ec66f8d55bdddeb398e9a790
|
|
| MD5 |
0a185a19809fd5a1b29e5d4a0d0a12b8
|
|
| BLAKE2b-256 |
7f4e536c47fe0deec71cd450806b13cc8db3afb0eb73899a197c87c8a43981fe
|