2018-05-28 07:18:18 -04:00
|
|
|
import msgpack
|
2018-06-08 00:46:41 -04:00
|
|
|
import pickle
|
2018-05-28 07:18:18 -04:00
|
|
|
|
|
|
|
from django_redis.serializers.base import BaseSerializer
|
|
|
|
|
|
|
|
|
2018-06-08 00:46:41 -04:00
|
|
|
def default(obj):
|
|
|
|
# Pickle anything that MessagePack can't handle itself.
|
|
|
|
return msgpack.ExtType(69, pickle.dumps(obj, protocol=4))
|
|
|
|
|
|
|
|
|
|
|
|
def ext_hook(code, data):
|
|
|
|
# Unpickle if we pickled - otherwise do nothing.
|
|
|
|
if code == 69:
|
|
|
|
return pickle.loads(data)
|
|
|
|
return msgpack.ExtType(code, data)
|
|
|
|
|
|
|
|
|
2018-05-28 07:18:18 -04:00
|
|
|
class MSGPackModernSerializer(BaseSerializer):
|
|
|
|
def dumps(self, value):
|
2018-06-08 00:46:41 -04:00
|
|
|
return msgpack.packb(value, default=default, use_bin_type=True)
|
2018-05-28 07:18:18 -04:00
|
|
|
|
|
|
|
def loads(self, value):
|
2018-06-08 00:46:41 -04:00
|
|
|
return msgpack.unpackb(value, ext_hook=ext_hook, raw=False)
|