From dd534e5384be8cf0c0fa35865afe2126ba75ac55 Mon Sep 17 00:00:00 2001 From: rgthree Date: Sat, 10 Aug 2024 20:40:00 -0400 Subject: [PATCH] Parse json through several passes of strictness. Fixes #294 --- py/utils.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/py/utils.py b/py/utils.py index 7fd0f95..bcfe248 100644 --- a/py/utils.py +++ b/py/utils.py @@ -84,8 +84,19 @@ def load_json_file(file: str, default=None): """Reads a json file and returns the json dict, stripping out "//" comments first.""" if path_exists(file): with open(file, 'r', encoding='UTF-8') as file: - config = re.sub(r"(?:^|\s)//.*", "", file.read(), flags=re.MULTILINE) - return json.loads(config) + config = file.read() + try: + return json.loads(config) + except json.decoder.JSONDecodeError: + try: + config = re.sub(r"^\s*//\s.*", "", config, flags=re.MULTILINE) + return json.loads(config) + except json.decoder.JSONDecodeError: + try: + config = re.sub(r"(?:^|\s)//.*", "", config, flags=re.MULTILINE) + return json.loads(config) + except json.decoder.JSONDecodeError: + pass return default