summaryrefslogtreecommitdiff
path: root/django/utils/json.py
diff options
context:
space:
mode:
authorJake Howard <git@theorangeone.net>2025-07-17 12:51:09 +0100
committernessita <124304+nessita@users.noreply.github.com>2025-09-16 17:28:32 -0300
commit4289966d1b8e848e5e460b7c782dac009d746b20 (patch)
treeef1d61a33562579d985c762036db5f7aa01406fc /django/utils/json.py
parent218f69f05eb51da1ea17d62a914a67ceff5bfd55 (diff)
Fixed #35859 -- Added background Tasks framework interface.
This work implements what was defined in DEP 14 (https://github.com/django/deps/blob/main/accepted/0014-background-workers.rst). Thanks to Raphael Gaschignard, Eric Holscher, Ran Benita, Sarah Boyce, Jacob Walls, and Natalia Bidart for the reviews.
Diffstat (limited to 'django/utils/json.py')
-rw-r--r--django/utils/json.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/django/utils/json.py b/django/utils/json.py
new file mode 100644
index 0000000000..e7389b70ed
--- /dev/null
+++ b/django/utils/json.py
@@ -0,0 +1,19 @@
+from collections.abc import Mapping, Sequence
+
+
+def normalize_json(obj):
+ """Recursively normalize an object into JSON-compatible types."""
+ match obj:
+ case Mapping():
+ return {normalize_json(k): normalize_json(v) for k, v in obj.items()}
+ case bytes():
+ try:
+ return obj.decode("utf-8")
+ except UnicodeDecodeError:
+ raise ValueError(f"Unsupported value: {type(obj)}")
+ case str() | int() | float() | bool() | None:
+ return obj
+ case Sequence(): # str and bytes were already handled.
+ return [normalize_json(v) for v in obj]
+ case _: # Other types can't be serialized to JSON
+ raise TypeError(f"Unsupported type: {type(obj)}")