summary refs log tree commit diff
path: root/synapse/util/frozenutils.py
diff options
context:
space:
mode:
authorNeil Johnson <neil@fragile.org.uk>2018-04-11 10:59:00 +0100
committerNeil Johnson <neil@fragile.org.uk>2018-04-11 10:59:00 +0100
commit427e6c4059e2eff5eacf114dc6708e2972ca36d2 (patch)
tree608df478fe94531db9a789e3c8d1e827a34d8302 /synapse/util/frozenutils.py
parentUpdate README.rst (diff)
parentbump version/changelog (diff)
downloadsynapse-427e6c4059e2eff5eacf114dc6708e2972ca36d2.tar.xz
Merge branch 'release-v0.27.0' of https://github.com/matrix-org/synapse v0.27.3
Diffstat (limited to 'synapse/util/frozenutils.py')
-rw-r--r--synapse/util/frozenutils.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/synapse/util/frozenutils.py b/synapse/util/frozenutils.py
index 6322f0f55c..f497b51f4a 100644
--- a/synapse/util/frozenutils.py
+++ b/synapse/util/frozenutils.py
@@ -14,6 +14,7 @@
 # limitations under the License.
 
 from frozendict import frozendict
+import simplejson as json
 
 
 def freeze(o):
@@ -49,3 +50,21 @@ def unfreeze(o):
         pass
 
     return o
+
+
+def _handle_frozendict(obj):
+    """Helper for EventEncoder. 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 JSONEncoder which is capable of encoding frozendics without barfing
+frozendict_json_encoder = json.JSONEncoder(
+    default=_handle_frozendict,
+)