diff options
author | Richard van der Hoff <1389908+richvdh@users.noreply.github.com> | 2020-10-28 20:41:42 +0000 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2020-10-30 15:24:10 +0000 |
commit | 90c900a8ff12cf330bb2010f13f3dfeb42930f41 (patch) | |
tree | 70035405751a623c34852dc091b7a420c04c7fe0 /synapse/util/__init__.py | |
parent | Tie together matches_user_in_member_list and get_users_in_room caches (#8676) (diff) | |
download | synapse-90c900a8ff12cf330bb2010f13f3dfeb42930f41.tar.xz |
Merge pull request #8678 from matrix-org/rav/fix_frozen_events
Fix serialisation errors when using third-party event rules.
Diffstat (limited to 'synapse/util/__init__.py')
-rw-r--r-- | synapse/util/__init__.py | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py index d55b93d763..517686f0a6 100644 --- a/synapse/util/__init__.py +++ b/synapse/util/__init__.py @@ -18,6 +18,7 @@ import logging import re import attr +from frozendict import frozendict from twisted.internet import defer, task @@ -31,9 +32,26 @@ def _reject_invalid_json(val): raise ValueError("Invalid JSON value: '%s'" % val) -# Create a custom encoder to reduce the whitespace produced by JSON encoding and -# ensure that valid JSON is produced. -json_encoder = json.JSONEncoder(allow_nan=False, separators=(",", ":")) +def _handle_frozendict(obj): + """Helper for json_encoder. Makes frozendicts serializable by returning + the underlying dict + """ + if type(obj) is frozendict: + # fishing the protected dict out of the object is a bit nasty, + # but we don't really want the overhead of copying the dict. + return obj._dict + raise TypeError( + "Object of type %s is not JSON serializable" % obj.__class__.__name__ + ) + + +# A custom JSON encoder which: +# * handles frozendicts +# * produces valid JSON (no NaNs etc) +# * reduces redundant whitespace +json_encoder = json.JSONEncoder( + allow_nan=False, separators=(",", ":"), default=_handle_frozendict +) # Create a custom decoder to reject Python extensions to JSON. json_decoder = json.JSONDecoder(parse_constant=_reject_invalid_json) |