Skip to content

feat: Honor field.encoder in prettify_json #1274

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: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/unfold/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _get_contents(self) -> str:
):
result_repr = self.get_admin_url(f.remote_field, value)
elif isinstance(f, models.JSONField):
formatted_output = prettify_json(value)
formatted_output = prettify_json(value, f.encoder)

if formatted_output:
return formatted_output
Expand Down
4 changes: 2 additions & 2 deletions src/unfold/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def hex_to_rgb(hex_color: str) -> list[int]:
return (r, g, b)


def prettify_json(data: Any) -> Optional[str]:
def prettify_json(data: Any, encoder: Any) -> Optional[str]:
try:
from pygments import highlight
from pygments.formatters import HtmlFormatter
Expand All @@ -167,7 +167,7 @@ def format_response(response: str, theme: str) -> str:
)
return highlight(response, JsonLexer(), formatter)

response = json.dumps(data, sort_keys=True, indent=4)
response = json.dumps(data, sort_keys=True, indent=4, cls=encoder)

return mark_safe(
f'<div class="block dark:hidden">{format_response(response, "colorful")}</div>'
Expand Down