patx / pickledb

pickleDB is an open source key-value store using Python's json module.
https://patx.github.io/pickledb
BSD 3-Clause "New" or "Revised" License
925 stars 125 forks source link

Problem with reloading an already created db. TypeError: the JSON object must be str, not 'bytes' #45

Closed mianchd closed 5 years ago

mianchd commented 5 years ago
import pickledb
db = pickledb.load('example.db', False)
db.set('key', 'value')
db.dump()
db = pickledb.load('example.db', False)

Traceback (most recent call last): File "", line 1, in File "C:\Users\Documents\Python Projects\garbage-chatter\venv\lib\site-packages\pickledb.py", line 39, in load return pickledb(location, auto_dump) File "C:\Users\Documents\Python Projects\garbage-chatter\venv\lib\site-packages\pickledb.py", line 49, in init self.load(location, auto_dump) File "C:\Users\Documents\Python Projects\garbage-chatter\venv\lib\site-packages\pickledb.py", line 79, in load self._loaddb() File "C:\Users\Documents\Python Projects\garbage-chatter\venv\lib\site-packages\pickledb.py", line 96, in _loaddb self.db = json.load(open(self.loco, 'rb')) File "C:\Users\AppData\Local\Programs\Python\Python35\Lib\json__init.py", line 268, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "C:\Users\AppData\Local\Programs\Python\Python35\Lib\json\init.py", line 312, in loads s.class.name__)) TypeError: the JSON object must be str, not 'bytes'

patx commented 5 years ago

What version of Python and pickleDB are you using? I was not able to recreate this in my own environment. I recomend using the latest version of pickleDB from the GitHub repo. The PyPI is a little behind.

mianchd commented 5 years ago

I am using,

pickleDB==0.8.1 Python==3.5.4

patx commented 5 years ago

I still am unable to re-create the error.

I am using Python 3.7 and also tested Python 2.7 along with pickleDB 0.9.1 which I just made available on PyPI. Please try using the latest pickleDB release and let me know if the error persists.

Once you have upgraded to 0.9.1 if the error is still happening you may want to change

self.db = json.load(open(self.loco, 'rb'))

to

self.db = json.load(open(self.loco, 'rt'))

and see if that fixes your problem. Please let me know the results.

patx commented 5 years ago

see https://github.com/patx/pickledb/commit/096761b233eb197aff33f9d2180c2ef1298d3aa8#diff-6ca8dfc863023bccd48ff0b1d882dc6b

patx commented 5 years ago

upgrade to 0.9.2 now available on PyPI

mianchd commented 5 years ago

Thanks @patx The issue is resolved.