Skip to content

Commit 3289774

Browse files
authored
feat: Honor field.encoder in prettify_json (#1274)
When users provide a custom json encoder in their field configuration, this configuration should also be honoured while pretty printing the field values.
1 parent 491d5d3 commit 3289774

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/unfold/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def _get_contents(self) -> str:
140140
):
141141
result_repr = self.get_admin_url(f.remote_field, value)
142142
elif isinstance(f, models.JSONField):
143-
formatted_output = prettify_json(value)
143+
formatted_output = prettify_json(value, f.encoder)
144144

145145
if formatted_output:
146146
return formatted_output

src/unfold/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def hex_to_rgb(hex_color: str) -> list[int]:
150150
return (r, g, b)
151151

152152

153-
def prettify_json(data: Any) -> Optional[str]:
153+
def prettify_json(data: Any, encoder: Any) -> Optional[str]:
154154
try:
155155
from pygments import highlight
156156
from pygments.formatters import HtmlFormatter
@@ -167,7 +167,7 @@ def format_response(response: str, theme: str) -> str:
167167
)
168168
return highlight(response, JsonLexer(), formatter)
169169

170-
response = json.dumps(data, sort_keys=True, indent=4)
170+
response = json.dumps(data, sort_keys=True, indent=4, cls=encoder)
171171

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

0 commit comments

Comments
 (0)