Skip to content

Commit

Permalink
Fix inconsistent returns in json serializers
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
  • Loading branch information
lukpueh committed Mar 5, 2021
1 parent 17c25e2 commit 8be8ec1
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions tuf/api/serialization/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ def deserialize(self, raw_data: bytes) -> Metadata:
"""Deserialize utf-8 encoded JSON bytes into Metadata object. """
try:
json_dict = json.loads(raw_data.decode("utf-8"))
return Metadata.from_dict(json_dict)
metadata_obj = Metadata.from_dict(json_dict)

except Exception as e: # pylint: disable=broad-except
six.raise_from(DeserializationError, e)

return metadata_obj


class JSONSerializer(MetadataSerializer):
"""A Metadata-to-JSON serialize method.
Expand All @@ -54,15 +56,17 @@ def serialize(self, metadata_obj: Metadata) -> bytes:
"""Serialize Metadata object into utf-8 encoded JSON bytes. """
try:
indent = (None if self.compact else 1)
separators=((',', ':') if self.compact else (',', ': '))
return json.dumps(metadata_obj.to_dict(),
indent=indent,
separators=separators,
sort_keys=True).encode("utf-8")
separators = ((',', ':') if self.compact else (',', ': '))
json_bytes = json.dumps(metadata_obj.to_dict(),
indent=indent,
separators=separators,
sort_keys=True).encode("utf-8")

except Exception as e: # pylint: disable=broad-except
six.raise_from(SerializationError, e)

return json_bytes


class CanonicalJSONSerializer(SignedSerializer):
"""A Signed-to-Canonical JSON 'serialize' method. """
Expand All @@ -71,7 +75,9 @@ def serialize(self, signed_obj: Signed) -> bytes:
"""Serialize Signed object into utf-8 encoded Canonical JSON bytes. """
try:
signed_dict = signed_obj.to_dict()
return encode_canonical(signed_dict).encode("utf-8")
canonical_bytes = encode_canonical(signed_dict).encode("utf-8")

except Exception as e: # pylint: disable=broad-except
six.raise_from(SerializationError, e)

return canonical_bytes

0 comments on commit 8be8ec1

Please sign in to comment.