-
Notifications
You must be signed in to change notification settings - Fork 1
/
numpy_json.py
72 lines (51 loc) · 2.11 KB
/
numpy_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json
import numpy as np
_json_keys = dict(
data="arraydata",
dtype="arraydtype",
shape="arraysize"
)
_complex_keys = dict(
real="real",
imag="imag"
)
def dumps(data, json_keys=None, complex_keys=None):
global _json_keys, _complex_keys
if json_keys is None:
json_keys = _json_keys
if complex_keys is None:
complex_keys = _complex_keys
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
data = {
json_keys['data']: obj.tolist(),
json_keys['shape']: obj.shape
}
if 'dtype' in json_keys:
data[json_keys['dtype']] = str(obj.dtype)
return data
if isinstance(obj, np.complex):
return {
complex_keys['real']: obj.real,
complex_keys['imag']: obj.imag,
}
return json.JSONEncoder(self, obj)
return json.dumps(data, cls=NumpyEncoder)
def loads(json_string, json_keys=None, complex_keys=None):
global _json_keys, _complex_keys
if json_keys is None:
json_keys = _json_keys
if complex_keys is None:
complex_keys = _complex_keys
def json_numpy_obj_hook(data):
if isinstance(data, dict) and json_keys['data'] in data and json_keys['shape'] in data:
if 'dtype' in json_keys and json_keys['dtype'] in data:
dtype = data[json_keys['dtype']]
else:
dtype = np.float
return np.asarray(data[json_keys['data']], dtype=dtype).reshape(data[json_keys['shape']])
if isinstance(data, dict) and complex_keys['real'] in data and complex_keys['imag'] in data:
return complex(data[complex_keys['real']], data[complex_keys['imag']])
return data
return json.loads(json_string, object_hook=json_numpy_obj_hook)