From 9ba46870d04d76478fd48a4542847593fbaa9e54 Mon Sep 17 00:00:00 2001 From: Simon Kuberski Date: Tue, 25 Oct 2022 16:16:07 +0200 Subject: [PATCH] feat: JSON export can handle numpy.int64 entries --- pyerrors/input/json.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pyerrors/input/json.py b/pyerrors/input/json.py index 69fa2c2d..45e6daa0 100644 --- a/pyerrors/input/json.py +++ b/pyerrors/input/json.py @@ -186,10 +186,15 @@ def create_json_string(ol, description='', indent=1): else: raise Exception("Unkown datatype.") + def _jsonifier(o): + if isinstance(o, np.int64): + return int(o) + raise TypeError('%r is not JSON serializable' % o) + if indent: - return json.dumps(d, indent=indent, ensure_ascii=False, write_mode=json.WM_SINGLE_LINE_ARRAY) + return json.dumps(d, indent=indent, ensure_ascii=False, default=_jsonifier, write_mode=json.WM_SINGLE_LINE_ARRAY) else: - return json.dumps(d, indent=indent, ensure_ascii=False, write_mode=json.WM_COMPACT) + return json.dumps(d, indent=indent, ensure_ascii=False, default=_jsonifier, write_mode=json.WM_COMPACT) def dump_to_json(ol, fname, description='', indent=1, gz=True):