summary refs log tree commit diff
path: root/synapse/__init__.py
diff options
context:
space:
mode:
authorDavid Robertson <davidr@element.io>2023-03-22 17:15:34 +0000
committerGitHub <noreply@github.com>2023-03-22 17:15:34 +0000
commit3b0083c92adf76daf4161908565de9e5efc08074 (patch)
treecc8e3883ec269a1153cd40b42eee5f737d906411 /synapse/__init__.py
parentMerge branch 'release-v1.80' into develop (diff)
downloadsynapse-3b0083c92adf76daf4161908565de9e5efc08074.tar.xz
Use immutabledict instead of frozendict (#15113)
Additionally:

* Consistently use `freeze()` in test

---------

Co-authored-by: Patrick Cloke <clokep@users.noreply.github.com>
Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'synapse/__init__.py')
-rw-r--r--synapse/__init__.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/synapse/__init__.py b/synapse/__init__.py
index a203ed533a..b97ee59f15 100644
--- a/synapse/__init__.py
+++ b/synapse/__init__.py
@@ -17,9 +17,9 @@
 """ This is an implementation of a Matrix homeserver.
 """
 
-import json
 import os
 import sys
+from typing import Any, Dict
 
 from synapse.util.rust import check_rust_lib_up_to_date
 from synapse.util.stringutils import strtobool
@@ -61,11 +61,20 @@ try:
 except ImportError:
     pass
 
-# Use the standard library json implementation instead of simplejson.
+# Teach canonicaljson how to serialise immutabledicts.
 try:
-    from canonicaljson import set_json_library
-
-    set_json_library(json)
+    from canonicaljson import register_preserialisation_callback
+    from immutabledict import immutabledict
+
+    def _immutabledict_cb(d: immutabledict) -> Dict[str, Any]:
+        try:
+            return d._dict
+        except Exception:
+            # Paranoia: fall back to a `dict()` call, in case a future version of
+            # immutabledict removes `_dict` from the implementation.
+            return dict(d)
+
+    register_preserialisation_callback(immutabledict, _immutabledict_cb)
 except ImportError:
     pass