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

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.storages import JSONStorage
>>> 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)}]

Important: A SerializationMiddleware instance always wraps a database’s storage. This means that the SerializationMiddleware instance cannot be shared between multiple TinyDB instances as they would use the same underlying storage instance and thus share all data between both instances.

Provided Serializers

  • tinydb_serialization.serializers.DateTimeSerializer: serializes datetime objects as ISO 8601 formatted strings

  • tinydb_serialization.serializers.DateSerializer: serializes date 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.2.0 (2024-10-05)

  • Include the DateSerializer in this package (see issue #16)

v2.1.1 (2023-07-24)

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)

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)

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

Uploaded Source

Built Distribution

tinydb_serialization-2.2.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file tinydb_serialization-2.2.0.tar.gz.

File metadata

  • Download URL: tinydb_serialization-2.2.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Linux/6.8.0-1014-azure

File hashes

Hashes for tinydb_serialization-2.2.0.tar.gz
Algorithm Hash digest
SHA256 b7e7f2b0cc17350438ba070eeb2f5fcb3a9949c5ff38476379450422de3f935f
MD5 6e0d7d6fe0685b8460c6b080a3ed37de
BLAKE2b-256 87afd6bc960eadee5fb4406eaeceec4b7be6ef82676a5f3cf638fd56ecf39448

See more details on using hashes here.

File details

Details for the file tinydb_serialization-2.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tinydb_serialization-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 02edeafd6560b811c8df3e39792a37dee68ac63978659c214eab593316c58253
MD5 406fc3f9c349f04761816bb9a828db6c
BLAKE2b-256 96cd7e44162c9d3288517b7f19d13932112b6ecef079eec28f8d23997ebbe2c6

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