Serialization for objects that TinyDB otherwise couldn't handle
Project description
tinydb-serialization provides serialization for objects that TinyDB otherwise couldn’t handle.
Usage
General Usage
To use a serializer, create a SerializationMiddleware instance with the storage class you want to use and register the serializers you want to use. Then you pass the middleware instance as the storage to TinyDB:
>>> from tinydb import TinyDB, Query
>>> from tinydb_serialization import SerializationMiddleware
>>> from tinydb_serialization.serializers import DateTimeSerializer
>>>
>>> from datetime import datetime
>>>
>>> serialization = SerializationMiddleware(JSONStorage)
>>> serialization.register_serializer(DateTimeSerializer(), 'TinyDate')
>>>
>>> db = TinyDB('db.json', storage=serialization)
>>> db.insert({'date': datetime(2000, 1, 1, 12, 0, 0)})
>>> db.all()
[{'date': datetime.datetime(2000, 1, 1, 12, 0)}]
>>> query = Query()
>>> db.insert({'date': datetime(2010, 1, 1, 12, 0, 0)})
>>> db.search(query.date > datetime(2005, 1, 1))
[{'date': datetime.datetime(2010, 1, 1, 12, 0)}]
Provided Serializers
tinydb_serialization.serializers.DateTimeSerializer: serializes datetime objects as ISO 8601 formatted strings
Creating Custom Serializers
In this example we implement a serializer for datetime objects (like the one provided by this package):
from datetime import datetime
from tinydb_serialization import Serializer
class DateTimeSerializer(Serializer):
OBJ_CLASS = datetime # The class this serializer handles
def encode(self, obj):
return obj.isoformat()
def decode(self, s):
return datetime.fromisoformat(s)
Changelog
v2.1.0 (2021-01-23)
Include the DateTimeSerializer in this package (see issue #10)
Drop Python 3.6 support (as 3.7 is needed for date parsing)
v2.0.0 (2020-05-26)
Add TinyDB v4.0.0 support (see pull request #9)
v1.0.4 (2017-03-27)
Don’t modify the original element if it contains a list (see pull request #5)
v1.0.3 (2016-02-11)
Handle nested data (nested dicts, lists) properly when serializing/deserializing (see pull request #3)
v1.0.2 (2016-01-04)
Don’t destroy original data when serializing (see pull request #2)
v1.0.1 (2015-11-17)
Fix installation via pip (see issue #1)
v1.0.0 (2015-09-27)
Initial release on PyPI
Project details
Release history Release notifications | RSS feed
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 tinydb-serialization-2.1.0.tar.gz
.
File metadata
- Download URL: tinydb-serialization-2.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13365c8a1a11452c925b160a6ea3996ac41dc42c37a80ec9fc8d8ee0609992e8 |
|
MD5 | 1124119c46054c3fd06d19d422efd49a |
|
BLAKE2b-256 | d0c0460c28fa5f8750db57b282706146b3e8efec749e90627aa3cb2860543763 |
File details
Details for the file tinydb_serialization-2.1.0-py3-none-any.whl
.
File metadata
- Download URL: tinydb_serialization-2.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cfd0824a6afd0a56cb7d6b3c09807981d38c349500ba239d651a5b4d2f906f5c |
|
MD5 | 6dd70afbdf11d1b293af3de570a0dc01 |
|
BLAKE2b-256 | 6d38d56d90e12162c2762747149b8f21943f4c345b1db4f19d48c269b30f772e |