Skip to main content

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 Serializers serpy is at least an order of magnitude faster.

Source

Source at: https://github.com/clarkduvall/serpy

If you want a feature, send a pull request!

Documentation

Full documentation at: http://serpy.readthedocs.org/en/latest/

Installation

$ pip install serpy

Examples

Simple Example

import serpy

class Foo(object):
    """The object to be serialized."""
    y = 'hello'
    z = 9.5

    def __init__(self, x):
        self.x = x


class FooSerializer(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).data
# {'x': 1, 'y': 'hello', 'z': 9.5}

fs = [Foo(i) for i in range(100)]
FooSerializer(fs, many=True).data
# [{'x': 0, 'y': 'hello', 'z': 9.5}, {'x': 1, 'y': 'hello', 'z': 9.5}, ...]

Nested Example

import serpy

class Nestee(object):
    """An object nested inside another object."""
    n = 'hi'


class Foo(object):
    x = 1
    nested = Nestee()


class NesteeSerializer(serpy.Serializer):
    n = serpy.Field()


class FooSerializer(serpy.Serializer):
    x = serpy.Field()
    # Use another serializer as a field.
    nested = NesteeSerializer()

f = Foo()
FooSerializer(f).data
# {'x': 1, 'nested': {'n': 'hi'}}

Complex Example

import serpy

class Foo(object):
    y = 1
    z = 2
    super_long_thing = 10

    def x(self):
        return 5


class FooSerializer(serpy.Serializer):
    w = serpy.Field(attr='super_long_thing')
    x = serpy.Field(call=True)
    plus = serpy.MethodField()

    def get_plus(self, obj):
        return obj.y + obj.z

f = Foo()
FooSerializer(f).data
# {'w': 10, 'x': 5, 'plus': 3}

Inheritance Example

import serpy

class Foo(object):
    a = 1
    b = 2


class ASerializer(serpy.Serializer):
    a = serpy.Field()


class ABSerializer(ASerializer):
    """ABSerializer inherits the 'a' field from ASerializer.

    This also works with multiple inheritance and mixins.
    """
    b = serpy.Field()

f = Foo()
ASerializer(f).data
# {'a': 1}
ABSerializer(f).data
# {'a': 1, 'b': 2}

License

serpy is free software distributed under the terms of the MIT license. See the LICENSE file.

Release files for serpy 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for serpy 0.3.1
File Size Uploaded
serpy-0.3.1.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for serpy 0.3.1
File Interpreter ABI Platform
serpy-0.3.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 16.4 kB

Release files / serpy-0.3.1.tar.gz

Download URL serpy-0.3.1.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3772b2a9923fbf674000ff51abebf6ea8f0fca0a2cfcbfa0d63ff118193d1ec5
BLAKE2b-256 checksum
How to use checksums
66254cbf8f523989adbbaa6b8e560867789f0bdf493882402d8fc0a4bf4209ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / serpy-0.3.1-py2.py3-none-any.whl

Download URL serpy-0.3.1-py2.py3-none-any.whl
Size 9.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
750ded3df0671918b81d6efcab2b85cac12f9fcc2bce496c24a0ffa65d84b5da
BLAKE2b-256 checksum
How to use checksums
62818e9efbb206bb6cd79e515ea54365c8cac0cdfffefb073b51b2b7f9da46fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page