From bb860637b48b7873b537e879b6798f6696b33853 Mon Sep 17 00:00:00 2001 From: David Cantu Date: Thu, 18 Feb 2021 08:53:14 -0800 Subject: [PATCH] Remove unnecessary check on polymorphic serialization --- .../Json/Serialization/JsonConverterOfT.cs | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs index b55bc531c8cd8c..c8ca15daec3305 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs @@ -363,26 +363,25 @@ internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions // For internal converter only: Handle polymorphic case and get the new converter. // Custom converter, even though polymorphic converter, get called for reading AND writing. JsonConverter jsonConverter = state.Current.InitializeReEntry(type, options); - if (jsonConverter != this) + Debug.Assert(jsonConverter != this); + + if (options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.IgnoreCycles && + jsonConverter.IsValueType) { - if (options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.IgnoreCycles && - jsonConverter.IsValueType) - { - // For boxed value types: push the value before it gets unboxed on TryWriteAsObject. - state.ReferenceResolver.PushReferenceForCycleDetection(value); - ignoreCyclesPopReference = true; - } + // For boxed value types: push the value before it gets unboxed on TryWriteAsObject. + state.ReferenceResolver.PushReferenceForCycleDetection(value); + ignoreCyclesPopReference = true; + } - // We found a different converter; forward to that. - bool success2 = jsonConverter.TryWriteAsObject(writer, value, options, ref state); + // We found a different converter; forward to that. + bool success2 = jsonConverter.TryWriteAsObject(writer, value, options, ref state); - if (ignoreCyclesPopReference) - { - state.ReferenceResolver.PopReferenceForCycleDetection(); - } - - return success2; + if (ignoreCyclesPopReference) + { + state.ReferenceResolver.PopReferenceForCycleDetection(); } + + return success2; } }