From 8b21766f1516c7313ae02d5739db1b699b11c4e0 Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Mon, 11 Mar 2024 15:54:45 +0100 Subject: [PATCH] Fix serialization of complex256 objects to JSON --- h5grove/encoders.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/h5grove/encoders.py b/h5grove/encoders.py index 4d20fe1..b9a3316 100644 --- a/h5grove/encoders.py +++ b/h5grove/encoders.py @@ -4,6 +4,7 @@ import orjson import h5py import tifffile +import numbers from .utils import QueryArgumentError, is_numeric_data @@ -23,10 +24,10 @@ def orjson_default(o: Any) -> Union[list, float, str, None]: if isinstance(o, np.number) and o.dtype.kind == "f" and o.itemsize > 8: # Force conversion of float >64bits to native float even if it means losing precision return float(o) + if isinstance(o, numbers.Complex): + return [o.real, o.imag] if isinstance(o, (np.generic, np.ndarray)): return o.tolist() - if isinstance(o, complex): - return [o.real, o.imag] if isinstance(o, h5py.Empty): return None if isinstance(o, bytes):