Skip to main content

Serialization for objects that TinyDB otherwise couldn't handle

Project description

Build Status Coverage Version

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.1 (2015-11-17)

  • Fix installation via pip (see issue #1)

v1.0.0 (2015-09-27)

  • Initial release on PyPI

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

tinydb-serialization-1.0.1.zip (7.3 kB view details)

Uploaded Source

File details

Details for the file tinydb-serialization-1.0.1.zip.

File metadata

File hashes

Hashes for tinydb-serialization-1.0.1.zip
Algorithm Hash digest
SHA256 cfc71784079361ed45edcbcd65e81f0883e1e9cb884d617cbd4553c7625d382b
MD5 820c361495f83decefe26339cd119f76
BLAKE2b-256 2b991bb73fae9bfffccd5442005e497eb09922ddf067693625d8bed7871fb4b6

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