-
Notifications
You must be signed in to change notification settings - Fork 49
Add JsonConverter classes for Date and Time to support System.Text.Json deserialization #641
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
Open
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/fix-json-deserialization-date-time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+352
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
779feeb
Initial plan
Copilot 45dcd35
Add JsonConstructor attributes and tests for Date and Time deserializ…
Copilot f7cf683
Merge branch 'main' into copilot/fix-json-deserialization-date-time
gavinbarron 09fab74
Run dotnet format to fix whitespace formatting
Copilot aa05ced
Change approach: use JsonConverter instead of JsonConstructor attribute
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Globalization; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.Kiota.Abstractions; | ||
|
|
||
| namespace Microsoft.Kiota.Serialization.Json; | ||
|
|
||
| /// <summary> | ||
| /// Converts a Date object or value to/from JSON. | ||
| /// </summary> | ||
| public class DateJsonConverter : JsonConverter<Date> | ||
| { | ||
| /// <inheritdoc /> | ||
| public override Date Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if(reader.TokenType == JsonTokenType.Null) | ||
| return default; | ||
|
|
||
| if(reader.TokenType == JsonTokenType.String) | ||
| { | ||
| var stringValue = reader.GetString(); | ||
| if(string.IsNullOrEmpty(stringValue)) | ||
| return default; | ||
|
|
||
| if(DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var dateTime)) | ||
| return new Date(dateTime); | ||
|
|
||
| throw new JsonException($"Unable to parse '{stringValue}' as a Date."); | ||
| } | ||
|
|
||
| throw new JsonException($"Unexpected token type '{reader.TokenType}' when reading Date."); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Write(Utf8JsonWriter writer, Date value, JsonSerializerOptions options) | ||
| { | ||
| writer.WriteStringValue(value.ToString()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Globalization; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.Kiota.Abstractions; | ||
|
|
||
| namespace Microsoft.Kiota.Serialization.Json; | ||
|
|
||
| /// <summary> | ||
| /// Converts a Time object or value to/from JSON. | ||
| /// </summary> | ||
| public class TimeJsonConverter : JsonConverter<Time> | ||
| { | ||
| /// <inheritdoc /> | ||
| public override Time Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if(reader.TokenType == JsonTokenType.Null) | ||
| return default; | ||
|
|
||
| if(reader.TokenType == JsonTokenType.String) | ||
| { | ||
| var stringValue = reader.GetString(); | ||
| if(string.IsNullOrEmpty(stringValue)) | ||
| return default; | ||
|
|
||
| if(DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var dateTime)) | ||
| return new Time(dateTime); | ||
|
|
||
| throw new JsonException($"Unable to parse '{stringValue}' as a Time."); | ||
| } | ||
|
|
||
| throw new JsonException($"Unexpected token type '{reader.TokenType}' when reading Time."); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Write(Utf8JsonWriter writer, Time value, JsonSerializerOptions options) | ||
| { | ||
| writer.WriteStringValue(value.ToString()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Text.Json; | ||
| using Microsoft.Kiota.Abstractions; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Kiota.Serialization.Json.Tests; | ||
|
|
||
| public class DateJsonConverterTests | ||
| { | ||
| private readonly JsonSerializerOptions _options; | ||
|
|
||
| public DateJsonConverterTests() | ||
| { | ||
| _options = new JsonSerializerOptions(); | ||
| _options.Converters.Add(new DateJsonConverter()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SerializesDateAsString() | ||
| { | ||
| var date = new Date(2025, 10, 24); | ||
| var json = JsonSerializer.Serialize(date, _options); | ||
|
|
||
| Assert.Equal("\"2025-10-24\"", json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DeserializesDateFromString() | ||
| { | ||
| var json = "\"2025-10-24\""; | ||
| var date = JsonSerializer.Deserialize<Date>(json, _options); | ||
|
|
||
| Assert.Equal(2025, date.Year); | ||
| Assert.Equal(10, date.Month); | ||
| Assert.Equal(24, date.Day); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RoundTripSerialization() | ||
| { | ||
| var original = new Date(2025, 10, 24); | ||
| var json = JsonSerializer.Serialize(original, _options); | ||
| var deserialized = JsonSerializer.Deserialize<Date>(json, _options); | ||
|
|
||
| Assert.Equal(original.Year, deserialized.Year); | ||
| Assert.Equal(original.Month, deserialized.Month); | ||
| Assert.Equal(original.Day, deserialized.Day); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DeserializesNullToDefault() | ||
| { | ||
| var json = "null"; | ||
| var date = JsonSerializer.Deserialize<Date>(json, _options); | ||
|
|
||
| Assert.Equal(default(Date), date); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DeserializesEmptyStringToDefault() | ||
| { | ||
| var json = "\"\""; | ||
| var date = JsonSerializer.Deserialize<Date>(json, _options); | ||
|
|
||
| Assert.Equal(default(Date), date); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ThrowsOnInvalidFormat() | ||
| { | ||
| var json = "\"not-a-date\""; | ||
|
|
||
| Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Date>(json, _options)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ThrowsOnUnexpectedTokenType() | ||
| { | ||
| var json = "123"; | ||
|
|
||
| Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Date>(json, _options)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WorksWithDefaultOptionsWithConverters() | ||
| { | ||
| var date = new Date(2025, 10, 24); | ||
| var json = JsonSerializer.Serialize(date, KiotaJsonSerializationContext.DefaultOptionsWithConverters); | ||
| var deserialized = JsonSerializer.Deserialize<Date>(json, KiotaJsonSerializationContext.DefaultOptionsWithConverters); | ||
|
|
||
| Assert.Equal(date.Year, deserialized.Year); | ||
| Assert.Equal(date.Month, deserialized.Month); | ||
| Assert.Equal(date.Day, deserialized.Day); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Text.Json; | ||
| using Microsoft.Kiota.Abstractions; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Kiota.Serialization.Json.Tests; | ||
|
|
||
| public class TimeJsonConverterTests | ||
| { | ||
| private readonly JsonSerializerOptions _options; | ||
|
|
||
| public TimeJsonConverterTests() | ||
| { | ||
| _options = new JsonSerializerOptions(); | ||
| _options.Converters.Add(new TimeJsonConverter()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SerializesTimeAsString() | ||
| { | ||
| var time = new Time(10, 18, 54); | ||
| var json = JsonSerializer.Serialize(time, _options); | ||
|
|
||
| Assert.Equal("\"10:18:54\"", json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DeserializesTimeFromString() | ||
| { | ||
| var json = "\"10:18:54\""; | ||
| var time = JsonSerializer.Deserialize<Time>(json, _options); | ||
|
|
||
| Assert.Equal(10, time.Hour); | ||
| Assert.Equal(18, time.Minute); | ||
| Assert.Equal(54, time.Second); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RoundTripSerialization() | ||
| { | ||
| var original = new Time(10, 18, 54); | ||
| var json = JsonSerializer.Serialize(original, _options); | ||
| var deserialized = JsonSerializer.Deserialize<Time>(json, _options); | ||
|
|
||
| Assert.Equal(original.Hour, deserialized.Hour); | ||
| Assert.Equal(original.Minute, deserialized.Minute); | ||
| Assert.Equal(original.Second, deserialized.Second); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DeserializesNullToDefault() | ||
| { | ||
| var json = "null"; | ||
| var time = JsonSerializer.Deserialize<Time>(json, _options); | ||
|
|
||
| Assert.Equal(default(Time), time); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DeserializesEmptyStringToDefault() | ||
| { | ||
| var json = "\"\""; | ||
| var time = JsonSerializer.Deserialize<Time>(json, _options); | ||
|
|
||
| Assert.Equal(default(Time), time); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ThrowsOnInvalidFormat() | ||
| { | ||
| var json = "\"not-a-time\""; | ||
|
|
||
| Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Time>(json, _options)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ThrowsOnUnexpectedTokenType() | ||
| { | ||
| var json = "123"; | ||
|
|
||
| Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Time>(json, _options)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WorksWithDefaultOptionsWithConverters() | ||
| { | ||
| var time = new Time(10, 18, 54); | ||
| var json = JsonSerializer.Serialize(time, KiotaJsonSerializationContext.DefaultOptionsWithConverters); | ||
| var deserialized = JsonSerializer.Deserialize<Time>(json, KiotaJsonSerializationContext.DefaultOptionsWithConverters); | ||
|
|
||
| Assert.Equal(time.Hour, deserialized.Hour); | ||
| Assert.Equal(time.Minute, deserialized.Minute); | ||
| Assert.Equal(time.Second, deserialized.Second); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious: should these attributes be updated to point to the converters as well?