Skip to content

Commit

Permalink
fix: JSON decode error (#76)
Browse files Browse the repository at this point in the history
* fix: JSON decode error
  • Loading branch information
lxndrblz authored Jun 30, 2024
1 parent 23d24aa commit c4abb26
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/forensicsim/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ def write_results_to_json(data: list[dict[str, Any]], outputpath: Path) -> None:
# Dump messages into a json file
try:
with open(outputpath, "w", encoding="utf-8") as f:
json.dump(
data, f, indent=4, sort_keys=True, default=str, ensure_ascii=False
)
json.dump(data, f, indent=4, default=str, ensure_ascii=False)
except OSError as e:
print(e)
28 changes: 18 additions & 10 deletions src/forensicsim/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import warnings
from dataclasses import dataclass, field
from datetime import datetime
from json import JSONDecodeError
from pathlib import Path
from typing import Any, Optional, Union

Expand All @@ -26,15 +27,22 @@ def strip_html_tags(value: str) -> str:


def decode_dict(properties: Union[bytes, str, dict]) -> dict[str, Any]:
if isinstance(properties, bytes):
soup = BeautifulSoup(properties, features="html.parser")
properties = properties.decode(soup.original_encoding)
if isinstance(properties, dict):
# handle case where nested childs are dicts or list but provided with "" but have to be expanded.
for key, value in properties.items():
if isinstance(value, str) and value.startswith(("[", "{")):
properties[key] = json.loads(value, strict=False)
return properties
try:
if isinstance(properties, bytes):
soup = BeautifulSoup(properties, features="html.parser")
properties = properties.decode(
encoding=soup.original_encoding, errors="ignore"
)
if isinstance(properties, dict):
# handle case where nested childs are dicts or list but provided with "" but have to be expanded.
for key, value in properties.items():
if isinstance(value, str) and value.startswith(("[", "{")):
properties[key] = json.loads(value, strict=False)
return properties
except JSONDecodeError as e:
print(e)
print("Couldn't decode dictionary ", properties)
return {}

return json.loads(properties, strict=False)

Expand Down Expand Up @@ -292,7 +300,7 @@ def _parse_reply_chains(reply_chains: list[dict], version: str) -> set[Message]:
elif version == "v2":
rc |= {"cached_deduplication_key": md.get("dedupeKey")}
rc |= {"clientmessageid": md.get("clientMessageId")}
# set to clientArrivalTime as compose Time is no longer present
# set to clientArrivalTime as compose time is no longer present
rc |= {"composetime": md.get("clientArrivalTime")}
rc |= {"contenttype": md.get("contentType")}
# set to clientArrivalTime as created time is no longer present
Expand Down

0 comments on commit c4abb26

Please sign in to comment.