|
14 | 14 | pool = ThreadPoolExecutor() |
15 | 15 |
|
16 | 16 |
|
17 | | -class ManifestEncoder(json.JSONEncoder): |
18 | | - def default(self, o): |
19 | | - if isinstance(o, datetime.datetime): |
20 | | - return { |
21 | | - "_type": "datetime.datetime", |
22 | | - "value": o.isoformat(), |
23 | | - } |
24 | | - return super().default(o) |
25 | | - |
| 17 | +def encode_date(obj): |
| 18 | + if isinstance(obj, datetime.datetime): |
| 19 | + return { |
| 20 | + "_type": "datetime.datetime", |
| 21 | + "value": obj.isoformat(), |
| 22 | + } |
| 23 | + raise TypeError |
26 | 24 |
|
27 | | -class ManifestDecoder(json.JSONDecoder): |
28 | | - def __init__(self, *args, **kwargs): |
29 | | - super().__init__(object_hook=self.object_hook, *args, **kwargs) |
30 | 25 |
|
31 | | - def object_hook(self, o): |
32 | | - type_ = o.get("_type") |
33 | | - if type_ != "datetime.datetime": |
34 | | - return o |
35 | | - return dateutil.parser.parse(o["value"]) |
| 26 | +def decode_date(o): |
| 27 | + type_ = o.get("_type") |
| 28 | + if type_ != "datetime.datetime": |
| 29 | + return o |
| 30 | + return dateutil.parser.parse(o["value"]) |
36 | 31 |
|
37 | 32 |
|
38 | 33 | class DurableBuffer: |
@@ -75,12 +70,12 @@ async def _save_manifest(self): |
75 | 70 |
|
76 | 71 | def _write_manifest(self): |
77 | 72 | with open(self._manifest_path, "w") as fp: |
78 | | - json.dump(list(self.q._queue), fp, cls=ManifestEncoder) |
| 73 | + json.dump(list(self.q._queue), fp, default=encode_date) |
79 | 74 |
|
80 | 75 | def _read_manifest(self): |
81 | 76 | try: |
82 | 77 | with open(self._manifest_path, "r") as fp: |
83 | | - return json.load(fp, cls=ManifestDecoder) |
| 78 | + return json.load(fp, object_hook=decode_date) |
84 | 79 | except FileNotFoundError: |
85 | 80 | return [] |
86 | 81 | except json.decoder.JSONDecodeError: |
|
0 commit comments