Skip to main content

Fast and flexible object mapping (serialization, marshalling)

Project description

Lion is a very flexible yet fast library for mapping objects to dictionaries. It uses a declarative API and supports a query language similar to GraphQL. Unlike other serialization libraries it also allows to skip entire fields instead of having a null value in the dictionary.

It is inspired by libraries like serpy, marshmallow, Django REST Framework and Kim.

Example:

import lion

class UserMapper(lion.Mapper):
    id = lion.UUIDField()
    email = lion.StrField(condition=lion.skip_empty)
    first_name = lion.StrField()
    last_name = lion.StrField()

user = User(
    id=UUID('ad94d0e8-2526-4d9b-ad76-0fbffcf41033'),
    email='john.doe@example.com',
    first_name='John',
    last_name='Doe'
)

# Dump all fields to a dictionary
assert UserMapper().dump(user) == {
    'id': 'ad94d0e8-2526-4d9b-ad76-0fbffcf41033',
    'email': 'john.doe@example.com',
    'first_name': 'John',
    'last_name': 'Doe'
}

# Dump a subset of fields
assert UserMapper('{id,email}').dump(user) == {
    'id': 'ad94d0e8-2526-4d9b-ad76-0fbffcf41033',
    'email': 'john.doe@example.com'
}

The query language also supports subfields:

UserMapper('{id,email,groups{id,name}}).dump(user)

The performance is somewhat slower than serpy but still far ahead of marshmallow and Django REST Framework

Right now only dumping (aka. serialization, marshalling) is supported but future versions will also introduce loading (aka. deserialization, unmarshalling) of data.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lion-0.3.0.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

lion-0.3.0-py3-none-any.whl (7.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page