diff --git a/src/_DomainDriven/Framework.DomainDriven.WebApiNetCore/JsonConverter/DefaultJsonNamingPolicy.cs b/src/_DomainDriven/Framework.DomainDriven.WebApiNetCore/JsonConverter/DefaultJsonNamingPolicy.cs new file mode 100644 index 000000000..cb9422d4a --- /dev/null +++ b/src/_DomainDriven/Framework.DomainDriven.WebApiNetCore/JsonConverter/DefaultJsonNamingPolicy.cs @@ -0,0 +1,12 @@ +#nullable enable + +using System.Text.Json; + +namespace Framework.DomainDriven.WebApiNetCore.JsonConverter; + +public class DefaultJsonNamingPolicy : JsonNamingPolicy +{ + public override string ConvertName(string name) => name; + + public static DefaultJsonNamingPolicy Default { get; } = new(); +} diff --git a/src/_DomainDriven/Framework.DomainDriven.WebApiNetCore/JsonConverter/PeriodJsonConverter.cs b/src/_DomainDriven/Framework.DomainDriven.WebApiNetCore/JsonConverter/PeriodJsonConverter.cs index b137906ed..8164c880d 100644 --- a/src/_DomainDriven/Framework.DomainDriven.WebApiNetCore/JsonConverter/PeriodJsonConverter.cs +++ b/src/_DomainDriven/Framework.DomainDriven.WebApiNetCore/JsonConverter/PeriodJsonConverter.cs @@ -16,7 +16,7 @@ public override Period Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS var stringComparer = options.PropertyNameCaseInsensitive ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal; - var (startDateName, endDateName) = GetPropertyNames(options.PropertyNamingPolicy); + var namingPolicy = options.PropertyNamingPolicy ?? DefaultJsonNamingPolicy.Default; while (reader.Read()) { @@ -28,16 +28,13 @@ public override Period Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS var propertyName = reader.GetString(); reader.Read(); - if (stringComparer.Equals(propertyName, startDateName)) + if (stringComparer.Equals(propertyName, namingPolicy.ConvertName(nameof(Period.StartDate)))) { - startDate = reader.GetDateTime(); + startDate = JsonSerializer.Deserialize(ref reader, options); } - else if (stringComparer.Equals(propertyName, endDateName)) + else if (stringComparer.Equals(propertyName, namingPolicy.ConvertName(nameof(Period.EndDate)))) { - if (reader.TokenType != JsonTokenType.Null) - { - endDate = reader.GetDateTime(); - } + endDate = JsonSerializer.Deserialize(ref reader, options); } } } @@ -49,24 +46,14 @@ public override void Write(Utf8JsonWriter writer, Period value, JsonSerializerOp { writer.WriteStartObject(); - var (startDateName, endDateName) = GetPropertyNames(options.PropertyNamingPolicy); + var namingPolicy = options.PropertyNamingPolicy ?? DefaultJsonNamingPolicy.Default; - writer.WriteString(startDateName, value.StartDate); - if (value.EndDate.HasValue) - { - writer.WriteString(endDateName, value.EndDate.Value); - } - else - { - writer.WriteNull(endDateName); - } + writer.WritePropertyName(namingPolicy.ConvertName(nameof(Period.StartDate))); + JsonSerializer.Serialize(writer, value.StartDate, options); - writer.WriteEndObject(); - } + writer.WritePropertyName(namingPolicy.ConvertName(nameof(Period.EndDate))); + JsonSerializer.Serialize(writer, value.EndDate, options); - private static (string StartPropertName, string EndPropertyName) GetPropertyNames(JsonNamingPolicy? namingPolicy) - { - return (namingPolicy?.ConvertName(nameof(Period.StartDate)) ?? nameof(Period.StartDate), - namingPolicy?.ConvertName(nameof(Period.EndDate)) ?? nameof(Period.EndDate)); + writer.WriteEndObject(); } } diff --git a/src/_SampleSystem/_Tests/SampleSystem.IntegrationTests/JsonConverterTests.cs b/src/_SampleSystem/_Tests/SampleSystem.IntegrationTests/JsonConverterTests.cs index 627000867..735284745 100644 --- a/src/_SampleSystem/_Tests/SampleSystem.IntegrationTests/JsonConverterTests.cs +++ b/src/_SampleSystem/_Tests/SampleSystem.IntegrationTests/JsonConverterTests.cs @@ -35,15 +35,31 @@ public void DateTimeConverted_ResultCorrected() public void PeriodConverted_ResultCorrected() { //Arrange - var currentMonth = this.TimeProvider.GetCurrentMonth(); + var testPeriod = this.TimeProvider.GetCurrentMonth(); var options = new JsonSerializerOptions { Converters = { new UtcDateTimeJsonConverter(), new PeriodJsonConverter() } }; //Act - var jsonText = JsonSerializer.Serialize(currentMonth, options); + var jsonText = JsonSerializer.Serialize(testPeriod, options); var restored = JsonSerializer.Deserialize(jsonText, options); //Assert - currentMonth.Should().Be(restored); + testPeriod.Should().Be(restored); + } + + [TestMethod] + public void PeriodWithNullConverted_ResultCorrected() + { + //Arrange + var testPeriod = new Period(this.TimeProvider.GetToday()); + + var options = new JsonSerializerOptions { Converters = { new UtcDateTimeJsonConverter(), new PeriodJsonConverter() } }; + + //Act + var jsonText = JsonSerializer.Serialize(testPeriod, options); + var restored = JsonSerializer.Deserialize(jsonText, options); + + //Assert + testPeriod.Should().Be(restored); } } diff --git a/src/__SolutionItems/CommonAssemblyInfo.cs b/src/__SolutionItems/CommonAssemblyInfo.cs index dc436986e..757c80acd 100644 --- a/src/__SolutionItems/CommonAssemblyInfo.cs +++ b/src/__SolutionItems/CommonAssemblyInfo.cs @@ -4,9 +4,9 @@ [assembly: AssemblyCompany("Luxoft")] [assembly: AssemblyCopyright("Copyright © Luxoft 2009-2024")] -[assembly: AssemblyVersion("22.2.2.0")] -[assembly: AssemblyFileVersion("22.2.2.0")] -[assembly: AssemblyInformationalVersion("22.2.2.0")] +[assembly: AssemblyVersion("22.2.3.0")] +[assembly: AssemblyFileVersion("22.2.3.0")] +[assembly: AssemblyInformationalVersion("22.2.3.0")] #if DEBUG [assembly: AssemblyConfiguration("Debug")]