Skip to content

Commit

Permalink
Merge pull request #60 from silx-kit/float128
Browse files Browse the repository at this point in the history
Convert float128 in float when serializing to JSON
  • Loading branch information
loichuder authored Feb 28, 2022
2 parents 975cde5 + 0f2ae61 commit 35b7d47
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion h5grove/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ def bin_encode(array: Sequence[Number]) -> bytes:
return sanitized_array.tobytes()


def orjson_default(o: Any) -> Union[list, str, None]:
def orjson_default(o: Any) -> Union[list, float, str, None]:
"""Converts Python objects to JSON-serializable objects.
:raises TypeError: if the object is not supported."""
if isinstance(o, np.float128):
# float128 is not converted to native float by NumPy so we need to force it even if it means loosing precision
return float(o)
if isinstance(o, (np.generic, np.ndarray)):
return o.tolist()
if isinstance(o, complex):
Expand Down

0 comments on commit 35b7d47

Please sign in to comment.