I'm using python to serialize a python object to store it in my cache. For this serialization, I'm using json.dumps()
and to unserialize it after I get it out of the cache, I'm using json.loads()
. I assumed this roundtrip would work without any trouble. But as you can see below it fails.
>>> import json
>>> from collections import namedtuple
>>> x = {"hello": 1, "goodbye": 2}
>>> y = namedtuple('Struct', x.keys())(*x.values())
>>> y
Struct(goodbye=2, hello=1)
>>> json.loads(json.dumps(y))
[2, 1] # <= I expected this to be the same value as y above!!
Why is this json.dumps/loads roundtrip lossy? What function can I use to serialize this object to that unserialization will preserve its original value? I tried to use pickle
but it fails to serialize the object.