From b5c85f7511b59802e85fd9716d7e72a99ef2739b Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Fri, 25 Feb 2022 16:31:17 +0000 Subject: [PATCH 1/2] Fix default converter reallocation on each call to JsonSerializerOptions.GetConverter --- .../JsonSerializerOptions.Converters.cs | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs index 4276d374058934..77b5d7071f7120 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs @@ -30,23 +30,26 @@ public sealed partial class JsonSerializerOptions [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] private void RootBuiltInConverters() { - s_defaultSimpleConverters = GetDefaultSimpleConverters(); - s_defaultFactoryConverters = new JsonConverter[] + if (s_defaultSimpleConverters is null) { - // Check for disallowed types. - new UnsupportedTypeConverterFactory(), - // Nullable converter should always be next since it forwards to any nullable type. - new NullableConverterFactory(), - new EnumConverterFactory(), - new JsonNodeConverterFactory(), - new FSharpTypeConverterFactory(), - // IAsyncEnumerable takes precedence over IEnumerable. - new IAsyncEnumerableConverterFactory(), - // IEnumerable should always be second to last since they can convert any IEnumerable. - new IEnumerableConverterFactory(), - // Object should always be last since it converts any type. - new ObjectConverterFactory() - }; + s_defaultSimpleConverters = GetDefaultSimpleConverters(); + s_defaultFactoryConverters = new JsonConverter[] + { + // Check for disallowed types. + new UnsupportedTypeConverterFactory(), + // Nullable converter should always be next since it forwards to any nullable type. + new NullableConverterFactory(), + new EnumConverterFactory(), + new JsonNodeConverterFactory(), + new FSharpTypeConverterFactory(), + // IAsyncEnumerable takes precedence over IEnumerable. + new IAsyncEnumerableConverterFactory(), + // IEnumerable should always be second to last since they can convert any IEnumerable. + new IEnumerableConverterFactory(), + // Object should always be last since it converts any type. + new ObjectConverterFactory() + }; + } } private static Dictionary GetDefaultSimpleConverters() From c399afaaeb7e62707c50d83f82e4f56c3c352e36 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Wed, 9 Mar 2022 08:12:33 +0000 Subject: [PATCH 2/2] ensure package is released --- src/libraries/System.Text.Json/src/System.Text.Json.csproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index 9fa6bfb8a500ba..ee5d0b2c6e97d5 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -9,7 +9,8 @@ enable true true - 2 + true + 3 Provides high-performance and low-allocating types that serialize objects to JavaScript Object Notation (JSON) text and deserialize JSON text to objects, with UTF-8 support built-in. Also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document object model (DOM), that is read-only, for random access of the JSON elements within a structured view of the data. Commonly Used Types: