2020-02-19 17:14:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from benedict.serializers.abstract import AbstractSerializer
|
|
|
|
|
2020-02-21 12:53:09 +00:00
|
|
|
import base64
|
2020-02-19 17:14:23 +00:00
|
|
|
import pickle
|
|
|
|
|
|
|
|
|
|
|
|
class PickleSerializer(AbstractSerializer):
|
|
|
|
|
2020-02-20 16:14:48 +00:00
|
|
|
def __init__(self):
|
|
|
|
super(PickleSerializer, self).__init__()
|
|
|
|
|
2020-02-21 12:53:09 +00:00
|
|
|
def decode(self, s, **kwargs):
|
|
|
|
encoding = kwargs.pop('encoding', 'utf-8')
|
|
|
|
return pickle.loads(
|
|
|
|
base64.b64decode(s.encode(encoding)), **kwargs)
|
2020-02-19 17:14:23 +00:00
|
|
|
|
2020-02-20 16:14:48 +00:00
|
|
|
def encode(self, d, **kwargs):
|
2020-02-21 12:53:09 +00:00
|
|
|
encoding = kwargs.pop('encoding', 'utf-8')
|
2020-02-21 13:36:07 +00:00
|
|
|
kwargs.setdefault('protocol', 2)
|
2020-02-21 12:53:09 +00:00
|
|
|
return base64.b64encode(
|
|
|
|
pickle.dumps(d, **kwargs)).decode(encoding)
|