12
12
import zlib
13
13
from collections import Counter
14
14
from collections .abc import AsyncIterator , Callable , Sequence
15
- from datetime import datetime
16
15
from enum import StrEnum , auto
17
16
from typing import TYPE_CHECKING , Any , ClassVar
18
- from uuid import UUID
19
17
20
18
import anyio
21
19
from pydantic import BaseModel
@@ -62,22 +60,6 @@ class AsyncRetryError(Exception):
62
60
"""Flags a retry for another tenacity attempt."""
63
61
64
62
65
- class RobustEncoder (json .JSONEncoder ):
66
- """JSON encoder that can handle UUID and set objects."""
67
-
68
- def default (self , o ):
69
- if isinstance (o , UUID ):
70
- # if the obj is uuid, we simply return the value of uuid
71
- return str (o )
72
- if isinstance (o , set ):
73
- return list (o )
74
- if isinstance (o , os .PathLike ):
75
- return str (o )
76
- if isinstance (o , datetime ):
77
- return o .isoformat ()
78
- return json .JSONEncoder .default (self , o )
79
-
80
-
81
63
class SearchDocumentStorage (StrEnum ):
82
64
"""Method to serialize a document."""
83
65
@@ -95,7 +77,7 @@ def extension(self) -> str:
95
77
def write_to_string (self , data : BaseModel | SupportsPickle ) -> bytes :
96
78
if self == SearchDocumentStorage .JSON_MODEL_DUMP :
97
79
if isinstance (data , BaseModel ):
98
- return json . dumps ( data .model_dump (), cls = RobustEncoder ).encode ("utf-8" )
80
+ return data .model_dump_json ( ).encode ("utf-8" )
99
81
raise ValueError ("JSON_MODEL_DUMP requires a BaseModel object." )
100
82
if self == SearchDocumentStorage .PICKLE_COMPRESSED :
101
83
return zlib .compress (pickle .dumps (data ))
0 commit comments