diff --git a/pyerrors/input/json.py b/pyerrors/input/json.py index deda0d9e..2e960c3b 100644 --- a/pyerrors/input/json.py +++ b/pyerrors/input/json.py @@ -279,22 +279,24 @@ def import_json_string(json_string, verbose=True, full_output=False): ret[-1].tag = taglist[i] return np.reshape(ret, layout) - prog = json_string.get('program', '') - version = json_string.get('version', '') - who = json_string.get('who', '') - date = json_string.get('date', '') - host = json_string.get('host', '') + json_dict = json.loads(json_string) + + prog = json_dict.get('program', '') + version = json_dict.get('version', '') + who = json_dict.get('who', '') + date = json_dict.get('date', '') + host = json_dict.get('host', '') if prog and verbose: print('Data has been written using %s.' % (prog)) if version and verbose: print('Format version %s' % (version)) if np.any([who, date, host] and verbose): print('Written by %s on %s on host %s' % (who, date, host)) - description = json_string.get('description', '') + description = json_dict.get('description', '') if description and verbose: print() print('Description: ', description) - obsdata = json_string['obsdata'] + obsdata = json_dict['obsdata'] ol = [] for io in obsdata: if io['type'] == 'Obs': @@ -346,11 +348,11 @@ def load_json(fname, verbose=True, gz=True, full_output=False): if not fname.endswith('.gz'): fname += '.gz' with gzip.open(fname, 'r') as fin: - d = json.loads(fin.read().decode('utf-8')) + d = fin.read().decode('utf-8') else: if fname.endswith('.gz'): warnings.warn("Trying to read from %s without unzipping!" % fname, UserWarning) with open(fname, 'r', encoding='utf-8') as fin: - d = json.loads(fin.read()) + d = fin.read() return import_json_string(d, verbose, full_output) diff --git a/tests/io_test.py b/tests/io_test.py index 79b61c61..18a48a76 100644 --- a/tests/io_test.py +++ b/tests/io_test.py @@ -1,4 +1,4 @@ -import pyerrors.obs as pe +import pyerrors as pe import pyerrors.input.json as jsonio import numpy as np import os @@ -66,3 +66,10 @@ def test_jsonio(): assert np.all(o == r) assert(description == rl['description']) + + +def test_json_string_reconstruction(): + my_obs = pe.Obs([np.random.rand(100)], ['name']) + json_string = pe.input.json.create_json_string(my_obs) + reconstructed_obs = pe.input.json.import_json_string(json_string) + assert my_obs == reconstructed_obs