From 4593368d4d4087cef99122566e00dad5f9e1f844 Mon Sep 17 00:00:00 2001 From: Yevhenii Vaskivskyi Date: Mon, 30 Oct 2023 21:27:31 +0100 Subject: [PATCH] Improve JSON parsing (#315) --- asusrouter/modules/endpoint/onboarding.py | 3 +++ asusrouter/modules/homeassistant.py | 1 - asusrouter/tools/readers.py | 32 ++++++++++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/asusrouter/modules/endpoint/onboarding.py b/asusrouter/modules/endpoint/onboarding.py index f4b0600..b89c353 100644 --- a/asusrouter/modules/endpoint/onboarding.py +++ b/asusrouter/modules/endpoint/onboarding.py @@ -32,6 +32,9 @@ def read(content: str) -> dict[str, Any]: ) content = '{"' + content[:-3] + "}" + # In case we have a trailing comma inside a dict + content = content.replace(",}", "}") + # Read the json content onboarding: dict[str, Any] = read_json_content(content) diff --git a/asusrouter/modules/homeassistant.py b/asusrouter/modules/homeassistant.py index d67d508..e5d8033 100644 --- a/asusrouter/modules/homeassistant.py +++ b/asusrouter/modules/homeassistant.py @@ -141,7 +141,6 @@ def convert_to_ha_string(data: Any) -> str: # Check whether value is an enum or a string # If string, return it, if enum, go recursive if isinstance(data, Enum): - print("Enum") return convert_to_ha_string(data.value) # Check if we have string diff --git a/asusrouter/tools/readers.py b/asusrouter/tools/readers.py index a110406..6af5ccf 100644 --- a/asusrouter/tools/readers.py +++ b/asusrouter/tools/readers.py @@ -3,12 +3,30 @@ from __future__ import annotations import json +import logging import re from typing import Any, Optional from asusrouter.const import ContentType from asusrouter.tools.converters import clean_string +_LOGGER = logging.getLogger(__name__) + + +# Random symbols to avoid json errors +RANDOM_SYMBOLS: list[str] = [ + "\u0000", + "\u0001", + "\u0002", + "\u0003", + "\u0004", + "\u0005", + "\u0006", + "\u0007", + "\u0008", + "\u0009", +] + def merge_dicts(data: dict[Any, Any], merge_data: dict[Any, Any]) -> dict[Any, Any]: """This methods merges two nested dicts into a single one @@ -117,8 +135,20 @@ def read_json_content(content: Optional[str]) -> dict[str, Any]: if not content: return {} + # Random control characters to avoid json errors + for symbol in RANDOM_SYMBOLS: + content = content.replace(symbol, "") + # Return the json content - return json.loads(content.encode().decode("utf-8-sig")) + try: + return json.loads(content.encode().decode("utf-8-sig")) + except json.JSONDecodeError as ex: + _LOGGER.error( + "Unable to decode json content with exception `%s`. Please, copy this end fill in a bug report: %s", + ex, + content, + ) + return {} def readable_mac(raw: str) -> bool: