Skip to content

fix: Replace surrogate characters before rendering #7871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion label_studio/core/templatetags/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,20 @@ def get_item(dictionary, key):

@register.filter
def json_dumps_ensure_ascii(dictionary):
return json.dumps(dictionary, ensure_ascii=False)
def clean_surrogates(obj):
"""Recursively clean surrogate characters from strings in nested data structures"""
if isinstance(obj, str):
# Replace surrogate characters with replacement character
return obj.encode('utf-8', errors='replace').decode('utf-8')
elif isinstance(obj, dict):
return {k: clean_surrogates(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [clean_surrogates(item) for item in obj]
else:
return obj

cleaned_dictionary = clean_surrogates(dictionary)
return json.dumps(cleaned_dictionary, ensure_ascii=False)


@register.filter
Expand Down
Loading