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

Commit

Permalink
Add a helper from decoding bytes, then json.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Jul 9, 2020
1 parent 1f0fe4d commit f792456
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
13 changes: 3 additions & 10 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
from http import HTTPStatus
from typing import Dict, List

from canonicaljson import json

from twisted.web import http

from synapse.util.json import load_bytes

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -572,15 +572,8 @@ def to_synapse_error(self):
"""
# try to parse the body as json, to get better errcode/msg, but
# default to M_UNKNOWN with the HTTP status as the error text

# Decode to Unicode since json on Python 3.5 requires str, not bytes.
try:
response_unicode = self.response.decode("utf8")
except UnicodeDecodeError:
response_unicode = "{}"

try:
j = json.loads(response_unicode)
j = load_bytes(self.response)
except ValueError:
j = {}

Expand Down
12 changes: 2 additions & 10 deletions synapse/http/servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

import logging

from canonicaljson import json

from synapse.api.errors import Codes, SynapseError
from synapse.util.json import load_bytes

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -214,15 +213,8 @@ def parse_json_value_from_request(request, allow_empty_body=False):
if not content_bytes and allow_empty_body:
return None

# Decode to Unicode since json on Python 3.5 requires str, not bytes.
try:
content_unicode = content_bytes.decode("utf8")
except UnicodeDecodeError:
logger.warning("Unable to decode UTF-8")
raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)

try:
content = json.loads(content_unicode)
content = load_bytes(content_bytes)
except Exception as e:
logger.warning("Unable to parse JSON: %s", e)
raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)
Expand Down
40 changes: 40 additions & 0 deletions synapse/util/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2020 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import typing

# Guard against an import loop from synapse.types -> synapse.api.errors -> here.
if typing.TYPE_CHECKING:
from synapse.types import JsonDict

logger = logging.getLogger(__name__)


def load_bytes(s: bytes) -> "JsonDict":
"""
Deserialize a bytes containing a JSON document to a Python object.
This is a compatibility shim since Python 3.5 does NOT support deserializing
bytes.
Note that this does not do error handling.
Raises:
UnicodeDecodeError if the bytes cannot be converted to UTF-8.
JSONDecodeError if the data is not a valid JSON document.
"""
# Decode to Unicode since json on Python 3.5 requires str, not bytes.
s_str = s.decode("utf8")
return json.loads(s_str)

0 comments on commit f792456

Please sign in to comment.