-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializer.py
More file actions
387 lines (300 loc) · 11 KB
/
Copy pathserializer.py
File metadata and controls
387 lines (300 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""
Supports multiple serialization formats with automatic type detection and handling
of common FastAPI data types including Pydantic models, dataclasses, datetime objects,
and more.
"""
import json
import pickle
from datetime import datetime, date, time, timedelta
from decimal import Decimal
from enum import Enum
from typing import Any, Callable, Optional, Union
from uuid import UUID
import warnings
try:
import msgpack
MSGPACK_AVAILABLE = True
except ImportError:
MSGPACK_AVAILABLE = False
try:
from pydantic import BaseModel
PYDANTIC_AVAILABLE = True
except ImportError:
PYDANTIC_AVAILABLE = False
class SerializationFormat(str, Enum):
"""Supported serialization formats."""
JSON = "json"
PICKLE = "pickle"
MSGPACK = "msgpack"
class JSONEncoder(json.JSONEncoder):
"""
Custom JSON encoder that handles additional Python types commonly used in FastAPI.
Supports:
- datetime, date, time objects
- UUID
- Decimal
- Pydantic models
- dataclasses
- Enums
- bytes
- sets
- timedelta
"""
def default(self, obj: Any) -> Any:
# Handle datetime objects
if isinstance(obj, datetime):
return {"__type__": "datetime", "value": obj.isoformat()}
if isinstance(obj, date):
return {"__type__": "date", "value": obj.isoformat()}
if isinstance(obj, time):
return {"__type__": "time", "value": obj.isoformat()}
if isinstance(obj, timedelta):
return {"__type__": "timedelta", "value": obj.total_seconds()}
if isinstance(obj, UUID):
return {"__type__": "uuid", "value": str(obj)}
if isinstance(obj, Decimal):
return {"__type__": "decimal", "value": str(obj)}
if isinstance(obj, Enum):
return {"__type__": "enum", "module": obj.__class__.__module__,
"name": obj.__class__.__name__, "value": obj.value}
if isinstance(obj, bytes):
return {"__type__": "bytes", "value": obj.decode("latin-1")}
if isinstance(obj, set):
return {"__type__": "set", "value": list(obj)}
if isinstance(obj, frozenset):
return {"__type__": "frozenset", "value": list(obj)}
if PYDANTIC_AVAILABLE and isinstance(obj, BaseModel):
return {
"__type__": "pydantic",
"model": f"{obj.__class__.__module__}.{obj.__class__.__name__}",
"value": obj.model_dump()
}
# Handle dataclasses
if hasattr(obj, "__dataclass_fields__"):
from dataclasses import asdict
return {
"__type__": "dataclass",
"class": f"{obj.__class__.__module__}.{obj.__class__.__name__}",
"value": asdict(obj)
}
return super().default(obj)
def _json_object_hook(obj: dict) -> Any:
"""
Custom JSON decoder hook for deserializing custom types.
:param obj: Dictionary to decode
:return: Decoded Python object
"""
if "__type__" not in obj:
return obj
obj_type = obj["__type__"]
if obj_type == "datetime":
return datetime.fromisoformat(obj["value"])
if obj_type == "date":
return date.fromisoformat(obj["value"])
if obj_type == "time":
return time.fromisoformat(obj["value"])
if obj_type == "timedelta":
return timedelta(seconds=obj["value"])
if obj_type == "uuid":
return UUID(obj["value"])
if obj_type == "decimal":
return Decimal(obj["value"])
if obj_type == "enum":
# Dynamically import the enum class
module_path = obj["module"]
class_name = obj["name"]
try:
module = __import__(module_path, fromlist=[class_name])
enum_class = getattr(module, class_name)
return enum_class(obj["value"])
except (ImportError, AttributeError):
# If we can't import the enum, return the value
return obj["value"]
if obj_type == "bytes":
return obj["value"].encode("latin-1")
if obj_type == "set":
return set(obj["value"])
if obj_type == "frozenset":
return frozenset(obj["value"])
if obj_type == "pydantic" and PYDANTIC_AVAILABLE:
model_path = obj["model"]
module_path, class_name = model_path.rsplit(".", 1)
try:
module = __import__(module_path, fromlist=[class_name])
model_class = getattr(module, class_name)
return model_class(**obj["value"])
except (ImportError, AttributeError):
# If we can't import the model, return the dict
return obj["value"]
if obj_type == "dataclass":
class_path = obj["class"]
module_path, class_name = class_path.rsplit(".", 1)
try:
module = __import__(module_path, fromlist=[class_name])
dataclass_type = getattr(module, class_name)
return dataclass_type(**obj["value"])
except (ImportError, AttributeError):
# If we can't import the dataclass, return the dict
return obj["value"]
return obj
def serialize_json(data: Any) -> bytes:
"""
Serialize data to JSON format with support for custom types.
:param data: Data to serialize
:return: Serialized bytes
"""
json_str = json.dumps(data, cls=JSONEncoder, separators=(',', ':'), ensure_ascii=False)
return json_str.encode('utf-8')
def deserialize_json(data: bytes) -> Any:
"""
Deserialize JSON data with support for custom types.
:param data: Serialized bytes
:return: Deserialized Python object
"""
json_str = data.decode('utf-8')
return json.loads(json_str, object_hook=_json_object_hook)
def serialize_pickle(data: Any) -> bytes:
"""
Serialize data using pickle protocol 4 (Python 3.4+).
Note: Pickle is the most flexible but least secure. Only use with trusted data.
:param data: Data to serialize
:return: Serialized bytes
"""
warnings.warn(
"Pickle serialization is unsafe for untrusted data. "
"Only use with trusted cache backends.",
RuntimeWarning,
stacklevel=2,
)
return pickle.dumps(data, protocol=pickle.HIGHEST_PROTOCOL)
def deserialize_pickle(data: bytes) -> Any:
"""
Deserialize pickle data.
:param data: Serialized bytes
:return: Deserialized Python object
"""
return pickle.loads(data)
def serialize_msgpack(data: Any) -> bytes:
"""
Serialize data using MessagePack format.
Requires msgpack package to be installed.
Falls back to JSON if msgpack is not available.
Note: Msgpack uses JSON-compatible transformation internally
:param data: Data to serialize
:return: Serialized bytes
"""
if not MSGPACK_AVAILABLE:
return serialize_json(data)
# Convert to JSON-serializable format first using our custom encoder
json_compatible = json.loads(json.dumps(data, cls=JSONEncoder))
return msgpack.packb(json_compatible, use_bin_type=True)
def deserialize_msgpack(data: bytes) -> Any:
"""
Deserialize MessagePack data.
Requires msgpack package to be installed.
Falls back to JSON if msgpack is not available.
:param data: Serialized bytes
:return: Deserialized Python object
"""
if not MSGPACK_AVAILABLE:
return deserialize_json(data)
unpacked = msgpack.unpackb(data, raw=False)
# Apply the same object hook as JSON to restore custom types
return json.loads(json.dumps(unpacked), object_hook=_json_object_hook)
# Default serialization format
_DEFAULT_FORMAT = SerializationFormat.JSON
# Serializer registry
_SERIALIZERS: dict[SerializationFormat, Callable[[Any], bytes]] = {
SerializationFormat.JSON: serialize_json,
SerializationFormat.PICKLE: serialize_pickle,
SerializationFormat.MSGPACK: serialize_msgpack,
}
# Deserializer registry
_DESERIALIZERS: dict[SerializationFormat, Callable[[bytes], Any]] = {
SerializationFormat.JSON: deserialize_json,
SerializationFormat.PICKLE: deserialize_pickle,
SerializationFormat.MSGPACK: deserialize_msgpack,
}
def set_default_format(format: SerializationFormat) -> None:
"""
Set the default serialization format.
:param format: Serialization format to use
"""
global _DEFAULT_FORMAT
_DEFAULT_FORMAT = format
def get_default_format() -> SerializationFormat:
"""
Get the current default serialization format.
:return: Current default format
"""
return _DEFAULT_FORMAT
def register_serializer(
format: Union[str, SerializationFormat],
serializer: Callable[[Any], bytes],
deserializer: Callable[[bytes], Any]
) -> None:
"""
Register a custom serializer/deserializer pair.
:param format: Format identifier
:param serializer: Serialization function
:param deserializer: Deserialization function
"""
if isinstance(format, str):
format = SerializationFormat(format)
_SERIALIZERS[format] = serializer
_DESERIALIZERS[format] = deserializer
def serialize(
data: Any,
format: Optional[SerializationFormat] = None
) -> bytes:
"""
Serialize data to bytes using the specified or default format.
:param data: Data to serialize
:param format: Optional serialization format (uses default if not specified)
:return: Serialized bytes
:raises ValueError: If the format is not supported
"""
if format is None:
format = _DEFAULT_FORMAT
if format not in _SERIALIZERS:
raise ValueError(f"Unsupported serialization format: {format}")
try:
serializer = _SERIALIZERS[format]
return serializer(data)
except Exception as e:
raise ValueError(f"Failed to serialize data with format {format}: {str(e)}") from e
def deserialize(
data: bytes,
format: Optional[SerializationFormat] = None
) -> Any:
"""
Deserialize bytes to Python object using the specified or default format.
:param data: Serialized bytes
:param format: Optional serialization format (uses default if not specified)
:return: Deserialized Python object
:raises ValueError: If the format is not supported or deserialization fails
"""
if format is None:
format = _DEFAULT_FORMAT
if format not in _DESERIALIZERS:
raise ValueError(f"Unsupported deserialization format: {format}")
try:
deserializer = _DESERIALIZERS[format]
return deserializer(data)
except Exception as e:
raise ValueError(f"Failed to deserialize data with format {format}: {str(e)}") from e
__all__ = [
"serialize",
"deserialize",
"SerializationFormat",
"set_default_format",
"get_default_format",
"register_serializer",
"JSONEncoder",
"serialize_json",
"deserialize_json",
"serialize_pickle",
"deserialize_pickle",
"serialize_msgpack",
"deserialize_msgpack",
]