File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ __all__ = [
2
+ "Serializer" ,
3
+ "Deserializer" ,
4
+ "JsonSerializer" ,
5
+ "JsonDeserializer" ,
6
+ ]
7
+
8
+ from json import dumps , loads
9
+ from typing import Any , Generic , TypeVar
10
+
11
+ T = TypeVar ("T" )
12
+
13
+
14
+ class Serializer (Generic [T ]):
15
+ """
16
+ Serializer interface
17
+ """
18
+
19
+ def __call__ (self , data : T ) -> str :
20
+ raise NotImplementedError
21
+
22
+
23
+ class Deserializer :
24
+ """
25
+ De-serializer interface
26
+ """
27
+
28
+ def __call__ (self , data : str ) -> Any :
29
+ raise NotImplementedError
30
+
31
+
32
+ class JsonSerializer (Serializer [Any ]):
33
+ """
34
+ Default JSON serializer
35
+ """
36
+
37
+ def __call__ (self , data : Any ) -> str :
38
+ return dumps (data , separators = ("," , ":" ))
39
+
40
+
41
+ class JsonDeserializer (Deserializer ):
42
+ """
43
+ Default JSON de-serializer
44
+ """
45
+
46
+ def __call__ (self , data : str ) -> Any :
47
+ return loads (data )
You can’t perform that action at this time.
0 commit comments