summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorHrushikesh Vaidya <hrushikeshrv@gmail.com>2022-06-23 14:20:20 +0530
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-06-28 10:54:38 +0200
commit72e41a0df6db23410135364223eeda83ac2a8b27 (patch)
tree4c173f6eae2977140cf30bb1f1c10ab9c623c044 /django/utils
parent0ee03a439b7016c068ab2e0c477d5c84f750a82c (diff)
Fixed #33779 -- Allowed customizing encoder class in django.utils.html.json_script().
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/html.py6
1 files changed, 4 insertions, 2 deletions
diff --git a/django/utils/html.py b/django/utils/html.py
index d0bc97be80..007602a14a 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -59,7 +59,7 @@ _json_script_escapes = {
}
-def json_script(value, element_id=None):
+def json_script(value, element_id=None, encoder=None):
"""
Escape all the HTML/XML special characters with their unicode escapes, so
value is safe to be output anywhere except for inside a tag attribute. Wrap
@@ -67,7 +67,9 @@ def json_script(value, element_id=None):
"""
from django.core.serializers.json import DjangoJSONEncoder
- json_str = json.dumps(value, cls=DjangoJSONEncoder).translate(_json_script_escapes)
+ json_str = json.dumps(value, cls=encoder or DjangoJSONEncoder).translate(
+ _json_script_escapes
+ )
if element_id:
template = '<script id="{}" type="application/json">{}</script>'
args = (element_id, mark_safe(json_str))