my.core.serialize: support serializing Paths

This commit is contained in:
Sean Breckenridge 2021-03-27 18:23:44 -07:00 committed by karlicoss
parent 1b36bd4379
commit d47f3c28aa

View file

@ -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