mirror of
https://github.com/theupdateframework/python-tuf
synced 2026-05-24 10:08:28 +00:00
Fix inconsistent returns in json serializers
Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
This commit is contained in:
parent
326d2af7c4
commit
ab92ba257f
1 changed files with 13 additions and 7 deletions
|
|
@ -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.
|
||||
|
|
@ -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. """
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue