Serialization for objects that TinyDB otherwise couldn't handle
Project description
tinydb-serialization provides serialization for objects that TinyDB otherwise couldn’t handle.
Usage
Creating a Serializer
In this example we implement a serializer for datetime objects:
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.strftime('%Y-%m-%dT%H:%M:%S')
def decode(self, s):
return datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')
Using a Serializer
You can use your serializer like this:
>>> from tinydb.storages import JSONStorage
>>> from tinydb_serialization import SerializationMiddleware
>>>
>>> serialization = SerializationMiddleware()
>>> 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)}]
Changelog
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
File details
Details for the file tinydb-serialization-1.0.0.zip
.
File metadata
- Download URL: tinydb-serialization-1.0.0.zip
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32a28fa984ba2cd69a3503bb62cd293206fa8fc1700e3695b375952296f1f8da |
|
MD5 | a68af449b6ea36d39c2b550c9ec8864c |
|
BLAKE2b-256 | 268f864b234a5cd3fe5daaebd2dd9b83c9c7e32dbb90ca18fbc4e81eb9629ba2 |