my.core.serialize: support serializing Paths
This commit is contained in:
parent
1b36bd4379
commit
d47f3c28aa
1 changed files with 4 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
from pathlib import Path
|
||||||
from typing import Any, Optional, Callable, NamedTuple
|
from typing import Any, Optional, Callable, NamedTuple
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
|
||||||
|
@ -25,6 +26,9 @@ def _default_encode(obj: Any) -> Any:
|
||||||
return obj._asdict()
|
return obj._asdict()
|
||||||
if isinstance(obj, datetime.timedelta):
|
if isinstance(obj, datetime.timedelta):
|
||||||
return obj.total_seconds()
|
return obj.total_seconds()
|
||||||
|
# convert paths to their string representation
|
||||||
|
if isinstance(obj, Path):
|
||||||
|
return str(obj)
|
||||||
if isinstance(obj, Exception):
|
if isinstance(obj, Exception):
|
||||||
return error_to_json(obj)
|
return error_to_json(obj)
|
||||||
# note: _serialize would only be called for items which aren't already
|
# note: _serialize would only be called for items which aren't already
|
||||||
|
|
Loading…
Add table
Reference in a new issue