From f487a9bbee4fecd4597a87a435e92cf3a4c337e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20J=C3=B8rgen=20Skogstad?= Date: Fri, 13 Dec 2024 10:52:18 +0100 Subject: [PATCH 1/3] --wip-- [skip ci] --- .../Common/Json/NonNullableListConverter.cs | 74 +++++++++++++++++++ .../Program.cs | 3 + 2 files changed, 77 insertions(+) create mode 100644 src/Digdir.Domain.Dialogporten.WebApi/Common/Json/NonNullableListConverter.cs diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Common/Json/NonNullableListConverter.cs b/src/Digdir.Domain.Dialogporten.WebApi/Common/Json/NonNullableListConverter.cs new file mode 100644 index 000000000..f27f8b318 --- /dev/null +++ b/src/Digdir.Domain.Dialogporten.WebApi/Common/Json/NonNullableListConverter.cs @@ -0,0 +1,74 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Digdir.Domain.Dialogporten.WebApi.Common.Json; + +public class NonNullableListConverterFactory : JsonConverterFactory +{ + public override bool CanConvert(Type typeToConvert) + => typeToConvert.IsGenericType && + typeToConvert.GetGenericTypeDefinition() == typeof(List<>); + + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) + { + var elementType = typeToConvert.GetGenericArguments()[0]; + var converterType = typeof(NonNullableListConverter<>).MakeGenericType(elementType); + + return (JsonConverter)Activator.CreateInstance(converterType)!; + } + + private sealed class NonNullableListConverter : JsonConverter> + { + public override List Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.Null) + { + throw new JsonException("Custom error message: The list cannot be null."); + } + + if (reader.TokenType != JsonTokenType.StartArray) + { + throw new JsonException(); + } + + List list = []; + while (reader.Read()) + { + if (reader.TokenType == JsonTokenType.EndArray) + { + return list; + } + + // // Get the key. + // if (reader.TokenType != JsonTokenType.PropertyName) + // { + // throw new JsonException(); + // } + // + // string? propertyName = reader.GetString(); + // + // // For performance, parse with ignoreCase:false first. + // if (!Enum.TryParse(propertyName, ignoreCase: false, out TKey key) && + // !Enum.TryParse(propertyName, ignoreCase: true, out key)) + // { + // throw new JsonException( + // $"Unable to convert \"{propertyName}\" to Enum \"{_keyType}\"."); + // } + // + // // Get the value. + // reader.Read(); + // TValue value = _valueConverter.Read(ref reader, _valueType, options)!; + // + // // Add to dictionary. + // dictionary.Add(key, value); + } + + throw new JsonException(); + } + + public override void Write(Utf8JsonWriter writer, List value, JsonSerializerOptions options) + { + // JsonSerializer.Serialize(writer, value, options); + } + } +} diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index 21433a66e..fbde5cc56 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -7,6 +7,7 @@ using Digdir.Domain.Dialogporten.Application.Common.Extensions; using Digdir.Domain.Dialogporten.Application.Common.Extensions.OptionExtensions; using Digdir.Domain.Dialogporten.Application.Externals.Presentation; +using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Update; using Digdir.Domain.Dialogporten.WebApi.Common.Extensions; using Digdir.Domain.Dialogporten.Infrastructure; using Digdir.Domain.Dialogporten.WebApi; @@ -173,12 +174,14 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura new EndpointNameMetadata( TypeNameConverter.ToShortName(endpointDefinition.EndpointType))))); }; + // x.Serializer.Options.RespectNullableAnnotations = true; x.Serializer.Options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; // Do not serialize empty collections x.Serializer.Options.TypeInfoResolver = new DefaultJsonTypeInfoResolver { Modifiers = { IgnoreEmptyCollections } }; + x.Serializer.Options.Converters.Add(new NonNullableListConverterFactory()); x.Serializer.Options.Converters.Add(new JsonStringEnumConverter()); x.Serializer.Options.Converters.Add(new UtcDateTimeOffsetConverter()); x.Serializer.Options.Converters.Add(new DateTimeNotSupportedConverter()); From ccc77db283f1cb09fc20922cf506c4cc9804c28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20J=C3=B8rgen=20Skogstad?= Date: Fri, 13 Dec 2024 12:37:42 +0100 Subject: [PATCH 2/3] brp --- .../Common/Json/NonNullableListConverter.cs | 74 ------------------- .../Program.cs | 3 +- 2 files changed, 1 insertion(+), 76 deletions(-) delete mode 100644 src/Digdir.Domain.Dialogporten.WebApi/Common/Json/NonNullableListConverter.cs diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Common/Json/NonNullableListConverter.cs b/src/Digdir.Domain.Dialogporten.WebApi/Common/Json/NonNullableListConverter.cs deleted file mode 100644 index f27f8b318..000000000 --- a/src/Digdir.Domain.Dialogporten.WebApi/Common/Json/NonNullableListConverter.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace Digdir.Domain.Dialogporten.WebApi.Common.Json; - -public class NonNullableListConverterFactory : JsonConverterFactory -{ - public override bool CanConvert(Type typeToConvert) - => typeToConvert.IsGenericType && - typeToConvert.GetGenericTypeDefinition() == typeof(List<>); - - public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) - { - var elementType = typeToConvert.GetGenericArguments()[0]; - var converterType = typeof(NonNullableListConverter<>).MakeGenericType(elementType); - - return (JsonConverter)Activator.CreateInstance(converterType)!; - } - - private sealed class NonNullableListConverter : JsonConverter> - { - public override List Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.Null) - { - throw new JsonException("Custom error message: The list cannot be null."); - } - - if (reader.TokenType != JsonTokenType.StartArray) - { - throw new JsonException(); - } - - List list = []; - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndArray) - { - return list; - } - - // // Get the key. - // if (reader.TokenType != JsonTokenType.PropertyName) - // { - // throw new JsonException(); - // } - // - // string? propertyName = reader.GetString(); - // - // // For performance, parse with ignoreCase:false first. - // if (!Enum.TryParse(propertyName, ignoreCase: false, out TKey key) && - // !Enum.TryParse(propertyName, ignoreCase: true, out key)) - // { - // throw new JsonException( - // $"Unable to convert \"{propertyName}\" to Enum \"{_keyType}\"."); - // } - // - // // Get the value. - // reader.Read(); - // TValue value = _valueConverter.Read(ref reader, _valueType, options)!; - // - // // Add to dictionary. - // dictionary.Add(key, value); - } - - throw new JsonException(); - } - - public override void Write(Utf8JsonWriter writer, List value, JsonSerializerOptions options) - { - // JsonSerializer.Serialize(writer, value, options); - } - } -} diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index fbde5cc56..3ce01d8f0 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -174,14 +174,13 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura new EndpointNameMetadata( TypeNameConverter.ToShortName(endpointDefinition.EndpointType))))); }; - // x.Serializer.Options.RespectNullableAnnotations = true; + x.Serializer.Options.RespectNullableAnnotations = true; x.Serializer.Options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; // Do not serialize empty collections x.Serializer.Options.TypeInfoResolver = new DefaultJsonTypeInfoResolver { Modifiers = { IgnoreEmptyCollections } }; - x.Serializer.Options.Converters.Add(new NonNullableListConverterFactory()); x.Serializer.Options.Converters.Add(new JsonStringEnumConverter()); x.Serializer.Options.Converters.Add(new UtcDateTimeOffsetConverter()); x.Serializer.Options.Converters.Add(new DateTimeNotSupportedConverter()); From 6da2e7a1473ab7fe51f36daaf5d06fc6f72ab6c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20J=C3=B8rgen=20Skogstad?= Date: Fri, 13 Dec 2024 12:39:31 +0100 Subject: [PATCH 3/3] brp --- src/Digdir.Domain.Dialogporten.WebApi/Program.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index 3ce01d8f0..c02e9f007 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -7,7 +7,6 @@ using Digdir.Domain.Dialogporten.Application.Common.Extensions; using Digdir.Domain.Dialogporten.Application.Common.Extensions.OptionExtensions; using Digdir.Domain.Dialogporten.Application.Externals.Presentation; -using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Update; using Digdir.Domain.Dialogporten.WebApi.Common.Extensions; using Digdir.Domain.Dialogporten.Infrastructure; using Digdir.Domain.Dialogporten.WebApi;