diff --git a/pyhmy/util.py b/pyhmy/util.py index 03d691e..ac35e81 100644 --- a/pyhmy/util.py +++ b/pyhmy/util.py @@ -101,7 +101,7 @@ def download_cli(bin_name="hmy", replace=True, verbose=True): os.chdir(old_cwd) -def json_load(string): +def json_load(string, **kwargs): """ :param string: The JSON string to load :returns A dictionary loaded from a JSON string to a dictionary. @@ -110,7 +110,7 @@ def json_load(string): Note that this prints the failed input should an error arise. """ try: - return json.loads(string) + return json.loads(string, **kwargs) except Exception as e: print(f"{Typgpy.FAIL}Could not parse input: '{string}'{Typgpy.ENDC}") raise e from e diff --git a/tests/util-pyhmy/test_util.py b/tests/util-pyhmy/test_util.py new file mode 100644 index 0000000..3e86935 --- /dev/null +++ b/tests/util-pyhmy/test_util.py @@ -0,0 +1,22 @@ +import decimal +import json + +import pytest + +from pyhmy import util + + +def test_json_load(): + dec = util.json_load('1.1', parse_float=decimal.Decimal) + assert isinstance(dec, decimal.Decimal) + assert float(dec) == 1.1 + ref_dict = { + 'test': 'val', + 'arr': [ + 1, + 2, + 3 + ] + } + loaded_dict = util.json_load(json.dumps(ref_dict)) + assert str(ref_dict) == str(loaded_dict)