Fast and flexible object mapping (serialization, marshalling)
Project description
Lion
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 GroupMapper(lion.Mapper):
id = lion.UUIDField()
name = lion.StrField()
class UserMapper(lion.Mapper):
id = lion.UUIDField()
email = lion.StrField(condition=lion.skip_empty)
first_name = lion.StrField()
last_name = lion.StrField()
groups = lion.ListField(GroupMapper)
user = User(
id=UUID('ad94d0e8-2526-4d9b-ad76-0fbffcf41033'),
email='john.doe@example.com',
first_name='John',
last_name='Doe',
groups=[
Group(
id=UUID('95a326fc-32e5-4d9b-a385-1ea1257d98da'),
name='Awesome people'
)
]
)
# 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',
'groups': [
{
'id': '95a326fc-32e5-4d9b-a385-1ea1257d98da',
'name': 'Awesome people'
}
]
}
# Load user object from a dictionary
assert user == UserMapper().load({
'id': 'ad94d0e8-2526-4d9b-ad76-0fbffcf41033',
'email': 'john.doe@example.com',
'first_name': 'John',
'last_name': 'Doe',
'groups': [
{
'id': '95a326fc-32e5-4d9b-a385-1ea1257d98da',
'name': 'Awesome people'
}
]
})
Query language
By using the GraphQL-like query language it is possible to dump and load only parts of a given structure:
# Dump a subset of fields
assert UserMapper('{id,email}').dump(user) == {
'id': 'ad94d0e8-2526-4d9b-ad76-0fbffcf41033',
'email': 'john.doe@example.com'
}
# Dump subset of a nested mapper
assert UserMapper('{id,email,groups{id}}').dump(user) == {
'id': 'ad94d0e8-2526-4d9b-ad76-0fbffcf41033',
'email': 'john.doe@example.com',
'groups': [
'id': '95a326fc-32e5-4d9b-a385-1ea1257d98da'
]
}
Performance
The performance is somewhat slower than serpy but still far ahead of marshmallow and Django REST Framework.
Caveats
Lion also supports loading (serialization/marshalling) of data but currently does not perform any kind of validation. This is not a big deal if using Lion as part of a project which uses something like connexion which already performs validation using the provided OpenAPI specification file. Just be warned that loading an unvalidated data structure using Lion might result in somewhat weird looking 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
Built Distribution
File details
Details for the file lion-0.4.1.tar.gz
.
File metadata
- Download URL: lion-0.4.1.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.3.0-51-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d42ab996dc3f1bb81d77195c4c0fc45c88e9fa48613c499fcab945910688a896 |
|
MD5 | db8ef80b456ada63af0dc582920b4e49 |
|
BLAKE2b-256 | 024777cb6ac3f0d53d55052c6a7f27c06da41f7bf986a898e68e42e11778e6d4 |
File details
Details for the file lion-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: lion-0.4.1-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.3.0-51-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a1adb21e3d6cd4a6de12109101b9b50b2035e9aa63c6327272c9986751ae7b7 |
|
MD5 | f013ea5d324af305f8536aa12a1ccc3b |
|
BLAKE2b-256 | 3101c534f1a32fa487079a5bebf7c0caf092cac1b69ff111255577589a09b6e0 |