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);
}
}
}
// 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;
/// 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)
{
/// <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);
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();
}
}
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();
}
try
{
- isFinalBlock = EffectiveConverter.WriteCore(writer, rootValue!, Options, ref state);
+ isFinalBlock = EffectiveConverter.WriteCore(writer, rootValue, Options, ref state);
writer.Flush();
if (state.SuppressFlush)
{
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);