A serializer/deserializer mechanism for simple and complex data structures
Project description
anserializer
A module for serializing and deserializing complex data structures to/from json. It allows the user to (de)serialize a complex dictionary/list structure in one go by defining serializers/deserializers for arbitrary sets of classes.
Tested with python3.
Serializer can be utilized either as instantiated or non-instantiated.
Install
pip3 install anserializer
Examples
Instantiated example
from anserializer import Serializer, DatetimeSerializer, ObjectSerializer
class MyObjectClass(object):
pass
# instantiate the serializer
s = s = Serializer([ DatetimeSerializer(), ObjectSerializer(MyObjectClass) ])
# create object
o = MyObjectClass()
print(o)
# serialize object
x = s.get_serialized(o)
print(x)
# deserialize object
o = s.get_deserialized(x)
print(o)
Non-instantiated example
from anserializer import Serializer, DatetimeSerializer, ObjectSerializer
class MyObjectClass(object):
pass
# put our list of serializer classes available for use into a variable
serializers = [ DatetimeSerializer(), ObjectSerializer(MyObjectClass) ]
# create object
o = MyObjectClass()
print(o)
# serialize object
x = Serializer.serialize(o, serializers)
print(x)
# deserialize object
o = Serializer.deserialize(x, serializers)
print(o)
Allow children to be serialized by a serializer defined for their ancestor
from anserializer import Serializer, DatetimeSerializer, ObjectSerializer
class MyObjectClass(object):
pass
# instantiate the serializer
s = s = Serializer([ DatetimeSerializer(), ObjectSerializer(object) ], serialize_children=True)
# create object
o = MyObjectClass()
print(o)
# serialize object
x = s.get_serialized(o)
print(x)
Use your own serializer
from anserializer import Serializer, DatetimeSerializer, ObjectSerializer, BaseSerializer
class MyObjectClass(object):
pass
# create your serializer
class MySerializer(BaseSerializer):
def __init__(self):
super().__init__([MyObjectClass], '^!MyObjectClass\(\)$')
def serialize(self, obj):
# do the magic and return a serialized element
return { '!MyObjectClass()': {
# insert object data here
}
}
def deserialize(self, serialized_obj):
# do the magic and return an object with the data given in serialized format
kwargs = {}
return MyObjectClass(**kwargs)
# instantiate the serializer
s = Serializer([ DatetimeSerializer(), MySerializer(), ObjectSerializer(object) ])
# create object
o = MyObjectClass()
print(o)
# serialize object
x = s.get_serialized(o)
print(x)
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
anserializer-1.2.2.tar.gz
(7.1 kB
view details)
Built Distribution
File details
Details for the file anserializer-1.2.2.tar.gz
.
File metadata
- Download URL: anserializer-1.2.2.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eed8c351a3f683b18915dac24436c7356ab59b14578c791f7461fb238c9e29e |
|
MD5 | 7417f0ef75a602dadb591a4771efd51a |
|
BLAKE2b-256 | 32a3c69d6097e8592a5ca5811dc5e10cd54551d08518b18e2ddc058370fb0574 |
File details
Details for the file anserializer-1.2.2-py3-none-any.whl
.
File metadata
- Download URL: anserializer-1.2.2-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 057b8f07a6d17c5d180c32b43cddfaba140ac1b44524054029a8731182fc6025 |
|
MD5 | 462b327d8b36085649c573cbc27ab9f7 |
|
BLAKE2b-256 | 5c2103f2cc1e03e5530639ba6341fbd162899a724cba281c2dd73297a2f20ec8 |