serpy

raw JSON →
0.3.1 verified Fri May 01 auth: no python maintenance

serpy is a fast, simple object serialization library for Python, inspired by Django REST Framework (DRF) serializers but with a focus on speed. Current version is 0.3.1, released in 2018, with no recent updates. It supports Python 2.7 to 3.6 and has a low release cadence.

pip install serpy
error AttributeError: module 'serpy' has no attribute 'Serializer'
cause Importing only `import serpy` without the class.
fix
Use from serpy import Serializer or import serpy; serpy.Serializer.
error TypeError: __init__() got an unexpected keyword argument 'instance'
cause Using keyword argument `obj` instead of `instance` (changed in 0.1.0).
fix
Use instance=... instead of obj=....
error KeyError: 'name'
cause Serializing a dictionary missing a key with `required=True` (default).
fix
Use required=False on the field, or ensure the key exists.
breaking Upgrading from 0.1.x to 0.2.0: If `required=False`, `None` values are now dropped from the output. Also, support for Python 2.6 is dropped.
fix Review serializers that rely on `None` values being present; set `required=True` if you want `None` values included.
breaking In version 0.3.0, behavior for `required=True` and `required=False` changed to match DRF. Previously, `required=False` might have dropped `None` values; now `None` values are included. Missing keys cause `KeyError` for `required=True`.
fix Adjust serializers relying on the old behavior: now `None` values are kept regardless of `required`, except when key is missing.
gotcha MethodField catches `AttributeError` and `KeyError` only if the method returns a value; if the method raises those errors for missing attributes, they are not caught and propagate.
fix Ensure your `MethodField` method does not rely on catching these exceptions implicitly; handle them inside the method.
deprecated The library is no longer actively maintained; last release was 2018. No support for modern Python (3.7+), including f-strings, type hints, or asyncio.
fix Consider using `marshmallow` or `pydantic` for ongoing support and modern Python.

Define a serializer class and serialize a dictionary or object instance.

import serpy

class PersonSerializer(serpy.Serializer):
    name = serpy.StrField()
    age = serpy.IntField()

person = {'name': 'Alice', 'age': 30}
serializer = PersonSerializer(instance=person)
result = serializer.data
print(result)  # {'name': 'Alice', 'age': 30}