python-arango-async 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,111 @@
1
+ __all__ = [
2
+ "Serializer",
3
+ "Deserializer",
4
+ "JsonSerializer",
5
+ "JsonDeserializer",
6
+ "DefaultSerializer",
7
+ "DefaultDeserializer",
8
+ ]
9
+
10
+ import json
11
+ from abc import ABC, abstractmethod
12
+ from typing import Generic, Sequence, TypeVar
13
+
14
+ from arangoasync.exceptions import DeserializationError, SerializationError
15
+ from arangoasync.typings import Json, Jsons
16
+
17
+ T = TypeVar("T")
18
+ U = TypeVar("U")
19
+
20
+
21
+ class Serializer(ABC, Generic[T]): # pragma: no cover
22
+ """Abstract base class for serialization.
23
+
24
+ Custom serialization classes should inherit from this class.
25
+ Please be mindful of the performance implications.
26
+ """
27
+
28
+ @abstractmethod
29
+ def dumps(self, data: T | Sequence[T | str]) -> str:
30
+ """Serialize any generic data.
31
+
32
+ Args:
33
+ data: Data to serialize.
34
+
35
+ Returns:
36
+ str: Serialized data.
37
+
38
+ Raises:
39
+ SerializationError: If the data cannot be serialized.
40
+ """
41
+ raise NotImplementedError
42
+
43
+
44
+ class Deserializer(ABC, Generic[T, U]): # pragma: no cover
45
+ """Abstract base class for deserialization.
46
+
47
+ Custom deserialization classes should inherit from this class.
48
+ Please be mindful of the performance implications.
49
+ """
50
+
51
+ @abstractmethod
52
+ def loads(self, data: bytes) -> T:
53
+ """Deserialize response data.
54
+
55
+ Will be called on generic server data (such as server status) and
56
+ single documents.
57
+
58
+ Args:
59
+ data (bytes): Data to deserialize.
60
+
61
+ Returns:
62
+ Deserialized data.
63
+
64
+ Raises:
65
+ DeserializationError: If the data cannot be deserialized.
66
+ """
67
+ raise NotImplementedError
68
+
69
+ @abstractmethod
70
+ def loads_many(self, data: bytes) -> U:
71
+ """Deserialize response data.
72
+
73
+ Will only be called when deserializing a list of documents.
74
+
75
+ Args:
76
+ data (bytes): Data to deserialize.
77
+
78
+ Returns:
79
+ Deserialized data.
80
+
81
+ Raises:
82
+ DeserializationError: If the data cannot be deserialized.
83
+ """
84
+ raise NotImplementedError
85
+
86
+
87
+ class JsonSerializer(Serializer[Json]):
88
+ """JSON serializer."""
89
+
90
+ def dumps(self, data: Json | Sequence[str | Json]) -> str:
91
+ try:
92
+ return json.dumps(data, separators=(",", ":"))
93
+ except Exception as e:
94
+ raise SerializationError("Failed to serialize data to JSON.") from e
95
+
96
+
97
+ class JsonDeserializer(Deserializer[Json, Jsons]):
98
+ """JSON deserializer."""
99
+
100
+ def loads(self, data: bytes) -> Json:
101
+ try:
102
+ return json.loads(data) # type: ignore[no-any-return]
103
+ except Exception as e:
104
+ raise DeserializationError("Failed to deserialize data from JSON.") from e
105
+
106
+ def loads_many(self, data: bytes) -> Jsons:
107
+ return self.loads(data) # type: ignore[return-value]
108
+
109
+
110
+ DefaultSerializer = JsonSerializer
111
+ DefaultDeserializer = JsonDeserializer