Pickle dumps giving garbage value

1k Views Asked by At

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?

1

There are 1 best solutions below

0
On

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. An object_hook is used to recognize that format and converts the pickled object back into a Python object during json.loads:

from decimal import Decimal
import json
import pickle

class PythonObjectEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, (dict,list,tuple,str,unicode,int,long,float,bool,type(None))):
            return json.JSONEncoder.default(self,obj)
        return {'_python_object_':pickle.dumps(obj)}

def as_python_object(dct):
    if u'_python_object_' in dct:
        return pickle.loads(dct[u'_python_object_'])
    return dct

obj = {'a':1,'b':'string','c':1.2,'d':Decimal('123.456')}
print obj # original object
j = json.dumps(obj,cls=PythonObjectEncoder,indent=2)
print j  # encoded object
obj = json.loads(j,object_hook=as_python_object)
print obj # decoded object

Output:

{'a': 1, 'c': 1.2, 'b': 'string', 'd': Decimal('123.456')}
{
  "a": 1, 
  "c": 1.2, 
  "b": "string", 
  "d": {
    "_python_object_": "cdecimal\nDecimal\np0\n(S'123.456'\np1\ntp2\nRp3\n."
  }
}
{u'a': 1, u'c': 1.2, u'b': u'string', u'd': Decimal('123.456')}