I am having issues with msgpack
in python. It seems that when serialising a dict
, if the keys are strings str
, they are not unserialised properly and causing KeyError
exceptions to be raised.
Example:
>>> import msgpack
>>> d = dict()
>>> value = 1234
>>> d['key'] = value
>>> binary = msgpack.dumps(d)
>>> new_d = msgpack.loads(binary)
>>> new_d['key']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'key'
This is because the keys are not strings after calling loads()
but are unserialised to bytes
objects.
>>> d.keys()
dict_keys(['key'])
>>> new_d.keys()
dict_keys([b'key'])
It seems this is related to a unimplemented feature as mentioned in github
My question is, Is there a way to fix this issue or a work around to ensure that the same keys can be used upon deserialisation?
I would like to use msgpack
but if I cannot build a dict
object with str
keys and expect to be able to use the same key upon deserilisation, it becomes useless.