Hi I am using a JSON Encoder, where pickle.dumps() is giving me weird output. The output is coming as:
"cdecimal Decimal p0 (S'2097369' p1 tp2 Rp3 .",
While, it should be: 2097369
The code snippet is:
class PythonObjectEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, (list, dict, unicode, int, float, str, bool, type(None))):
return JSONEncoder.default(self, obj)
return pickle.dumps(obj)
def as_python_object(dct):
if '_python_object' in dct:
return pickle.loads('')
return dct
Can anyone tell me what is going wrong and how can I get back the desired value?
I think this is what you are looking for. Types not supported by JSON are serialized to string using
pickle
and stored with a format to indicate it is a Python object. Anobject_hook
is used to recognize that format and converts the pickled object back into a Python object duringjson.loads
:Output: