Skip to content

Commit

Permalink
Fix serialization of complex256 objects to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Mar 11, 2024
1 parent a248a20 commit 3dea3a7
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions h5grove/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import orjson
import h5py
import tifffile
import numbers

from .utils import QueryArgumentError, is_numeric_data

Expand All @@ -23,10 +24,11 @@ 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):
# Force conversion of float >64bits to native float even if it means losing precision
return [float(o.real), float(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):
Expand Down

0 comments on commit 3dea3a7

Please sign in to comment.