Skip to main content

Fast and flexible object mapping (serialization, marshalling)

Project description

.. sourcecode :: python

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',
'first_name': 'John',
'last_name': 'Doe'
}

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.

.. _serpy: https://pypi-hypernode.com/pypi/serpy
.. _marshmallow: https://pypi-hypernode.com/pypi/marshmallow/
.. _Kim: https://pypi-hypernode.com/pypi/py-kim/1.2.0
.. _`Django REST Framework`: https://pypi-hypernode.com/pypi/djangorestframework

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.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

lion-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file lion-0.1.0.tar.gz.

File metadata

  • Download URL: lion-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lion-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2424df3b1553af964b9c1050d81b3358d426fcf139f22130cf7c9ede10b4fad5
MD5 390c6054e6bf2723e5ef3d3c13f6526c
BLAKE2b-256 85fb4bd9f32852f78bf8b49f68d6e95866512be40dff260ad2d837d450af0790

See more details on using hashes here.

File details

Details for the file lion-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lion-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dcd8b08c00fa2ac25c153dbd407aa14fca55d39111bb8d60d01e02841ddc1544
MD5 c09bf384223ea700a031def5c8d768d6
BLAKE2b-256 e304c6919dc30b8d8dc044b4fd9434423dfa31e91bcf84b75f3737b1b93f2aa0

See more details on using hashes here.

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