From d47f3c28aa425c35422c7c6380235f76987fa2ea Mon Sep 17 00:00:00 2001 From: Sean Breckenridge Date: Sat, 27 Mar 2021 18:23:44 -0700 Subject: [PATCH] my.core.serialize: support serializing Paths --- my/core/serialize.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/my/core/serialize.py b/my/core/serialize.py index 34b8bdd..2f27b8d 100644 --- a/my/core/serialize.py +++ b/my/core/serialize.py @@ -1,4 +1,5 @@ import datetime +from pathlib import Path from typing import Any, Optional, Callable, NamedTuple from functools import lru_cache @@ -25,6 +26,9 @@ def _default_encode(obj: Any) -> Any: return obj._asdict() if isinstance(obj, datetime.timedelta): return obj.total_seconds() + # convert paths to their string representation + if isinstance(obj, Path): + return str(obj) if isinstance(obj, Exception): return error_to_json(obj) # note: _serialize would only be called for items which aren't already