Fast serialization of dataclasses using Cython
pip install serpyco==1.3.13
Serpyco is a serialization library for Python 3.6+ dataclasses that works just by defining your dataclasses:
import dataclasses
import typing
import serpyco
@dataclasses.dataclass
class Example(object):
name: str
num: int
tags: typing.List[str]
serializer = serpyco.Serializer(Example)
result = serializer.dump(Example(name="foo", num=2, tags=["hello", "world"]))
print(result)
{'name': 'foo', 'num': 2, 'tags': ['hello', 'world']}
Serpyco works by analysing the dataclass fields and can recognize many types : List, Set, Tuple, Optional, Union... You can also embed other dataclasses in a definition.
The main use-case for Serpyco is to serialize objects for an API, but it can be helpful whenever you need to transform objects to/from builtin Python types.
Serpyco is best installed via pip:
pip install serpyco
It has only 2 (3 with python 3.6 dataclasses backport) dependencies:
Serpyco is written using Python and Cython for parts needing speed.