From d14469c4a155c3ca9a0daeb11696bab1eed099e0 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Sat, 9 Jan 2021 19:41:38 -0800 Subject: [PATCH 1/2] Fixed a couple places where Nullable types were being boxed checking for null. --- .../Json/Serialization/Converters/Value/NullableConverter.cs | 2 ++ .../src/System/Text/Json/Serialization/JsonConverterOfT.cs | 4 +++- .../Text/Json/Serialization/JsonSerializer.Write.Helpers.cs | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverter.cs index 44e4270a26165..ef5098877dced 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverter.cs @@ -70,5 +70,7 @@ internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, T? v _converter.WriteNumberWithCustomHandling(writer, value.Value, handling); } } + + internal override bool IsNull(T? value) => !value.HasValue; } } 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 49b4400eef3b6..7309def2f239a 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 @@ -294,6 +294,8 @@ internal override sealed bool TryReadAsObject(ref Utf8JsonReader reader, JsonSer return success; } + internal virtual bool IsNull(T value) => value == null; + internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions options, ref WriteStack state) { if (writer.CurrentDepth >= options.EffectiveMaxDepth) @@ -342,7 +344,7 @@ internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions } } } - else if (value == null && !HandleNullOnWrite) + else if (IsNull(value) && !HandleNullOnWrite) { // We do not pass null values to converters unless HandleNullOnWrite is true. Null values for properties were // already handled in GetMemberAndWriteJson() so we don't need to check for IgnoreNullValues here. diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs index d23528f786f88..7664d453e2bff 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs @@ -21,7 +21,7 @@ private static void WriteCore( Debug.Assert(writer != null); // We treat typeof(object) special and allow polymorphic behavior. - if (value != null && inputType == JsonClassInfo.ObjectType) + if (inputType == JsonClassInfo.ObjectType && value != null) { inputType = value!.GetType(); } From 8a347a82eec677fa20a811eeb879616603f21ec2 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Fri, 15 Jan 2021 00:13:49 -0800 Subject: [PATCH 2/2] Code review. --- .../Json/Serialization/Converters/Value/NullableConverter.cs | 2 +- .../src/System/Text/Json/Serialization/JsonConverterOfT.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverter.cs index ef5098877dced..5c1d362f060da 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverter.cs @@ -71,6 +71,6 @@ internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, T? v } } - internal override bool IsNull(T? value) => !value.HasValue; + internal override bool IsNull(in T? value) => !value.HasValue; } } 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 7309def2f239a..d9cda3bafd97e 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 @@ -294,7 +294,7 @@ internal override sealed bool TryReadAsObject(ref Utf8JsonReader reader, JsonSer return success; } - internal virtual bool IsNull(T value) => value == null; + internal virtual bool IsNull(in T value) => value == null; internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions options, ref WriteStack state) { @@ -344,7 +344,7 @@ internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions } } } - else if (IsNull(value) && !HandleNullOnWrite) + else if (CanBeNull && !HandleNullOnWrite && IsNull(value)) { // We do not pass null values to converters unless HandleNullOnWrite is true. Null values for properties were // already handled in GetMemberAndWriteJson() so we don't need to check for IgnoreNullValues here.