Skip to content

Commit

Permalink
Remove a number of unneeded nullability suppressions. (dotnet#85788)
Browse files Browse the repository at this point in the history
* Fix nullability annotation of the JsonSerializer.Serialize methods.

* Remove a few more suppressions from the implementation.

* Revert annotations in the public APIs.
  • Loading branch information
eiriktsarpalis committed May 4, 2023
1 parent b2ff13f commit f924d6b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class JsonConverter<T>
{
internal bool WriteCore(
Utf8JsonWriter writer,
in T value,
in T? value,
JsonSerializerOptions options,
ref WriteStack state)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
/// </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)
{
Expand Down Expand Up @@ -604,7 +609,7 @@ internal void VerifyWrite(int originalDepth, Utf8JsonWriter writer)
/// <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);
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f924d6b

Please sign in to comment.