1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

Fixed #33779 -- Allowed customizing encoder class in django.utils.html.json_script().

This commit is contained in:
Hrushikesh Vaidya
2022-06-23 14:20:20 +05:30
committed by Mariusz Felisiak
parent 0ee03a439b
commit 72e41a0df6
4 changed files with 27 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
import os
from datetime import datetime
from django.core.serializers.json import DjangoJSONEncoder
from django.test import SimpleTestCase
from django.utils.functional import lazystr
from django.utils.html import (
@@ -211,6 +212,16 @@ class TestUtilsHtml(SimpleTestCase):
with self.subTest(arg=arg):
self.assertEqual(json_script(arg, "test_id"), expected)
def test_json_script_custom_encoder(self):
class CustomDjangoJSONEncoder(DjangoJSONEncoder):
def encode(self, o):
return '{"hello": "world"}'
self.assertHTMLEqual(
json_script({}, encoder=CustomDjangoJSONEncoder),
'<script type="application/json">{"hello": "world"}</script>',
)
def test_json_script_without_id(self):
self.assertHTMLEqual(
json_script({"key": "value"}),