msiemens / tinydb

TinyDB is a lightweight document oriented database optimized for your happiness :)
https://tinydb.readthedocs.org
MIT License
6.83k stars 549 forks source link

Cannot insert or update with storage=serialization #244

Closed joe307bad closed 6 years ago

joe307bad commented 6 years ago

I am having trouble inserting or updating while using serialization. Here is my code:

serialization = SerializationMiddleware(tinydb.storages.JSONStorage)
serialization.register_serializer(DateTimeSerializer(), 'TinyDate')
db = tinydb.TinyDB('db.json', storage=serialization)
db.insert({'int': 1, 'char': 'a'})

If I remove storage=serialization, it works fine. But if I leave it in, I get an error:

Exception has occurred: TypeError
isinstance() arg 2 must be a type or tuple of types
  File "C:\Projects\jbhs-scripts\roles\auto-download\files\src\main.py", line 52, in <module>
    db.insert({'int': 1, 'char': 'a'})

My serialization class is pretty simple. I don't think that's this issue but here is the code:

from datetime import datetime
from tinydb_serialization import Serializer
import arrow

class DateTimeSerializer(Serializer):
    OBJ_CLASS = arrow  # The class this serializer handles

    def encode(self, obj):
        return obj.isoformat().replace("+00:00", "Z")

    def decode(self, s):
        return arrow.get(s)

I am using Python version 3.6.0

joe307bad commented 6 years ago

Figured it out. My serlialization class was the issue.

I replaced OBJ_CLASS = arrow with OBJ_CLASS = arrow.Arrow and everything worked.

msiemens commented 6 years ago

Glad you found the issue @joe307bad!