serpy is a super simple object serialization framework built for speed. serpy serializes complex datatypes (Django Models, custom classes, ...) to simple native types (dicts, lists, strings, ...). The native types can easily be converted to JSON or any other format needed.
The goal of serpy is to be able to do this simply, reliably, and quickly. Since serializers are class based, they can be combined, extended and customized with very little code duplication. Compared to other popular Python serialization frameworks like marshmallow or Django Rest Framework Serializersserpy is at least an order of magnitude faster.
Source at: https://github.com/clarkduvall/serpy
If you want a feature, send a pull request!
Full documentation at: http://serpy.readthedocs.org/en/latest/
$ pip install serpyimportserpyclassFoo(object):
"""The object to be serialized."""y='hello'z=9.5def__init__(self, x):
self.x=xclassFooSerializer(serpy.Serializer):
"""The serializer schema definition."""# Use a Field subclass like IntField if you need more validation.x=serpy.IntField()
y=serpy.Field()
z=serpy.Field()
f=Foo(1)
FooSerializer(f).representation# {'x': 1, 'y': 'hello', 'z': 9.5}fs= [Foo(i) foriinrange(100)]
FooSerializer(fs, many=True).representation# [{'x': 0, 'y': 'hello', 'z': 9.5}, {'x': 1, 'y': 'hello', 'z': 9.5}, ...]importserpyclassNestee(object):
"""An object nested inside another object."""n='hi'classFoo(object):
x=1nested=Nestee()
classNesteeSerializer(serpy.Serializer):
n=serpy.Field()
classFooSerializer(serpy.Serializer):
x=serpy.Field()
# Use another serializer as a field.nested=NesteeSerializer()
f=Foo()
FooSerializer(f).representation# {'x': 1, 'nested': {'n': 'hi'}}importserpyclassFoo(object):
y=1z=2super_long_thing=10defx(self):
return5classFooSerializer(serpy.Serializer):
w=serpy.Field(attr='super_long_thing')
x=serpy.Field(call=True)
plus=serpy.MethodField()
defget_plus(self, obj):
returnobj.y+obj.zf=Foo()
FooSerializer(f).representation# {'w': 10, 'x': 5, 'plus': 3}importserpyclassFoo(object):
a=1b=2classASerializer(serpy.Serializer):
a=serpy.Field()
classABSerializer(ASerializer):
"""ABSerializer inherits the 'a' field from ASerializer. This also works with multiple inheritance and mixins. """b=serpy.Field()
f=Foo()
ASerializer(f).representation# {'a': 1}ABSerializer(f).representation# {'a': 1, 'b': 2}serpy is free software distributed under the terms of the MIT license. See the LICENSE file.