Total Complexity | 3 |
Total Lines | 27 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import base64 |
||
2 | import pickle |
||
3 | |||
4 | from benedict.serializers.abstract import AbstractSerializer |
||
5 | |||
6 | |||
7 | class PickleSerializer(AbstractSerializer): |
||
8 | """ |
||
9 | This class describes a pickle serializer. |
||
10 | """ |
||
11 | |||
12 | def __init__(self): |
||
13 | super().__init__( |
||
14 | extensions=[ |
||
15 | "pickle", |
||
16 | ], |
||
17 | ) |
||
18 | |||
19 | def decode(self, s, **kwargs): |
||
20 | encoding = kwargs.pop("encoding", "utf-8") |
||
21 | return pickle.loads(base64.b64decode(s.encode(encoding)), **kwargs) |
||
22 | |||
23 | def encode(self, d, **kwargs): |
||
24 | encoding = kwargs.pop("encoding", "utf-8") |
||
25 | kwargs.setdefault("protocol", 2) |
||
26 | return base64.b64encode(pickle.dumps(d, **kwargs)).decode(encoding) |
||
27 |