mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Optimize find bad JSON data (#31963)
This commit is contained in:
parent
a745134128
commit
9f830964ef
@ -4,7 +4,7 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
from typing import Dict, List, Optional, Type, Union
|
from typing import Any, Dict, List, Optional, Type, Union
|
||||||
|
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ def save_json(
|
|||||||
_LOGGER.error("JSON replacement cleanup failed: %s", err)
|
_LOGGER.error("JSON replacement cleanup failed: %s", err)
|
||||||
|
|
||||||
|
|
||||||
def find_paths_unserializable_data(bad_data: Union[List, Dict]) -> List[str]:
|
def find_paths_unserializable_data(bad_data: Any) -> List[str]:
|
||||||
"""Find the paths to unserializable data.
|
"""Find the paths to unserializable data.
|
||||||
|
|
||||||
This method is slow! Only use for error handling.
|
This method is slow! Only use for error handling.
|
||||||
@ -98,9 +98,9 @@ def find_paths_unserializable_data(bad_data: Union[List, Dict]) -> List[str]:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
json.dumps(obj)
|
json.dumps(obj)
|
||||||
valid = True
|
continue
|
||||||
except TypeError:
|
except TypeError:
|
||||||
valid = False
|
pass
|
||||||
|
|
||||||
if isinstance(obj, dict):
|
if isinstance(obj, dict):
|
||||||
for key, value in obj.items():
|
for key, value in obj.items():
|
||||||
@ -115,7 +115,7 @@ def find_paths_unserializable_data(bad_data: Union[List, Dict]) -> List[str]:
|
|||||||
elif isinstance(obj, list):
|
elif isinstance(obj, list):
|
||||||
for idx, value in enumerate(obj):
|
for idx, value in enumerate(obj):
|
||||||
to_process.append((value, f"{obj_path}[{idx}]"))
|
to_process.append((value, f"{obj_path}[{idx}]"))
|
||||||
elif not valid: # type: ignore
|
else:
|
||||||
invalid.append(obj_path)
|
invalid.append(obj_path)
|
||||||
|
|
||||||
return invalid
|
return invalid
|
||||||
|
Loading…
x
Reference in New Issue
Block a user