Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit d24e8d2

Browse files
author
David Robertson
committed
Use new canonicaljson serisalistion hook
From matrix-org/python-canonicaljson#59
1 parent 3537e5c commit d24e8d2

File tree

3 files changed

+20
-90
lines changed

3 files changed

+20
-90
lines changed

poetry.lock

+4-82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,8 @@ jsonschema = ">=3.0.0"
158158
immutabledict = ">=2.0"
159159
# We require 2.1.0 or higher for type hints. Previous guard was >= 1.1.0
160160
unpaddedbase64 = ">=2.1.0"
161-
# We require 1.5.0 to work around an issue when running against the C implementation of
162-
# frozendict: https://github.com/matrix-org/python-canonicaljson/issues/36
163-
canonicaljson = "^1.5.0"
161+
# We require 2.0.0 for immutabledict support.
162+
canonicaljson = "^2.0.0"
164163
# we use the type definitions added in signedjson 1.1.
165164
signedjson = "^1.1.0"
166165
# validating SSL certs for IP addresses requires service_identity 18.1.

synapse/__init__.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
""" This is an implementation of a Matrix homeserver.
1818
"""
1919

20-
import json
2120
import os
2221
import sys
22+
from typing import Any, Dict
2323

2424
from synapse.util.rust import check_rust_lib_up_to_date
2525
from synapse.util.stringutils import strtobool
@@ -61,11 +61,20 @@
6161
except ImportError:
6262
pass
6363

64-
# Use the standard library json implementation instead of simplejson.
64+
# Teach canonicaljson how to serialise immutabledicts.
6565
try:
66-
from canonicaljson import set_json_library
67-
68-
set_json_library(json)
66+
from canonicaljson import register_preserialisation_callback
67+
from immutabledict import immutabledict
68+
69+
def _immutabledict_cb(d: immutabledict) -> Dict[str, Any]:
70+
try:
71+
return d._dict
72+
except Exception:
73+
# Paranoia: fall back to a `dict()` call, in case a future version of
74+
# immutabledict removes `_dict` from the implementation.
75+
return dict(d)
76+
77+
register_preserialisation_callback(immutabledict, _immutabledict_cb)
6978
except ImportError:
7079
pass
7180

0 commit comments

Comments
 (0)