Remove a number of unneeded nullability suppressions. (#85788)
authorEirik Tsarpalis <eirik.tsarpalis@gmail.com>
Thu, 4 May 2023 22:13:35 +0000 (01:13 +0300)
committerGitHub <noreply@github.com>
Thu, 4 May 2023 22:13:35 +0000 (23:13 +0100)
* Fix nullability annotation of the JsonSerializer.Serialize methods.

* Remove a few more suppressions from the implementation.

* Revert annotations in the public APIs.

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/CastingConverter.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.WriteHelpers.cs

index 9318301..2f4f672 100644 (file)
@@ -69,7 +69,7 @@ namespace System.Text.Json.Serialization.Converters
         internal override T ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options)
             => JsonSerializer.UnboxOnRead<T>(_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);
     }
 }
index 3abbd65..8f1d8be 100644 (file)
@@ -7,7 +7,7 @@ namespace System.Text.Json.Serialization
     {
         internal bool WriteCore(
             Utf8JsonWriter writer,
-            in T value,
+            in T? value,
             JsonSerializerOptions options,
             ref WriteStack state)
         {
index a9f81fd..211a083 100644 (file)
@@ -152,7 +152,12 @@ namespace System.Text.Json.Serialization
         }
 
         // 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 @@ namespace System.Text.Json.Serialization
         /// 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.
         /// </summary>
-        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 @@ namespace System.Text.Json.Serialization
         /// <param name="options">The <see cref="JsonSerializerOptions"/> being used.</param>
         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 @@ namespace System.Text.Json.Serialization
         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();
     }
 }
index 644fed7..90b44b1 100644 (file)
@@ -52,7 +52,7 @@ namespace System.Text.Json.Serialization.Metadata
                 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 @@ namespace System.Text.Json.Serialization.Metadata
 
                         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 @@ namespace System.Text.Json.Serialization.Metadata
                 {
                     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);