11
11
import json
12
12
13
13
14
- class NullSerializer :
14
+ _NOT_SET = object ()
15
+
16
+
17
+ class BaseSerializer :
18
+
19
+ DEFAULT_ENCODING = 'utf-8'
20
+
21
+ def __init__ (self , * args , encoding = _NOT_SET , ** kwargs ):
22
+ self .encoding = self .DEFAULT_ENCODING if encoding is _NOT_SET else encoding
23
+ super ().__init__ (* args , ** kwargs )
24
+
25
+ def dumps (self , value ):
26
+ raise NotImplementedError ('dumps method must be implemented' )
27
+
28
+ def loads (self , value ):
29
+ raise NotImplementedError ('loads method must be implemented' )
30
+
31
+
32
+ class NullSerializer (BaseSerializer ):
15
33
"""
16
34
This serializer does nothing. Its only recommended to be used by
17
35
:class:`aiocache.SimpleMemoryCache` because for other backends it will
@@ -26,24 +44,20 @@ class NullSerializer:
26
44
my_list.append(2)
27
45
await cache.get("key") # Will return [1, 2]
28
46
"""
29
- encoding = 'utf-8'
30
-
31
- @classmethod
32
- def dumps (cls , value ):
47
+ def dumps (self , value ):
33
48
"""
34
49
Returns the same value
35
50
"""
36
51
return value
37
52
38
- @classmethod
39
- def loads (cls , value ):
53
+ def loads (self , value ):
40
54
"""
41
55
Returns the same value
42
56
"""
43
57
return value
44
58
45
59
46
- class StringSerializer :
60
+ class StringSerializer ( BaseSerializer ) :
47
61
"""
48
62
Converts all input values to str. All return values are also str. Be
49
63
careful because this means that if you store an ``int(1)``, you will get
@@ -54,13 +68,7 @@ class StringSerializer:
54
68
If you want to keep python types, use ``PickleSerializer``. ``JsonSerializer``
55
69
may also be useful to keep type of symple python types.
56
70
"""
57
- encoding = 'utf-8'
58
-
59
- def __init__ (self , * args , ** kwargs ):
60
- super ().__init__ (* args , ** kwargs )
61
-
62
- @classmethod
63
- def dumps (cls , value ):
71
+ def dumps (self , value ):
64
72
"""
65
73
Serialize the received value casting it to str.
66
74
@@ -69,22 +77,20 @@ def dumps(cls, value):
69
77
"""
70
78
return str (value )
71
79
72
- @classmethod
73
- def loads (cls , value ):
80
+ def loads (self , value ):
74
81
"""
75
82
Returns value back without transformations
76
83
"""
77
84
return value
78
85
79
86
80
- class PickleSerializer (StringSerializer ):
87
+ class PickleSerializer (BaseSerializer ):
81
88
"""
82
89
Transform data to bytes using pickle.dumps and pickle.loads to retrieve it back.
83
90
"""
84
- encoding = None
91
+ DEFAULT_ENCODING = None
85
92
86
- @classmethod
87
- def dumps (cls , value ):
93
+ def dumps (self , value ):
88
94
"""
89
95
Serialize the received value using ``pickle.dumps``.
90
96
@@ -93,8 +99,7 @@ def dumps(cls, value):
93
99
"""
94
100
return pickle .dumps (value )
95
101
96
- @classmethod
97
- def loads (cls , value ):
102
+ def loads (self , value ):
98
103
"""
99
104
Deserialize value using ``pickle.loads``.
100
105
@@ -106,7 +111,7 @@ def loads(cls, value):
106
111
return pickle .loads (value )
107
112
108
113
109
- class JsonSerializer (StringSerializer ):
114
+ class JsonSerializer (BaseSerializer ):
110
115
"""
111
116
Transform data to json string with json.dumps and json.loads to retrieve it back. Check
112
117
https://docs.python.org/3/library/json.html#py-to-json-table for how types are converted.
@@ -116,9 +121,7 @@ class JsonSerializer(StringSerializer):
116
121
- ujson dumps supports bytes while json doesn't
117
122
- ujson and json outputs may differ sometimes
118
123
"""
119
-
120
- @classmethod
121
- def dumps (cls , value ):
124
+ def dumps (self , value ):
122
125
"""
123
126
Serialize the received value using ``json.dumps``.
124
127
@@ -127,8 +130,7 @@ def dumps(cls, value):
127
130
"""
128
131
return json .dumps (value )
129
132
130
- @classmethod
131
- def loads (cls , value ):
133
+ def loads (self , value ):
132
134
"""
133
135
Deserialize value using ``json.loads``.
134
136
@@ -140,14 +142,21 @@ def loads(cls, value):
140
142
return json .loads (value )
141
143
142
144
143
- class MsgPackSerializer (StringSerializer ):
145
+ class MsgPackSerializer (BaseSerializer ):
144
146
"""
145
147
Transform data to bytes using msgpack.dumps and msgpack.loads to retrieve it back. You need
146
148
to have ``msgpack`` installed in order to be able to use this serializer.
149
+
150
+ :param encoding: str. Can be used to change encoding param for ``msg.loads`` method.
151
+ Default is utf-8.
152
+ :param use_list: bool. Can be used to change use_list param for ``msgpack.loads`` method.
153
+ Default is True.
147
154
"""
155
+ def __init__ (self , * args , use_list = True , ** kwargs ):
156
+ self .use_list = use_list
157
+ super ().__init__ (* args , ** kwargs )
148
158
149
- @classmethod
150
- def dumps (cls , value ):
159
+ def dumps (self , value ):
151
160
"""
152
161
Serialize the received value using ``msgpack.dumps``.
153
162
@@ -156,8 +165,7 @@ def dumps(cls, value):
156
165
"""
157
166
return msgpack .dumps (value )
158
167
159
- @classmethod
160
- def loads (cls , value ):
168
+ def loads (self , value ):
161
169
"""
162
170
Deserialize value using ``msgpack.loads``.
163
171
@@ -166,4 +174,4 @@ def loads(cls, value):
166
174
"""
167
175
if value is None :
168
176
return None
169
- return msgpack .loads (value , encoding = cls .encoding )
177
+ return msgpack .loads (value , encoding = self .encoding , use_list = self . use_list )
0 commit comments