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 DefaultJsonNamingPolicy #476

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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<DateTime>(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<DateTime?>(ref reader, options);
}
}
}
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Period>(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<Period>(jsonText, options);

//Assert
testPeriod.Should().Be(restored);
}
}
6 changes: 3 additions & 3 deletions src/__SolutionItems/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Loading