未验证 提交 b86b1f51 编写于 作者: E Eirik Tsarpalis 提交者: GitHub

Remove unneeded references to JsonTypeInfo.PropertyInfoForTypeInfo. (#74594)

* Remove unneeded references to JsonTypeInfo.PropertyInfoForTypeInfo.

* Update src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs

* Update src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs

* Address feedback
上级 ecb3038f
...@@ -567,7 +567,7 @@ private void BeginRead(scoped ref ReadStack state, ref Utf8JsonReader reader, Js ...@@ -567,7 +567,7 @@ private void BeginRead(scoped ref ReadStack state, ref Utf8JsonReader reader, Js
JsonSerializerOptions options, JsonSerializerOptions options,
out JsonParameterInfo? jsonParameterInfo) out JsonParameterInfo? jsonParameterInfo)
{ {
Debug.Assert(state.Current.JsonTypeInfo.PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object); Debug.Assert(state.Current.JsonTypeInfo.Kind == JsonTypeInfoKind.Object);
ReadOnlySpan<byte> unescapedPropertyName = JsonSerializer.GetPropertyName(ref state, ref reader, options); ReadOnlySpan<byte> unescapedPropertyName = JsonSerializer.GetPropertyName(ref state, ref reader, options);
......
...@@ -25,7 +25,7 @@ public static partial class JsonSerializer ...@@ -25,7 +25,7 @@ public static partial class JsonSerializer
bool createExtensionProperty = true) bool createExtensionProperty = true)
{ {
#if DEBUG #if DEBUG
if (state.Current.JsonTypeInfo.PropertyInfoForTypeInfo.ConverterStrategy != ConverterStrategy.Object) if (state.Current.JsonTypeInfo.Kind != JsonTypeInfoKind.Object)
{ {
string objTypeName = obj?.GetType().FullName ?? "<null>"; string objTypeName = obj?.GetType().FullName ?? "<null>";
Debug.Fail($"obj.GetType() => {objTypeName}; {state.Current.JsonTypeInfo.GetPropertyDebugInfo(unescapedPropertyName)}"); Debug.Fail($"obj.GetType() => {objTypeName}; {state.Current.JsonTypeInfo.GetPropertyDebugInfo(unescapedPropertyName)}");
......
...@@ -20,8 +20,6 @@ public abstract class JsonPropertyInfo ...@@ -20,8 +20,6 @@ public abstract class JsonPropertyInfo
internal JsonTypeInfo? ParentTypeInfo { get; private set; } internal JsonTypeInfo? ParentTypeInfo { get; private set; }
private JsonTypeInfo? _jsonTypeInfo; private JsonTypeInfo? _jsonTypeInfo;
internal ConverterStrategy ConverterStrategy { get; private protected set; }
/// <summary> /// <summary>
/// Converter after applying CustomConverter (i.e. JsonConverterAttribute) /// Converter after applying CustomConverter (i.e. JsonConverterAttribute)
/// </summary> /// </summary>
...@@ -424,7 +422,7 @@ private void DetermineSerializationCapabilities() ...@@ -424,7 +422,7 @@ private void DetermineSerializationCapabilities()
return; return;
} }
if ((ConverterStrategy & (ConverterStrategy.Enumerable | ConverterStrategy.Dictionary)) != 0) if ((EffectiveConverter.ConverterStrategy & (ConverterStrategy.Enumerable | ConverterStrategy.Dictionary)) != 0)
{ {
// Properties of collections types that only have setters are not supported. // Properties of collections types that only have setters are not supported.
if (Get == null && Set != null && !_isUserSpecifiedSetter) if (Get == null && Set != null && !_isUserSpecifiedSetter)
...@@ -501,7 +499,7 @@ private bool NumberHandingIsApplicable() ...@@ -501,7 +499,7 @@ private bool NumberHandingIsApplicable()
Type potentialNumberType; Type potentialNumberType;
if (!EffectiveConverter.IsInternalConverter || if (!EffectiveConverter.IsInternalConverter ||
((ConverterStrategy.Enumerable | ConverterStrategy.Dictionary) & ConverterStrategy) == 0) ((ConverterStrategy.Enumerable | ConverterStrategy.Dictionary) & EffectiveConverter.ConverterStrategy) == 0)
{ {
potentialNumberType = PropertyType; potentialNumberType = PropertyType;
} }
...@@ -802,8 +800,6 @@ internal JsonTypeInfo JsonTypeInfo ...@@ -802,8 +800,6 @@ internal JsonTypeInfo JsonTypeInfo
{ {
// This could potentially be double initialized // This could potentially be double initialized
Debug.Assert(_jsonTypeInfo == null || _jsonTypeInfo == value); Debug.Assert(_jsonTypeInfo == null || _jsonTypeInfo == value);
// Ensure the right strategy is surfaced in PropertyInfoForTypeInfo early
ConverterStrategy = value?.Converter.ConverterStrategy ?? default;
_jsonTypeInfo = value; _jsonTypeInfo = value;
} }
} }
......
...@@ -10,8 +10,6 @@ namespace System.Text.Json.Serialization.Metadata ...@@ -10,8 +10,6 @@ namespace System.Text.Json.Serialization.Metadata
/// <summary> /// <summary>
/// Represents a strongly-typed property to prevent boxing and to create a direct delegate to the getter\setter. /// Represents a strongly-typed property to prevent boxing and to create a direct delegate to the getter\setter.
/// </summary> /// </summary>
/// <typeparamref name="T"/> is the <see cref="JsonConverter{T}.TypeToConvert"/> for either the property's converter,
/// or a type's converter, if the current instance is a <see cref="JsonTypeInfo.PropertyInfoForTypeInfo"/>.
internal sealed class JsonPropertyInfo<T> : JsonPropertyInfo internal sealed class JsonPropertyInfo<T> : JsonPropertyInfo
{ {
private Func<object, T>? _typedGet; private Func<object, T>? _typedGet;
...@@ -221,7 +219,6 @@ private protected override void DetermineEffectiveConverter(JsonTypeInfo jsonTyp ...@@ -221,7 +219,6 @@ private protected override void DetermineEffectiveConverter(JsonTypeInfo jsonTyp
_effectiveConverter = converter; _effectiveConverter = converter;
_typedEffectiveConverter = converter; _typedEffectiveConverter = converter;
ConverterStrategy = converter.ConverterStrategy;
} }
internal override object? GetValueAsObject(object obj) internal override object? GetValueAsObject(object obj)
...@@ -249,7 +246,7 @@ internal override bool GetMemberAndWriteJson(object obj, ref WriteStack state, U ...@@ -249,7 +246,7 @@ internal override bool GetMemberAndWriteJson(object obj, ref WriteStack state, U
value is not null && value is not null &&
!state.IsContinuation && !state.IsContinuation &&
// .NET types that are serialized as JSON primitive values don't need to be tracked for cycle detection e.g: string. // .NET types that are serialized as JSON primitive values don't need to be tracked for cycle detection e.g: string.
ConverterStrategy != ConverterStrategy.Value && EffectiveConverter.ConverterStrategy != ConverterStrategy.Value &&
state.ReferenceResolver.ContainsReferenceForCycleDetection(value)) state.ReferenceResolver.ContainsReferenceForCycleDetection(value))
{ {
// If a reference cycle is detected, treat value as null. // If a reference cycle is detected, treat value as null.
......
...@@ -341,7 +341,7 @@ internal void ValidateCanBeUsedForMetadataSerialization() ...@@ -341,7 +341,7 @@ internal void ValidateCanBeUsedForMetadataSerialization()
{ {
if (KeyType != null) if (KeyType != null)
{ {
Debug.Assert(PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Dictionary); Debug.Assert(Kind == JsonTypeInfoKind.Dictionary);
// GetOrAddJsonTypeInfo already ensures JsonTypeInfo is configured // GetOrAddJsonTypeInfo already ensures JsonTypeInfo is configured
// also see comment on JsonPropertyInfo.JsonTypeInfo // also see comment on JsonPropertyInfo.JsonTypeInfo
...@@ -401,20 +401,15 @@ internal void ValidateCanBeUsedForMetadataSerialization() ...@@ -401,20 +401,15 @@ internal void ValidateCanBeUsedForMetadataSerialization()
public JsonTypeInfoKind Kind { get; private set; } public JsonTypeInfoKind Kind { get; private set; }
/// <summary> /// <summary>
/// The JsonPropertyInfo for this JsonTypeInfo. It is used to obtain the converter for the TypeInfo. /// Dummy <see cref="JsonPropertyInfo"/> instance corresponding to the declaring type of this <see cref="JsonTypeInfo"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The returned JsonPropertyInfo does not represent a real property; instead it represents either: /// Used as convenience in cases where we want to serialize property-like values that do not define property metadata, such as:
/// a collection element type, /// 1. a collection element type,
/// a generic type parameter, /// 2. a dictionary key or value type or,
/// a property type (if pushed to a new stack frame), /// 3. the property metadata for the root-level value.
/// or the root type passed into the root serialization APIs. /// For example, for a property returning <see cref="List{T}"/> where T is a string,
/// For example, for a property returning <see cref="Collections.Generic.List{T}"/> where T is a string,
/// a JsonTypeInfo will be created with .Type=typeof(string) and .PropertyInfoForTypeInfo=JsonPropertyInfo{string}. /// a JsonTypeInfo will be created with .Type=typeof(string) and .PropertyInfoForTypeInfo=JsonPropertyInfo{string}.
/// Without this property, a "Converter" property would need to be added to JsonTypeInfo and there would be several more
/// `if` statements to obtain the converter from either the actual JsonPropertyInfo (for a real property) or from the
/// TypeInfo (for the cases mentioned above). In addition, methods that have a JsonPropertyInfo argument would also likely
/// need to add an argument for JsonTypeInfo.
/// </remarks> /// </remarks>
internal JsonPropertyInfo PropertyInfoForTypeInfo { get; } internal JsonPropertyInfo PropertyInfoForTypeInfo { get; }
...@@ -456,7 +451,7 @@ internal JsonTypeInfo(Type type, JsonConverter converter, JsonSerializerOptions ...@@ -456,7 +451,7 @@ internal JsonTypeInfo(Type type, JsonConverter converter, JsonSerializerOptions
PropertyInfoForTypeInfo = CreatePropertyInfoForTypeInfo(); PropertyInfoForTypeInfo = CreatePropertyInfoForTypeInfo();
ElementType = converter.ElementType; ElementType = converter.ElementType;
switch (PropertyInfoForTypeInfo.ConverterStrategy) switch (converter.ConverterStrategy)
{ {
case ConverterStrategy.Dictionary: case ConverterStrategy.Dictionary:
{ {
...@@ -473,11 +468,11 @@ internal JsonTypeInfo(Type type, JsonConverter converter, JsonSerializerOptions ...@@ -473,11 +468,11 @@ internal JsonTypeInfo(Type type, JsonConverter converter, JsonSerializerOptions
} }
break; break;
default: default:
Debug.Fail($"Unexpected class type: {PropertyInfoForTypeInfo.ConverterStrategy}"); Debug.Fail($"Unexpected class type: {converter.ConverterStrategy}");
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
Kind = GetTypeInfoKind(type, PropertyInfoForTypeInfo.ConverterStrategy); Kind = GetTypeInfoKind(type, converter.ConverterStrategy);
} }
internal void VerifyMutable() internal void VerifyMutable()
...@@ -542,9 +537,8 @@ internal void Configure() ...@@ -542,9 +537,8 @@ internal void Configure()
CanUseSerializeHandler &= Options.SerializerContext?.CanUseSerializationLogic == true; CanUseSerializeHandler &= Options.SerializerContext?.CanUseSerializationLogic == true;
JsonConverter converter = Converter; JsonConverter converter = Converter;
Debug.Assert(PropertyInfoForTypeInfo.ConverterStrategy == Converter.ConverterStrategy, Debug.Assert(PropertyInfoForTypeInfo.EffectiveConverter.ConverterStrategy == Converter.ConverterStrategy,
$"ConverterStrategy from PropertyInfoForTypeInfo.ConverterStrategy ({PropertyInfoForTypeInfo.ConverterStrategy}) does not match converter's ({Converter.ConverterStrategy})"); $"ConverterStrategy from PropertyInfoForTypeInfo.EffectiveConverter.ConverterStrategy ({PropertyInfoForTypeInfo.EffectiveConverter.ConverterStrategy}) does not match converter's ({Converter.ConverterStrategy})");
if (Kind == JsonTypeInfoKind.Object) if (Kind == JsonTypeInfoKind.Object)
{ {
InitializePropertyCache(); InitializePropertyCache();
...@@ -570,7 +564,7 @@ internal string GetPropertyDebugInfo(ReadOnlySpan<byte> unescapedPropertyName) ...@@ -570,7 +564,7 @@ internal string GetPropertyDebugInfo(ReadOnlySpan<byte> unescapedPropertyName)
internal string GetDebugInfo() internal string GetDebugInfo()
{ {
ConverterStrategy strat = PropertyInfoForTypeInfo.ConverterStrategy; ConverterStrategy converterStrategy = Converter.ConverterStrategy;
string jtiTypeName = GetType().Name; string jtiTypeName = GetType().Name;
string typeName = Type.FullName!; string typeName = Type.FullName!;
bool propCacheInitialized = PropertyCache != null; bool propCacheInitialized = PropertyCache != null;
...@@ -579,7 +573,7 @@ internal string GetDebugInfo() ...@@ -579,7 +573,7 @@ internal string GetDebugInfo()
sb.AppendLine("{"); sb.AppendLine("{");
sb.AppendLine($" GetType: {jtiTypeName},"); sb.AppendLine($" GetType: {jtiTypeName},");
sb.AppendLine($" Type: {typeName},"); sb.AppendLine($" Type: {typeName},");
sb.AppendLine($" ConverterStrategy: {strat},"); sb.AppendLine($" ConverterStrategy: {converterStrategy},");
sb.AppendLine($" IsConfigured: {IsConfigured},"); sb.AppendLine($" IsConfigured: {IsConfigured},");
sb.AppendLine($" HasPropertyCache: {propCacheInitialized},"); sb.AppendLine($" HasPropertyCache: {propCacheInitialized},");
......
...@@ -23,7 +23,7 @@ internal ReflectionJsonTypeInfo(JsonConverter converter, JsonSerializerOptions o ...@@ -23,7 +23,7 @@ internal ReflectionJsonTypeInfo(JsonConverter converter, JsonSerializerOptions o
PopulatePolymorphismMetadata(); PopulatePolymorphismMetadata();
MapInterfaceTypesToCallbacks(); MapInterfaceTypesToCallbacks();
if (PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object) if (converter.ConverterStrategy == ConverterStrategy.Object)
{ {
AddPropertiesAndParametersUsingReflection(); AddPropertiesAndParametersUsingReflection();
} }
...@@ -41,7 +41,7 @@ internal ReflectionJsonTypeInfo(JsonConverter converter, JsonSerializerOptions o ...@@ -41,7 +41,7 @@ internal ReflectionJsonTypeInfo(JsonConverter converter, JsonSerializerOptions o
[RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)]
private void AddPropertiesAndParametersUsingReflection() private void AddPropertiesAndParametersUsingReflection()
{ {
Debug.Assert(PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object); Debug.Assert(Converter.ConverterStrategy == ConverterStrategy.Object);
const BindingFlags BindingFlags = const BindingFlags BindingFlags =
BindingFlags.Instance | BindingFlags.Instance |
......
...@@ -216,8 +216,8 @@ public JsonConverter InitializePolymorphicReEntry(JsonTypeInfo derivedJsonTypeIn ...@@ -216,8 +216,8 @@ public JsonConverter InitializePolymorphicReEntry(JsonTypeInfo derivedJsonTypeIn
Debug.Assert(Current.PolymorphicSerializationState == PolymorphicSerializationState.None); Debug.Assert(Current.PolymorphicSerializationState == PolymorphicSerializationState.None);
Current.PolymorphicJsonTypeInfo = Current.JsonTypeInfo; Current.PolymorphicJsonTypeInfo = Current.JsonTypeInfo;
Current.JsonTypeInfo = derivedJsonTypeInfo.PropertyInfoForTypeInfo.JsonTypeInfo; Current.JsonTypeInfo = derivedJsonTypeInfo;
Current.JsonPropertyInfo = Current.JsonTypeInfo.PropertyInfoForTypeInfo; Current.JsonPropertyInfo = derivedJsonTypeInfo.PropertyInfoForTypeInfo;
Current.NumberHandling ??= Current.JsonPropertyInfo.NumberHandling; Current.NumberHandling ??= Current.JsonPropertyInfo.NumberHandling;
Current.PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted; Current.PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted;
SetConstructorArgumentState(); SetConstructorArgumentState();
...@@ -411,6 +411,6 @@ private void SetConstructorArgumentState() ...@@ -411,6 +411,6 @@ private void SetConstructorArgumentState()
} }
[DebuggerBrowsable(DebuggerBrowsableState.Never)] [DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"Path:{JsonPath()} Current: ConverterStrategy.{Current.JsonTypeInfo?.PropertyInfoForTypeInfo.ConverterStrategy}, {Current.JsonTypeInfo?.Type.Name}"; private string DebuggerDisplay => $"Path:{JsonPath()} Current: ConverterStrategy.{Current.JsonTypeInfo?.Converter.ConverterStrategy}, {Current.JsonTypeInfo?.Type.Name}";
} }
} }
...@@ -105,7 +105,7 @@ public void EndElement() ...@@ -105,7 +105,7 @@ public void EndElement()
/// </summary> /// </summary>
public bool IsProcessingDictionary() public bool IsProcessingDictionary()
{ {
return (JsonTypeInfo.PropertyInfoForTypeInfo.ConverterStrategy & ConverterStrategy.Dictionary) != 0; return JsonTypeInfo.Kind is JsonTypeInfoKind.Dictionary;
} }
/// <summary> /// <summary>
...@@ -113,7 +113,7 @@ public bool IsProcessingDictionary() ...@@ -113,7 +113,7 @@ public bool IsProcessingDictionary()
/// </summary> /// </summary>
public bool IsProcessingEnumerable() public bool IsProcessingEnumerable()
{ {
return (JsonTypeInfo.PropertyInfoForTypeInfo.ConverterStrategy & ConverterStrategy.Enumerable) != 0; return JsonTypeInfo.Kind is JsonTypeInfoKind.Enumerable;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
...@@ -152,6 +152,6 @@ internal void ValidateAllRequiredPropertiesAreRead(JsonTypeInfo typeInfo) ...@@ -152,6 +152,6 @@ internal void ValidateAllRequiredPropertiesAreRead(JsonTypeInfo typeInfo)
} }
[DebuggerBrowsable(DebuggerBrowsableState.Never)] [DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"ConverterStrategy.{JsonTypeInfo?.PropertyInfoForTypeInfo.ConverterStrategy}, {JsonTypeInfo?.Type.Name}"; private string DebuggerDisplay => $"ConverterStrategy.{JsonTypeInfo?.Converter.ConverterStrategy}, {JsonTypeInfo?.Type.Name}";
} }
} }
...@@ -430,6 +430,6 @@ static void AppendPropertyName(StringBuilder sb, string? propertyName) ...@@ -430,6 +430,6 @@ static void AppendPropertyName(StringBuilder sb, string? propertyName)
} }
[DebuggerBrowsable(DebuggerBrowsableState.Never)] [DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"Path:{PropertyPath()} Current: ConverterStrategy.{Current.JsonPropertyInfo?.ConverterStrategy}, {Current.JsonTypeInfo?.Type.Name}"; private string DebuggerDisplay => $"Path:{PropertyPath()} Current: ConverterStrategy.{Current.JsonPropertyInfo?.EffectiveConverter.ConverterStrategy}, {Current.JsonTypeInfo?.Type.Name}";
} }
} }
...@@ -166,6 +166,6 @@ public void ExitPolymorphicConverter(bool success) ...@@ -166,6 +166,6 @@ public void ExitPolymorphicConverter(bool success)
} }
[DebuggerBrowsable(DebuggerBrowsableState.Never)] [DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"ConverterStrategy.{JsonTypeInfo?.PropertyInfoForTypeInfo.ConverterStrategy}, {JsonTypeInfo?.Type.Name}"; private string DebuggerDisplay => $"ConverterStrategy.{JsonTypeInfo?.Converter.ConverterStrategy}, {JsonTypeInfo?.Type.Name}";
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册