-
Notifications
You must be signed in to change notification settings - Fork 53
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
base: main
Are you sure you want to change the base?
Changes from all commits
779feeb
45dcd35
f7cf683
09fab74
aa05ced
7ffc275
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> | ||
|
Check failure on line 16 in src/serialization/json/DateJsonConverter.cs
|
||
| { | ||
| /// <inheritdoc /> | ||
| public override Date Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
|
Check failure on line 19 in src/serialization/json/DateJsonConverter.cs
|
||
| { | ||
| 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) | ||
|
Check failure on line 40 in src/serialization/json/DateJsonConverter.cs
|
||
| { | ||
| writer.WriteStringValue(value.ToString()); | ||
| } | ||
| } | ||
| 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> | ||
|
Check failure on line 16 in src/serialization/json/TimeJsonConverter.cs
|
||
| { | ||
| /// <inheritdoc /> | ||
| public override Time Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
|
Check failure on line 19 in src/serialization/json/TimeJsonConverter.cs
|
||
| { | ||
| 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) | ||
|
Check failure on line 40 in src/serialization/json/TimeJsonConverter.cs
|
||
| { | ||
| writer.WriteStringValue(value.ToString()); | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } |
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?