Skip to content

Commit

Permalink
Refactor AzureJSONEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Codejune committed Oct 3, 2021
1 parent c1c84eb commit 586464f
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions sdk/core/azure-core/azure/core/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,19 @@ def _timedelta_as_isostr(value):

return "P" + date + time


def _timestr_as_isostr(o):
# First try datetime.datetime
if hasattr(o, "year") and hasattr(o, "hour"):
# astimezone() fails for naive times in Python 2.7, so make make sure o is aware (tzinfo is set)
if not o.tzinfo:
iso_formatted = o.replace(tzinfo=TZ_UTC).isoformat()
else:
iso_formatted = o.astimezone(TZ_UTC).isoformat()
# Replace the trailing "+00:00" UTC offset with "Z" (RFC 3339: https://www.ietf.org/rfc/rfc3339.txt)
return iso_formatted.replace("+00:00", "Z")
# Next try datetime.date or datetime.time
return o.isoformat()

try:
from datetime import timezone

Expand All @@ -95,29 +107,16 @@ class AzureJSONEncoder(JSONEncoder):
"""A JSON encoder that's capable of serializing datetime objects and bytes."""

def default(self, o): # pylint: disable=too-many-return-statements
if isinstance(o, (bytes, bytearray)):
return base64.b64encode(o).decode()
try:
return _timestr_as_isostr(o)
except AttributeError:
pass
# Last, try datetime.timedelta
try:
return super(AzureJSONEncoder, self).default(o)
except TypeError:
if isinstance(o, (bytes, bytearray)):
return base64.b64encode(o).decode()
try:
# First try datetime.datetime
if hasattr(o, "year") and hasattr(o, "hour"):
# astimezone() fails for naive times in Python 2.7, so make make sure o is aware (tzinfo is set)
if not o.tzinfo:
iso_formatted = o.replace(tzinfo=TZ_UTC).isoformat()
else:
iso_formatted = o.astimezone(TZ_UTC).isoformat()
# Replace the trailing "+00:00" UTC offset with "Z" (RFC 3339: https://www.ietf.org/rfc/rfc3339.txt)
return iso_formatted.replace("+00:00", "Z")
# Next try datetime.date or datetime.time
return o.isoformat()
except AttributeError:
pass
# Last, try datetime.timedelta
try:
return _timedelta_as_isostr(o)
except AttributeError:
# This will be raised when it hits value.total_seconds in the method above
pass
return super(AzureJSONEncoder, self).default(o)
return _timedelta_as_isostr(o)
except AttributeError:
# This will be raised when it hits value.total_seconds in the method above
pass
return super(AzureJSONEncoder, self).default(o)

0 comments on commit 586464f

Please sign in to comment.