1 files changed, 16 insertions, 2 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py
index c63256d3bd..d55b93d763 100644
--- a/synapse/util/__init__.py
+++ b/synapse/util/__init__.py
@@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import json
import logging
import re
@@ -25,14 +26,27 @@ from synapse.logging import context
logger = logging.getLogger(__name__)
+def _reject_invalid_json(val):
+ """Do not allow Infinity, -Infinity, or NaN values in JSON."""
+ 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=(",", ":"))
+
+# Create a custom decoder to reject Python extensions to JSON.
+json_decoder = json.JSONDecoder(parse_constant=_reject_invalid_json)
+
+
def unwrapFirstError(failure):
# defer.gatherResults and DeferredLists wrap failures.
failure.trap(defer.FirstError)
return failure.value.subFailure
-@attr.s
-class Clock(object):
+@attr.s(slots=True)
+class Clock:
"""
A Clock wraps a Twisted reactor and provides utilities on top of it.
|