From 66e33857a4cebbae640cccc4e0b1907f0afc6d9b Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Wed, 10 Jul 2024 08:23:24 -0700 Subject: [PATCH] Reuse static converter instances and use known converters in the Document JsonSerializerOptions --- ...ocumentJsonSerializerOptionsConfiguration.cs | 8 +++++--- .../OrchardCore.Abstractions/Json/JOptions.cs | 17 +++++++++++++---- .../Json/Serialization/DateTimeJsonConverter.cs | 4 +++- .../Json/Serialization/TimeSpanJsonConverter.cs | 4 +++- .../UsersServiceCollectionExtensions.cs | 2 +- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/OrchardCore/OrchardCore.Abstractions/Extensions/DocumentJsonSerializerOptionsConfiguration.cs b/src/OrchardCore/OrchardCore.Abstractions/Extensions/DocumentJsonSerializerOptionsConfiguration.cs index 7f3ab5b0832..9f477b06802 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Extensions/DocumentJsonSerializerOptionsConfiguration.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Extensions/DocumentJsonSerializerOptionsConfiguration.cs @@ -2,7 +2,6 @@ using System.Text.Json.Serialization; using Microsoft.Extensions.Options; using OrchardCore.Json; -using OrchardCore.Json.Serialization; namespace OrchardCore.Extensions; @@ -25,7 +24,10 @@ public void Configure(DocumentJsonSerializerOptions options) options.SerializerOptions.WriteIndented = JOptions.Base.WriteIndented; options.SerializerOptions.TypeInfoResolverChain.Add(new PolymorphicJsonTypeInfoResolver(_derivedTypesOptions)); - options.SerializerOptions.Converters.Add(DynamicJsonConverter.Instance); - options.SerializerOptions.Converters.Add(PathStringJsonConverter.Instance); + + foreach (var converter in JOptions.KnownConverters) + { + options.SerializerOptions.Converters.Add(converter); + } } } diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs index 727e2feb931..6b296a95928 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs @@ -10,6 +10,14 @@ namespace System.Text.Json; /// public static class JOptions { + public static readonly JsonConverter[] KnownConverters = + [ + DynamicJsonConverter.Instance, + PathStringJsonConverter.Instance, + TimeSpanJsonConverter.Instance, + DateTimeJsonConverter.Instance, + ]; + public static readonly JsonSerializerOptions Base = new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, @@ -34,10 +42,11 @@ public static class JOptions static JOptions() { Default = new JsonSerializerOptions(Base); - Default.Converters.Add(new DynamicJsonConverter()); - Default.Converters.Add(new PathStringJsonConverter()); - Default.Converters.Add(new TimeSpanJsonConverter()); - Default.Converters.Add(new DateTimeJsonConverter()); + + foreach (var converter in KnownConverters) + { + Default.Converters.Add(converter); + } Indented = new JsonSerializerOptions(Default) { diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs index 9c2915aa2dc..ba4dde3979c 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs @@ -7,6 +7,8 @@ namespace OrchardCore.Json.Serialization; public class DateTimeJsonConverter : JsonConverter { + public static readonly DateTimeJsonConverter Instance = new(); + public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (typeToConvert != typeof(DateTime)) @@ -14,7 +16,7 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso throw new ArgumentException("Unexpected type to convert.", nameof(typeToConvert)); } - if (!reader.TryGetDateTime(out DateTime value) && DateTime.TryParse(reader.GetString()!, out value)) + if (!reader.TryGetDateTime(out var value) && DateTime.TryParse(reader.GetString()!, out value)) { return value; } diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/TimeSpanJsonConverter.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/TimeSpanJsonConverter.cs index f7811abb64a..4fcafce8113 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/TimeSpanJsonConverter.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/TimeSpanJsonConverter.cs @@ -6,6 +6,8 @@ namespace OrchardCore.Json.Serialization; public class TimeSpanJsonConverter : JsonConverter { + public static readonly TimeSpanJsonConverter Instance = new(); + public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.String) @@ -14,7 +16,7 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso } var stringValue = reader.GetString(); - + if (TimeSpan.TryParse(stringValue, out var timeSpan)) { return timeSpan; diff --git a/src/OrchardCore/OrchardCore.Users.Core/Extensions/UsersServiceCollectionExtensions.cs b/src/OrchardCore/OrchardCore.Users.Core/Extensions/UsersServiceCollectionExtensions.cs index 4d071924f84..c8ca86e82bf 100644 --- a/src/OrchardCore/OrchardCore.Users.Core/Extensions/UsersServiceCollectionExtensions.cs +++ b/src/OrchardCore/OrchardCore.Users.Core/Extensions/UsersServiceCollectionExtensions.cs @@ -59,7 +59,7 @@ public static IServiceCollection AddUsers(this IServiceCollection services) services.Configure(options => { - options.SerializerOptions.Converters.Add(new LoginInfoJsonConverter()); + options.SerializerOptions.Converters.Add(LoginInfoJsonConverter.Instance); }); return services;