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.1.0.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for anserializer-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5acbf1348e152ea8de6962c9bb6a0b89f610f53be13fd61c1afbe5c6d29f9470 |
|
MD5 | 164ac4d7b19778f74e97d65255c8581a |
|
BLAKE2b-256 | 9d7dbeba35bec438db4204f7a0ae140ad8d0d56dfe9fd1d0b864624b28a3a833 |