Improve JSON data sanitization to handle tuples and dict keys
- Sanitize dictionary keys - Preserve tuple types - Handle nested structures better
This commit is contained in:
parent
5885637ebf
commit
abeaac84fa
1 changed files with 19 additions and 4 deletions
|
|
@ -961,19 +961,34 @@ def _sanitize_string_for_json(text: str) -> str:
|
||||||
def _sanitize_json_data(data: Any) -> Any:
|
def _sanitize_json_data(data: Any) -> Any:
|
||||||
"""Recursively sanitize all string values in data structure for safe UTF-8 encoding
|
"""Recursively sanitize all string values in data structure for safe UTF-8 encoding
|
||||||
|
|
||||||
|
Handles all JSON-serializable types including:
|
||||||
|
- Dictionary keys and values
|
||||||
|
- Lists and tuples (preserves type)
|
||||||
|
- Nested structures
|
||||||
|
- Strings at any level
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
data: Data to sanitize (dict, list, str, or other types)
|
data: Data to sanitize (dict, list, tuple, str, or other types)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Sanitized data with all strings cleaned of problematic characters
|
Sanitized data with all strings cleaned of problematic characters
|
||||||
"""
|
"""
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
return {k: _sanitize_json_data(v) for k, v in data.items()}
|
# Sanitize both keys and values
|
||||||
elif isinstance(data, list):
|
return {
|
||||||
return [_sanitize_json_data(item) for item in data]
|
_sanitize_string_for_json(k)
|
||||||
|
if isinstance(k, str)
|
||||||
|
else k: _sanitize_json_data(v)
|
||||||
|
for k, v in data.items()
|
||||||
|
}
|
||||||
|
elif isinstance(data, (list, tuple)):
|
||||||
|
# Handle both lists and tuples, preserve original type
|
||||||
|
sanitized = [_sanitize_json_data(item) for item in data]
|
||||||
|
return type(data)(sanitized)
|
||||||
elif isinstance(data, str):
|
elif isinstance(data, str):
|
||||||
return _sanitize_string_for_json(data)
|
return _sanitize_string_for_json(data)
|
||||||
else:
|
else:
|
||||||
|
# Numbers, booleans, None, etc. - return as-is
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue