tinydb-serialization


Nametinydb-serialization JSON
Version 2.2.0 PyPI version JSON
download
home_pageNone
SummarySerialization for objects that TinyDB otherwise couldn't handle
upload_time2024-10-05 18:50:19
maintainerNone
docs_urlNone
authorMarkus Siemens
requires_python<4.0,>=3.8
licenseMIT
keywords database nosql
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            tinydb-serialization
^^^^^^^^^^^^^^^^^^^^

|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:

.. code-block:: python

    >>> 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):

.. code-block:: python

    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 <https://github.com/msiemens/tinydb-serialization/pull/16>`_)

**v2.1.1** (2023-07-24)
-----------------------

- Fix VSCode Pylance type error (see `pull request #14 <https://github.com/msiemens/tinydb-serialization/pull/14>`_)
- Add support for storages that encode as binary strings (see `pull request #17 <https://github.com/msiemens/tinydb-serialization/pull/17>`_)
- Drop Python 3.7 support

**v2.1.0** (2021-01-23)
-----------------------

- Include the ``DateTimeSerializer`` in this package (see `issue #10 <https://github.com/msiemens/tinydb-serialization/pull/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 <https://github.com/msiemens/tinydb-serialization/pull/9>`_)

**v1.0.4** (2017-03-27)
-----------------------

- Don't modify the original element if it contains a list (see
  `pull request #5 <https://github.com/msiemens/tinydb-serialization/pull/5>`_)

**v1.0.3** (2016-02-11)
-----------------------

- Handle nested data (nested dicts, lists) properly when serializing/deserializing (see
  `pull request #3 <https://github.com/msiemens/tinydb-serialization/pull/3>`_)

**v1.0.2** (2016-01-04)
-----------------------

- Don't destroy original data when serializing (see
  `pull request #2 <https://github.com/msiemens/tinydb-serialization/pull/2>`_)

**v1.0.1** (2015-11-17)
-----------------------

- Fix installation via pip (see `issue #1 <https://github.com/msiemens/tinydb-serialization/issues/1>`_)

**v1.0.0** (2015-09-27)
-----------------------

- Initial release on PyPI

.. |Build Status| image:: https://img.shields.io/github/workflow/status/msiemens/tinydb-serialization/Python%20CI?style=flat-square
   :target: https://github.com/msiemens/tinydb-serialization/actions?query=workflow%3A%22Python+CI%22
.. |Coverage| image:: https://img.shields.io/coveralls/msiemens/tinydb-serialization.svg?style=flat-square
   :target: https://coveralls.io/r/msiemens/tinydb-serialization
.. |Version| image:: https://img.shields.io/pypi/v/tinydb-serialization.svg?style=flat-square
   :target: https://pypi.python.org/pypi/tinydb-serialization/

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tinydb-serialization",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": "database, nosql",
    "author": "Markus Siemens",
    "author_email": "markus@m-siemens.de",
    "download_url": "https://files.pythonhosted.org/packages/87/af/d6bc960eadee5fb4406eaeceec4b7be6ef82676a5f3cf638fd56ecf39448/tinydb_serialization-2.2.0.tar.gz",
    "platform": null,
    "description": "tinydb-serialization\n^^^^^^^^^^^^^^^^^^^^\n\n|Build Status| |Coverage| |Version|\n\n``tinydb-serialization`` provides serialization for objects that TinyDB\notherwise couldn't handle.\n\nUsage\n*****\n\nGeneral Usage\n-------------\n\nTo use a serializer, create a ``SerializationMiddleware`` instance with\nthe storage class you want to use and register the serializers you want\nto use. Then you pass the middleware instance as the storage to TinyDB:\n\n.. code-block:: python\n\n    >>> from tinydb import TinyDB, Query\n    >>> from tinydb.storages import JSONStorage\n    >>> from tinydb_serialization import SerializationMiddleware\n    >>> from tinydb_serialization.serializers import DateTimeSerializer\n    >>>\n    >>> from datetime import datetime\n    >>>\n    >>> serialization = SerializationMiddleware(JSONStorage)\n    >>> serialization.register_serializer(DateTimeSerializer(), 'TinyDate')\n    >>>\n    >>> db = TinyDB('db.json', storage=serialization)\n    >>> db.insert({'date': datetime(2000, 1, 1, 12, 0, 0)})\n    >>> db.all()\n    [{'date': datetime.datetime(2000, 1, 1, 12, 0)}]\n    >>> query = Query()\n    >>> db.insert({'date': datetime(2010, 1, 1, 12, 0, 0)})\n    >>> db.search(query.date > datetime(2005, 1, 1))\n    [{'date': datetime.datetime(2010, 1, 1, 12, 0)}]\n\n\n**Important:** A ``SerializationMiddleware`` instance always wraps a database's storage.\nThis means that the ``SerializationMiddleware`` instance **cannot** be shared\nbetween multiple ``TinyDB`` instances as they would use the same underlying storage\ninstance and thus share all data between both instances.\n\nProvided Serializers\n--------------------\n\n- ``tinydb_serialization.serializers.DateTimeSerializer``: serializes ``datetime`` objects\n  as ISO 8601 formatted strings\n- ``tinydb_serialization.serializers.DateSerializer``: serializes ``date`` objects\n  as ISO 8601 formatted strings\n\nCreating Custom Serializers\n---------------------------\n\nIn this example we implement a serializer for ``datetime`` objects (like the one provided\nby this package):\n\n.. code-block:: python\n\n    from datetime import datetime\n    from tinydb_serialization import Serializer\n\n    class DateTimeSerializer(Serializer):\n        OBJ_CLASS = datetime  # The class this serializer handles\n\n        def encode(self, obj):\n            return obj.isoformat()\n\n        def decode(self, s):\n            return datetime.fromisoformat(s)\n\n\nChangelog\n*********\n\n**v2.2.0** (2024-10-05)\n-----------------------\n\n- Include the ``DateSerializer`` in this package (see `issue #16 <https://github.com/msiemens/tinydb-serialization/pull/16>`_)\n\n**v2.1.1** (2023-07-24)\n-----------------------\n\n- Fix VSCode Pylance type error (see `pull request #14 <https://github.com/msiemens/tinydb-serialization/pull/14>`_)\n- Add support for storages that encode as binary strings (see `pull request #17 <https://github.com/msiemens/tinydb-serialization/pull/17>`_)\n- Drop Python 3.7 support\n\n**v2.1.0** (2021-01-23)\n-----------------------\n\n- Include the ``DateTimeSerializer`` in this package (see `issue #10 <https://github.com/msiemens/tinydb-serialization/pull/10>`_)\n- Drop Python 3.6 support (as 3.7 is needed for date parsing)\n\n**v2.0.0** (2020-05-26)\n-----------------------\n\n- Add TinyDB v4.0.0 support (see `pull request #9 <https://github.com/msiemens/tinydb-serialization/pull/9>`_)\n\n**v1.0.4** (2017-03-27)\n-----------------------\n\n- Don't modify the original element if it contains a list (see\n  `pull request #5 <https://github.com/msiemens/tinydb-serialization/pull/5>`_)\n\n**v1.0.3** (2016-02-11)\n-----------------------\n\n- Handle nested data (nested dicts, lists) properly when serializing/deserializing (see\n  `pull request #3 <https://github.com/msiemens/tinydb-serialization/pull/3>`_)\n\n**v1.0.2** (2016-01-04)\n-----------------------\n\n- Don't destroy original data when serializing (see\n  `pull request #2 <https://github.com/msiemens/tinydb-serialization/pull/2>`_)\n\n**v1.0.1** (2015-11-17)\n-----------------------\n\n- Fix installation via pip (see `issue #1 <https://github.com/msiemens/tinydb-serialization/issues/1>`_)\n\n**v1.0.0** (2015-09-27)\n-----------------------\n\n- Initial release on PyPI\n\n.. |Build Status| image:: https://img.shields.io/github/workflow/status/msiemens/tinydb-serialization/Python%20CI?style=flat-square\n   :target: https://github.com/msiemens/tinydb-serialization/actions?query=workflow%3A%22Python+CI%22\n.. |Coverage| image:: https://img.shields.io/coveralls/msiemens/tinydb-serialization.svg?style=flat-square\n   :target: https://coveralls.io/r/msiemens/tinydb-serialization\n.. |Version| image:: https://img.shields.io/pypi/v/tinydb-serialization.svg?style=flat-square\n   :target: https://pypi.python.org/pypi/tinydb-serialization/\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Serialization for objects that TinyDB otherwise couldn't handle",
    "version": "2.2.0",
    "project_urls": null,
    "split_keywords": [
        "database",
        " nosql"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "96cd7e44162c9d3288517b7f19d13932112b6ecef079eec28f8d23997ebbe2c6",
                "md5": "406fc3f9c349f04761816bb9a828db6c",
                "sha256": "02edeafd6560b811c8df3e39792a37dee68ac63978659c214eab593316c58253"
            },
            "downloads": -1,
            "filename": "tinydb_serialization-2.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "406fc3f9c349f04761816bb9a828db6c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 5736,
            "upload_time": "2024-10-05T18:50:18",
            "upload_time_iso_8601": "2024-10-05T18:50:18.206347Z",
            "url": "https://files.pythonhosted.org/packages/96/cd/7e44162c9d3288517b7f19d13932112b6ecef079eec28f8d23997ebbe2c6/tinydb_serialization-2.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "87afd6bc960eadee5fb4406eaeceec4b7be6ef82676a5f3cf638fd56ecf39448",
                "md5": "6e0d7d6fe0685b8460c6b080a3ed37de",
                "sha256": "b7e7f2b0cc17350438ba070eeb2f5fcb3a9949c5ff38476379450422de3f935f"
            },
            "downloads": -1,
            "filename": "tinydb_serialization-2.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6e0d7d6fe0685b8460c6b080a3ed37de",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 4912,
            "upload_time": "2024-10-05T18:50:19",
            "upload_time_iso_8601": "2024-10-05T18:50:19.813926Z",
            "url": "https://files.pythonhosted.org/packages/87/af/d6bc960eadee5fb4406eaeceec4b7be6ef82676a5f3cf638fd56ecf39448/tinydb_serialization-2.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-05 18:50:19",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "tinydb-serialization"
}
        
Elapsed time: 0.37334s