Skip to content

Commit e48635f

Browse files
0.14.3
1 parent a3a0f56 commit e48635f

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotPython"
10-
version = "0.14.2"
10+
version = "0.14.3"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/utils/numpy2json.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import json
2+
import numpy as np
3+
4+
5+
class NumpyEncoder(json.JSONEncoder):
6+
"""
7+
JSONEncoder subclass that knows how to encode numpy arrays.
8+
9+
Note:
10+
Taken from:
11+
https://stackoverflow.com/questions/26646362/numpy-array-is-not-json-serializable
12+
13+
"""
14+
15+
def default(self, obj):
16+
if isinstance(obj, np.integer):
17+
return int(obj)
18+
elif isinstance(obj, np.floating):
19+
return float(obj)
20+
elif isinstance(obj, np.bool_):
21+
return int(obj)
22+
elif isinstance(obj, np.ndarray):
23+
return obj.tolist()
24+
elif isinstance(obj, np.float64):
25+
if np.isnan(obj):
26+
return "NaN"
27+
elif np.isinf(obj):
28+
return "Inf"
29+
else:
30+
return float(obj)
31+
return json.JSONEncoder.default(self, obj)

0 commit comments

Comments
 (0)