Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom JSONEncoder for model serialization #19595

Merged
merged 11 commits into from
Aug 4, 2021
Merged

Conversation

mccoyp
Copy link
Member

@mccoyp mccoyp commented Jun 30, 2021

This adds a custom JSONEncoder that can serialize datetime objects (dates, times, datetimes, and timedeltas) in UTC ISO 8601 format, as well as bytes and bytearrays in base64 strings.

@mccoyp mccoyp added Client This issue points to a problem in the data-plane of the library. Azure.Core labels Jun 30, 2021
@mccoyp mccoyp force-pushed the serialize-pr branch 2 times, most recently from e4e71cd to 6ec7641 Compare July 8, 2021 01:04
@mccoyp mccoyp marked this pull request as ready for review July 8, 2021 01:59
@mccoyp mccoyp requested a review from xiangyan99 as a code owner July 8, 2021 01:59
return 'P' + date + time


class UTC(datetime.tzinfo):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mccoyp mccoyp requested a review from xiangyan99 July 9, 2021 01:46
@xiangyan99
Copy link
Member

/azp run python - core - ci

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@xiangyan99
Copy link
Member

@mccoyp _utils.py has been moved into utils folder.

sdk/core/azure-core/azure/core/serialization.py Outdated Show resolved Hide resolved
sdk/core/azure-core/dev_requirements.txt Outdated Show resolved Hide resolved
"timedelta": timedelta(1),
"date": date(2021, 5, 12),
"datetime": isodate.parse_datetime('2012-02-24T00:53:52.780Z'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the conversation starts regarding deserialization - we will want to make sure we support weird .NET timestamps (with extra decimal places). When that conversation happens - what we do to serialize round-tripped values will be important as well.

import datetime
from json import JSONEncoder

from .utils._utils import _FixedOffset
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to import a private type from a private submodule?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rename _FixedOffset to FixedOffset. But not sure if it is worth...

@mccoyp mccoyp requested review from annatisch and johanste July 17, 2021 01:56
@iscai-msft
Copy link
Contributor

/azp run python - autorest - pr

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

# Remove trailing zeros
seconds_string = seconds_string.rstrip("0")
except AttributeError: # int.is_integer() raises on Python 2.7
seconds_string = "{:02}".format(seconds)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still need int() around seconds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't think so, since this AttributeError should only come up if seconds is an integer. seconds.is_integer() works when seconds is a float on 2.7. 3.6, and 3.9 when I test locally



NULL = _Null()
"""
A falsy sentinel object which is supposed to be used to specify attributes
with no data. This gets serialized to `null` on the wire.
"""


def timedelta_as_isostr(value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently part of the public API - which I don't think it should be, at least to start with.
Can we make it private?

@mccoyp mccoyp requested a review from annatisch July 26, 2021 21:19
@annatisch
Copy link
Member

/azp run python - core - ci

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mccoyp mccoyp requested a review from annatisch August 2, 2021 18:12
@mccoyp
Copy link
Member Author

mccoyp commented Aug 3, 2021

/azp run python - core - ci

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mccoyp
Copy link
Member Author

mccoyp commented Aug 4, 2021

/azp run python - core - ci

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mccoyp mccoyp enabled auto-merge (squash) August 4, 2021 01:33
@mccoyp mccoyp merged commit 6585a10 into Azure:main Aug 4, 2021
@mccoyp mccoyp deleted the serialize-pr branch August 4, 2021 03:45
azure-sdk pushed a commit to azure-sdk/azure-sdk-for-python that referenced this pull request Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Core Client This issue points to a problem in the data-plane of the library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants