python-benedict/benedict/serializers/pickle.py

25 lines
664 B
Python
Raw Normal View History

2020-02-19 17:14:23 +00:00
# -*- coding: utf-8 -*-
from benedict.serializers.abstract import AbstractSerializer
import base64
2020-02-19 17:14:23 +00:00
import pickle
class PickleSerializer(AbstractSerializer):
2022-02-13 10:56:44 +00:00
"""
This class describes a pickle serializer.
"""
def __init__(self):
super(PickleSerializer, self).__init__()
def decode(self, s, **kwargs):
2022-02-13 10:35:43 +00:00
encoding = kwargs.pop("encoding", "utf-8")
2021-10-12 12:27:35 +00:00
return pickle.loads(base64.b64decode(s.encode(encoding)), **kwargs)
2020-02-19 17:14:23 +00:00
def encode(self, d, **kwargs):
2022-02-13 10:35:43 +00:00
encoding = kwargs.pop("encoding", "utf-8")
kwargs.setdefault("protocol", 2)
2021-10-12 12:27:35 +00:00
return base64.b64encode(pickle.dumps(d, **kwargs)).decode(encoding)