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 hashes)
Built Distribution
Close
Hashes for anserializer-1.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 057b8f07a6d17c5d180c32b43cddfaba140ac1b44524054029a8731182fc6025 |
|
MD5 | 462b327d8b36085649c573cbc27ab9f7 |
|
BLAKE2b-256 | 5c2103f2cc1e03e5530639ba6341fbd162899a724cba281c2dd73297a2f20ec8 |