Skip to content

Commit

Permalink
Fix JSON reading on missing values (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaskivskyi committed May 28, 2024
1 parent bb41434 commit 9fa2caf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions asusrouter/tools/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ def read_json_content(content: Optional[str]) -> dict[str, Any]:
for symbol in RANDOM_SYMBOLS:
content = content.replace(symbol, "")

# Handle missing values in JSON
content = re.sub(r"\s*,\s*,", ", ", content)
content = re.sub(r"^\s*{\s*,", "{", content)
content = re.sub(r",\s*}\s*$", "}", content)

# Return the json content
try:
return json.loads(content.encode().decode("utf-8-sig"))
Expand Down
1 change: 1 addition & 0 deletions tests/test_data/map_ac1300_stock_386/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test data for Lyra Mini MAP-AC1300 / Stock 386."""
1 change: 1 addition & 0 deletions tests/test_data/map_ac1300_stock_386/hook_001.content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ , "wgs_enable":"", "wgs_lanaccess":"", "wgs_addr":"", "wgs_port":"", "wgs_dns":"", "wgs_nat6":"", "wgs_psk":"", "wgs_alive":"", "wgs_priv":"", "wgs_pub":"", "wgs1_c1_enable":"", "wgs1_c1_name":"", "wgs1_c1_addr":"", "wgs1_c1_aips":"", "wgs1_c1_caips":"", "wgs1_c2_enable":"", "wgs1_c2_name":"", "wgs1_c2_addr":"", "wgs1_c2_aips":"", "wgs1_c2_caips":"", "wgs1_c3_enable":"", "wgs1_c3_name":"", "wgs1_c3_addr":"", "wgs1_c3_aips":"", "wgs1_c3_caips":"", "wgs1_c4_enable":"", "wgs1_c4_name":"", "wgs1_c4_addr":"", "wgs1_c4_aips":"", "wgs1_c4_caips":"", "wgs1_c5_enable":"", "wgs1_c5_name":"", "wgs1_c5_addr":"", "wgs1_c5_aips":"", "wgs1_c5_caips":"", "wgs1_c6_enable":"", "wgs1_c6_name":"", "wgs1_c6_addr":"", "wgs1_c6_aips":"", "wgs1_c6_caips":"", "wgs1_c7_enable":"", "wgs1_c7_name":"", "wgs1_c7_addr":"", "wgs1_c7_aips":"", "wgs1_c7_caips":"", "wgs1_c8_enable":"", "wgs1_c8_name":"", "wgs1_c8_addr":"", "wgs1_c8_aips":"", "wgs1_c8_caips":"", "wgs1_c9_enable":"", "wgs1_c9_name":"", "wgs1_c9_addr":"", "wgs1_c9_aips":"", "wgs1_c9_caips":"", "wgs1_c10_enable":"", "wgs1_c10_name":"", "wgs1_c10_addr":"", "wgs1_c10_aips":"", "wgs1_c10_caips":"" }
7 changes: 7 additions & 0 deletions tests/test_data/map_ac1300_stock_386/hook_001.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Result of processing hook_007.content."""

from asusrouter import AsusData
from asusrouter.modules.connection import ConnectionState
from asusrouter.modules.wireguard import AsusWireGuardServer

expected_result = {}
5 changes: 5 additions & 0 deletions tests/tools/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ def test_read_js_variables(content, expected):
(None, {}),
# Test invalid JSON content
("not a json", {}),
# Test missing values
(
'{ , "key1": "value1", , "key2": "value2", }',
{"key1": "value1", "key2": "value2"},
),
],
)
def test_read_json_content(content, expected):
Expand Down

0 comments on commit 9fa2caf

Please sign in to comment.