diff --git a/slic/utils/json.py b/slic/utils/json.py index 79ab5cc40..77f26e0ff 100644 --- a/slic/utils/json.py +++ b/slic/utils/json.py @@ -1,8 +1,9 @@ import json import numpy as np +from pathlib import Path -class NumpyJSONEncoder(json.JSONEncoder): +class ExtendedJSONEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.ndarray): @@ -11,15 +12,17 @@ class NumpyJSONEncoder(json.JSONEncoder): return obj.item() elif isinstance(obj, complex): # covers builtin complex return {"real": obj.real, "imag": obj.imag} + elif isinstance(obj, Path): + return str(obj) return super().default(obj) def json_validate(obj): - return json.loads(json.dumps(obj, cls=NumpyJSONEncoder)) + return json.loads(json.dumps(obj, cls=ExtendedJSONEncoder)) -def json_save(what, filename, *args, indent=4, sort_keys=True, cls=NumpyJSONEncoder, **kwargs): +def json_save(what, filename, *args, indent=4, sort_keys=True, cls=ExtendedJSONEncoder, **kwargs): with open(filename, "w") as f: json.dump(what, f, *args, indent=indent, sort_keys=sort_keys, cls=cls, **kwargs)