Skip to content

Support resumable serialization in NullableConverter<T> #65524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected static JsonConverter<TElement> GetElementConverter(JsonTypeInfo elemen

protected static JsonConverter<TElement> GetElementConverter(ref WriteStack state)
{
JsonConverter<TElement> converter = (JsonConverter<TElement>)state.Current.DeclaredJsonPropertyInfo!.ConverterBase;
JsonConverter<TElement> converter = (JsonConverter<TElement>)state.Current.JsonPropertyInfo!.ConverterBase;
Copy link
Member Author

@eiriktsarpalis eiriktsarpalis Feb 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This renames a property I should have renamed in #65224 and matches the naming convention by the equivalent property in ReadStackFrame.

Debug.Assert(converter != null); // It should not be possible to have a null converter at this point.

return converter;
Expand Down Expand Up @@ -282,7 +282,7 @@ internal override bool OnTryWrite(
writer.WriteStartArray();
}

state.Current.DeclaredJsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
}

success = OnWriteResume(writer, value, options, ref state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ internal sealed override bool OnTryWrite(
}
}

state.Current.DeclaredJsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
}

bool success = OnWriteResume(writer, dictionary, options, ref state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal override bool OnTryWrite(Utf8JsonWriter writer, TOption value, JsonSeri
}

TElement element = _optionValueGetter(value);
state.Current.DeclaredJsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
return _elementConverter.TryWrite(writer, element, options, ref state);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal override bool OnTryWrite(Utf8JsonWriter writer, TValueOption value, Jso

TElement element = _optionValueGetter(ref value);

state.Current.DeclaredJsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
return _elementConverter.TryWrite(writer, element, options, ref state);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal sealed override bool OnTryWrite(
if (jsonPropertyInfo.ShouldSerialize)
{
// Remember the current property for JsonPath support if an exception is thrown.
state.Current.DeclaredJsonPropertyInfo = jsonPropertyInfo;
state.Current.JsonPropertyInfo = jsonPropertyInfo;
state.Current.NumberHandling = jsonPropertyInfo.NumberHandling;

bool success = jsonPropertyInfo.GetMemberAndWriteJson(obj, ref state, writer);
Expand All @@ -295,7 +295,7 @@ internal sealed override bool OnTryWrite(
if (dataExtensionProperty?.ShouldSerialize == true)
{
// Remember the current property for JsonPath support if an exception is thrown.
state.Current.DeclaredJsonPropertyInfo = dataExtensionProperty;
state.Current.JsonPropertyInfo = dataExtensionProperty;
state.Current.NumberHandling = dataExtensionProperty.NumberHandling;

bool success = dataExtensionProperty.GetMemberAndWriteJsonExtensionData(obj, ref state, writer);
Expand Down Expand Up @@ -334,7 +334,7 @@ internal sealed override bool OnTryWrite(
Debug.Assert(jsonPropertyInfo != null);
if (jsonPropertyInfo.ShouldSerialize)
{
state.Current.DeclaredJsonPropertyInfo = jsonPropertyInfo;
state.Current.JsonPropertyInfo = jsonPropertyInfo;
state.Current.NumberHandling = jsonPropertyInfo.NumberHandling;

if (!jsonPropertyInfo.GetMemberAndWriteJson(obj!, ref state, writer))
Expand Down Expand Up @@ -366,7 +366,7 @@ internal sealed override bool OnTryWrite(
if (dataExtensionProperty?.ShouldSerialize == true)
{
// Remember the current property for JsonPath support if an exception is thrown.
state.Current.DeclaredJsonPropertyInfo = dataExtensionProperty;
state.Current.JsonPropertyInfo = dataExtensionProperty;
state.Current.NumberHandling = dataExtensionProperty.NumberHandling;

if (!dataExtensionProperty.GetMemberAndWriteJsonExtensionData(obj, ref state, writer))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,73 +1,102 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace System.Text.Json.Serialization.Converters
{
internal sealed class NullableConverter<T> : JsonConverter<T?> where T : struct
{
internal override ConverterStrategy ConverterStrategy { get; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the ConverterStrategy is determined by the value held by Nullable? I.e. it can be Value, Object, Collection.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right.

internal override Type? ElementType => typeof(T);
public override bool HandleNull => true;

// It is possible to cache the underlying converter since this is an internal converter and
// an instance is created only once for each JsonSerializerOptions instance.
private readonly JsonConverter<T> _converter;
private readonly JsonConverter<T> _elementConverter;

public NullableConverter(JsonConverter<T> elementConverter)
{
_elementConverter = elementConverter;
ConverterStrategy = elementConverter.ConverterStrategy;
IsInternalConverterForNumberType = elementConverter.IsInternalConverterForNumberType;
// temporary workaround for JsonConverter base constructor needing to access
// ConverterStrategy when calculating `CanUseDirectReadOrWrite`.
CanUseDirectReadOrWrite = elementConverter.ConverterStrategy == ConverterStrategy.Value;
}

internal override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ref ReadStack state, out T? value)
{
if (!state.IsContinuation && reader.TokenType == JsonTokenType.Null)
{
value = null;
return true;
}

public NullableConverter(JsonConverter<T> converter)
state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
if (_elementConverter.TryRead(ref reader, typeof(T), options, ref state, out T element))
{
value = element;
return true;
}

value = null;
return false;
}

internal override bool OnTryWrite(Utf8JsonWriter writer, T? value, JsonSerializerOptions options, ref WriteStack state)
{
_converter = converter;
IsInternalConverterForNumberType = converter.IsInternalConverterForNumberType;
if (value is null)
{
writer.WriteNullValue();
return true;
}

state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
return _elementConverter.TryWrite(writer, value.Value, options, ref state);
}

public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// We do not check _converter.HandleNull, as the underlying struct cannot be null.
// A custom converter for some type T? can handle null.
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

T value = _converter.Read(ref reader, typeof(T), options);
T value = _elementConverter.Read(ref reader, typeof(T), options);
return value;
}

public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
{
if (!value.HasValue)
if (value is null)
{
// We do not check _converter.HandleNull, as the underlying struct cannot be null.
// A custom converter for some type T? can handle null.
writer.WriteNullValue();
}
else
{
_converter.Write(writer, value.Value, options);
_elementConverter.Write(writer, value.Value, options);
}
}

internal override T? ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling numberHandling, JsonSerializerOptions options)
{
// We do not check _converter.HandleNull, as the underlying struct cannot be null.
// A custom converter for some type T? can handle null.
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

T value = _converter.ReadNumberWithCustomHandling(ref reader, numberHandling, options);
T value = _elementConverter.ReadNumberWithCustomHandling(ref reader, numberHandling, options);
return value;
}

internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, T? value, JsonNumberHandling handling)
{
if (!value.HasValue)
if (value is null)
{
// We do not check _converter.HandleNull, as the underlying struct cannot be null.
// A custom converter for some type T? can handle null.
writer.WriteNullValue();
}
else
{
_converter.WriteNumberWithCustomHandling(writer, value.Value, handling);
_elementConverter.WriteNumberWithCustomHandling(writer, value.Value, handling);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ internal bool TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSeriali
JsonTypeInfo originalJsonTypeInfo = state.Current.JsonTypeInfo;
#endif
state.Push();
Debug.Assert(TypeToConvert.IsAssignableFrom(state.Current.JsonTypeInfo.Type));
Debug.Assert(TypeToConvert == state.Current.JsonTypeInfo.Type);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strengthens an assertion that should have been changed via #65224.


#if !DEBUG
// For performance, only perform validation on internal converters on debug builds.
Expand Down Expand Up @@ -462,7 +462,7 @@ internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions
JsonTypeInfo originalJsonTypeInfo = state.Current.JsonTypeInfo;
#endif
state.Push();
Debug.Assert(TypeToConvert.IsAssignableFrom(state.Current.JsonTypeInfo.Type));
Debug.Assert(TypeToConvert == state.Current.JsonTypeInfo.Type);

if (!isContinuation)
{
Expand Down Expand Up @@ -528,7 +528,7 @@ internal bool TryWriteDataExtensionProperty(Utf8JsonWriter writer, T value, Json

// Extension data properties change how dictionary key naming policies are applied.
state.Current.IsWritingExtensionDataProperty = true;
state.Current.DeclaredJsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;
state.Current.JsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;

success = dictionaryConverter.OnWriteResume(writer, value, options, ref state);
if (success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static JsonTypeInfo<T> CreateObjectInfo<T>(JsonSerializerOptions options!
/// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
public static JsonTypeInfo<T> CreateValueInfo<T>(JsonSerializerOptions options, JsonConverter converter)
{
JsonTypeInfo<T> info = new JsonTypeInfoInternal<T>(options);
JsonTypeInfo<T> info = new JsonTypeInfoInternal<T>(converter, options);
info.PropertyInfoForTypeInfo = CreateJsonPropertyInfoForClassInfo(typeof(T), info, converter, options);
converter.ConfigureJsonTypeInfo(info, options);
return info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ internal sealed class JsonTypeInfoInternal<T> : JsonTypeInfo<T>
/// <summary>
/// Creates serialization metadata for a type using a simple converter.
/// </summary>
public JsonTypeInfoInternal(JsonSerializerOptions options)
public JsonTypeInfoInternal(JsonConverter converter, JsonSerializerOptions options)
: base(typeof(T), options)
{
ElementType = converter.ElementType;
}

/// <summary>
Expand Down Expand Up @@ -45,6 +46,7 @@ public JsonTypeInfoInternal(JsonSerializerOptions options, JsonObjectInfoValues<
#pragma warning restore CS8714

PropInitFunc = objectInfo.PropertyMetadataInitializer;
ElementType = converter.ElementType;
SerializeHandler = objectInfo.SerializeHandler;
PropertyInfoForTypeInfo = JsonMetadataServices.CreateJsonPropertyInfoForClassInfo(typeof(T), this, converter, Options);
NumberHandling = objectInfo.NumberHandling;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public JsonConverter Initialize(Type type, JsonSerializerOptions options, bool s
internal JsonConverter Initialize(JsonTypeInfo jsonTypeInfo, bool supportContinuation)
{
Current.JsonTypeInfo = jsonTypeInfo;
Current.DeclaredJsonPropertyInfo = jsonTypeInfo.PropertyInfoForTypeInfo;
Current.NumberHandling = Current.DeclaredJsonPropertyInfo.NumberHandling;
Current.JsonPropertyInfo = jsonTypeInfo.PropertyInfoForTypeInfo;
Current.NumberHandling = Current.JsonPropertyInfo.NumberHandling;

JsonSerializerOptions options = jsonTypeInfo.Options;
if (options.ReferenceHandlingStrategy != ReferenceHandlingStrategy.None)
Expand Down Expand Up @@ -141,9 +141,9 @@ public void Push()
_count++;

Current.JsonTypeInfo = jsonTypeInfo;
Current.DeclaredJsonPropertyInfo = jsonTypeInfo.PropertyInfoForTypeInfo;
Current.JsonPropertyInfo = jsonTypeInfo.PropertyInfoForTypeInfo;
// Allow number handling on property to win over handling on type.
Current.NumberHandling = numberHandling ?? Current.DeclaredJsonPropertyInfo.NumberHandling;
Current.NumberHandling = numberHandling ?? Current.JsonPropertyInfo.NumberHandling;
}
}
else
Expand Down Expand Up @@ -347,7 +347,7 @@ public string PropertyPath()
static void AppendStackFrame(StringBuilder sb, ref WriteStackFrame frame)
{
// Append the property name.
string? propertyName = frame.DeclaredJsonPropertyInfo?.ClrName;
string? propertyName = frame.JsonPropertyInfo?.ClrName;
if (propertyName == null)
{
// Attempt to get the JSON property name from the property name specified in re-entry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal struct WriteStackFrame
/// For objects, it is either the actual (real) JsonPropertyInfo or the <see cref="JsonTypeInfo.PropertyInfoForTypeInfo"/> for the class.
/// For collections, it is the <see cref="JsonTypeInfo.PropertyInfoForTypeInfo"/> for the class and current element.
/// </remarks>
public JsonPropertyInfo? DeclaredJsonPropertyInfo;
public JsonPropertyInfo? JsonPropertyInfo;

/// <summary>
/// Used when processing extension data dictionaries.
Expand Down Expand Up @@ -90,7 +90,7 @@ public void EndDictionaryElement()

public void EndProperty()
{
DeclaredJsonPropertyInfo = null!;
JsonPropertyInfo = null!;
JsonPropertyNameAsString = null;
PolymorphicJsonPropertyInfo = null;
PropertyState = StackFramePropertyState.None;
Expand All @@ -102,7 +102,7 @@ public void EndProperty()
/// </summary>
public JsonPropertyInfo GetPolymorphicJsonPropertyInfo()
{
return PolymorphicJsonPropertyInfo ?? DeclaredJsonPropertyInfo!;
return PolymorphicJsonPropertyInfo ?? JsonPropertyInfo!;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public static void ThrowNotSupportedException(ref WriteStack state, NotSupported
Debug.Assert(!message.Contains(" Path: "));

// Obtain the type to show in the message.
Type? propertyType = state.Current.DeclaredJsonPropertyInfo?.PropertyType;
Type? propertyType = state.Current.JsonPropertyInfo?.PropertyType;
if (propertyType == null)
{
propertyType = state.Current.JsonTypeInfo.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ public async Task WriteNestedAsyncEnumerable_DTO<TElement>(IEnumerable<TElement>
Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
}

[Theory]
[MemberData(nameof(GetAsyncEnumerableSources))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a non-IAsyncEnumerable test added that can verify a larger, nullable POCO (as a value type)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nullable POCOs still work, however they don't flow the state. This becomes more evident in the case of IAE which simply fails serialization.

public async Task WriteNestedAsyncEnumerable_Nullable<TElement>(IEnumerable<TElement> source, int delayInterval, int bufferSize)
{
// Primarily tests the ability of NullableConverter to flow async serialization state

JsonSerializerOptions options = new JsonSerializerOptions
{
DefaultBufferSize = bufferSize,
IncludeFields = true,
};

string expectedJson = await JsonSerializerWrapperForString.SerializeWrapper<(IEnumerable<TElement>, bool)?>((source, false), options);

using var stream = new Utf8MemoryStream();
var asyncEnumerable = new MockedAsyncEnumerable<TElement>(source, delayInterval);
await JsonSerializerWrapperForStream.SerializeWrapper<(IAsyncEnumerable<TElement>, bool)?>(stream, (asyncEnumerable, false), options);

JsonTestHelper.AssertJsonEqual(expectedJson, stream.ToString());
Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators);
Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
}

[Theory, OuterLoop]
[InlineData(5000, 1000, true)]
[InlineData(5000, 1000, false)]
Expand Down