From f924d6b5f69792f33a507b47a1fc870a185ad638 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Fri, 5 May 2023 01:13:35 +0300 Subject: [PATCH] Remove a number of unneeded nullability suppressions. (#85788) * Fix nullability annotation of the JsonSerializer.Serialize methods. * Remove a few more suppressions from the implementation. * Revert annotations in the public APIs. --- .../Serialization/Converters/CastingConverter.cs | 2 +- .../Serialization/JsonConverterOfT.WriteCore.cs | 2 +- .../Text/Json/Serialization/JsonConverterOfT.cs | 15 ++++++++++----- .../Metadata/JsonTypeInfoOfT.WriteHelpers.cs | 6 +++--- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/CastingConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/CastingConverter.cs index 9318301476a5e..2f4f672e3e16e 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/CastingConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/CastingConverter.cs @@ -69,7 +69,7 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, T value, J internal override T ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options) => JsonSerializer.UnboxOnRead(_sourceConverter.ReadNumberWithCustomHandlingAsObject(ref reader, handling, options))!; - internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, T value, JsonNumberHandling handling) + internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, T? value, JsonNumberHandling handling) => _sourceConverter.WriteNumberWithCustomHandlingAsObject(writer, value, handling); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs index 3abbd6564067c..8f1d8be16f89e 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs @@ -7,7 +7,7 @@ public partial class JsonConverter { internal bool WriteCore( Utf8JsonWriter writer, - in T value, + in T? value, JsonSerializerOptions options, ref WriteStack state) { 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 a9f81fda0f0c8..211a083763c6f 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 @@ -152,7 +152,12 @@ internal sealed override bool TryWriteAsObject(Utf8JsonWriter writer, object? va } // Provide a default implementation for value converters. - internal virtual bool OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state) + internal virtual bool OnTryWrite(Utf8JsonWriter writer, +#nullable disable // T may or may not be nullable depending on the derived converter's HandleNull override. + T value, +#nullable enable + JsonSerializerOptions options, + ref WriteStack state) { Write(writer, value, options); return true; @@ -356,9 +361,9 @@ internal sealed override bool TryReadAsObject(ref Utf8JsonReader reader, Type ty /// The 'in' modifier in 'TryWrite(in T Value)' causes boxing for Nullable{T}, so this helper avoids that. /// TODO: Remove this work-around once https://github.com/dotnet/runtime/issues/50915 is addressed. /// - private static bool IsNull(T value) => value is null; + private static bool IsNull(T? value) => value is null; - internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions options, ref WriteStack state) + internal bool TryWrite(Utf8JsonWriter writer, in T? value, JsonSerializerOptions options, ref WriteStack state) { if (writer.CurrentDepth >= options.EffectiveMaxDepth) { @@ -604,7 +609,7 @@ internal void VerifyWrite(int originalDepth, Utf8JsonWriter writer) /// The being used. public abstract void Write( Utf8JsonWriter writer, -#nullable disable // T may or may not be nullable depending on the derived type's overload. +#nullable disable // T may or may not be nullable depending on the derived converter's HandleNull override. T value, #nullable restore JsonSerializerOptions options); @@ -707,7 +712,7 @@ internal virtual void WriteAsPropertyNameCore(Utf8JsonWriter writer, T value, Js internal virtual T ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options) => throw new InvalidOperationException(); - internal virtual void WriteNumberWithCustomHandling(Utf8JsonWriter writer, T value, JsonNumberHandling handling) + internal virtual void WriteNumberWithCustomHandling(Utf8JsonWriter writer, T? value, JsonNumberHandling handling) => throw new InvalidOperationException(); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.WriteHelpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.WriteHelpers.cs index 644fed7c3675a..90b44b1b6f4d6 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.WriteHelpers.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.WriteHelpers.cs @@ -52,7 +52,7 @@ rootValue is not null && WriteStack state = default; state.Initialize(this, rootValueBoxed); - bool success = EffectiveConverter.WriteCore(writer, rootValue!, Options, ref state); + bool success = EffectiveConverter.WriteCore(writer, rootValue, Options, ref state); Debug.Assert(success); writer.Flush(); } @@ -128,7 +128,7 @@ rootValue is not null && try { - isFinalBlock = EffectiveConverter.WriteCore(writer, rootValue!, Options, ref state); + isFinalBlock = EffectiveConverter.WriteCore(writer, rootValue, Options, ref state); writer.Flush(); if (state.SuppressFlush) @@ -247,7 +247,7 @@ rootValue is not null && { state.FlushThreshold = (int)(bufferWriter.Capacity * JsonSerializer.FlushThreshold); - isFinalBlock = EffectiveConverter.WriteCore(writer, rootValue!, Options, ref state); + isFinalBlock = EffectiveConverter.WriteCore(writer, rootValue, Options, ref state); writer.Flush(); bufferWriter.WriteToStream(utf8Json);