Skip to content

Instantly share code, notes, and snippets.

@Dowwie
Created July 26, 2015 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dowwie/fa49c5decd62bd63ab5a to your computer and use it in GitHub Desktop.
Save Dowwie/fa49c5decd62bd63ab5a to your computer and use it in GitHub Desktop.
Marshmallow Full Example
from marshmallow import fields, Schema, pprint
from abc import ABCMeta, abstractmethod
class Serializable(metaclass=ABCMeta):
@classmethod
@abstractmethod
def serialization_schema(cls):
"""
Each serializable class must define its respective Schema (marshmallow)
and its make_object method.
:returns: a SerializationSchema class
"""
pass
def serialize(self):
schema = self.serialization_schema()()
return schema.dump(self)
@classmethod
def deserialize(cls, data):
schema = cls.serialization_schema()()
return schema.load(data=data).data
class Test1(Serializable):
def __init__(self):
self.a = 'A'
self.b = 'B'
self.c = 'C'
def __repr__(self):
return "Test1(a={0}, a={1}, a={2})".\
format(self.a, self.b, self.c)
@classmethod
def serialization_schema(self):
class SerializationSchema(Schema):
a = fields.Str()
b = fields.Str()
c = fields.Str()
def make_object(self, data):
cls = Test1
instance = cls.__new__(cls)
instance.__dict__.update(data)
return instance
return SerializationSchema
class Test2(Serializable):
def __init__(self):
self.d = 'D'
self.e = 'E'
self.f = 'F'
def __repr__(self):
return "Test2(d={0}, e={1}, f={2})".format(self.d, self.e, self.f)
@classmethod
def serialization_schema(self):
class SerializationSchema(Schema):
d = fields.Str()
e = fields.Str()
f = fields.Str()
def make_object(self, data):
cls = Test2
instance = cls.__new__(cls)
instance.__dict__.update(data)
return instance
return SerializationSchema
class Test3(Serializable):
def __init__(self, test1, test2):
self.test1 = test1
self.test2 = test2
self.g = 'G'
self.h = 'H'
self.i = 'I'
def __repr__(self):
return "Test3(test1={0}, test2={1}, g={2}, h={3}, i={4})".\
format(self.test1, self.test2, self.g, self.h, self.i)
@classmethod
def serialization_schema(self):
class SerializationSchema(Schema):
g = fields.Str()
h = fields.Str()
i = fields.Str()
test1 = fields.Nested(Test1.serialization_schema())
test2 = fields.Nested(Test2.serialization_schema())
def make_object(self, data):
cls = Test3
instance = cls.__new__(cls)
instance.__dict__.update(data)
return instance
return SerializationSchema
class Test4(Serializable):
def __init__(self, test3):
super().__init__()
self.test3 = test3
self.j = 'J'
self.k = 'K'
self.l = 'L'
def __repr__(self):
return "Test4(test3={0}, j={1}, k={2}, l={3})".format(
self.test3, self.j, self.k, self.l)
@classmethod
def serialization_schema(self):
class SerializationSchema(Schema):
j = fields.Str()
k = fields.Str()
l = fields.Str()
test3 = fields.Nested(Test3.serialization_schema())
def make_object(self, data):
cls = Test4
instance = cls.__new__(cls)
instance.__dict__.update(data)
return instance
return SerializationSchema
class Test5(Serializable):
def __init__(self):
self.w = 'W'
if __name__ == '__main__':
t3 = Test3(test1=Test1(), test2=Test2())
t4 = Test4(test3=t3)
result = t4.serialize()
expected_out =\
{'j': 'J',
'k': 'K',
'l': 'L',
'test3': {'g': 'G',
'h': 'H',
'i': 'I',
'test1': {'a': 'A', 'b': 'B', 'c': 'C'},
'test2': {'d': 'D', 'e': 'E', 'f': 'F'}}
}
print('Results: ')
pprint(result.data)
print('Expected Results: {result}'.format(result=expected_out == result.data))
new_t4 = Test4.deserialize(data=result.data)
print(new_t4)
# these raise an error:
# t5 = Test5()
# result = t5.serialize(t5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment