Skip to content
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 @@ -104,12 +104,6 @@ public static bool ContainsAttribute(this ISymbol memberInfo, string attributeFu
public static bool IsVirtual(this ISymbol symbol)
=> symbol.IsVirtual || symbol.IsOverride || symbol.IsAbstract;

public static bool IsNestedPrivate(this ISymbol symbol)
=> symbol.DeclaredAccessibility is Accessibility.Private && symbol.ContainingType != null;

public static bool IsPublic(this ITypeSymbol symbol)
=> symbol.DeclaredAccessibility is Accessibility.Public && symbol.ContainingType is null;

public static bool IsAssignableFrom(this ITypeSymbol? baseType, ITypeSymbol? type)
{
if (baseType is null || type is null)
Expand Down
24 changes: 9 additions & 15 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,11 +709,7 @@ private void GenerateFastPathFuncForObject(SourceWriter writer, ContextGeneratio
{
string typeRef = typeGenSpec.TypeRef.FullyQualifiedName;

if (!TryFilterSerializableProps(
typeGenSpec,
contextSpec,
out Dictionary<string, PropertyGenerationSpec>? serializableProperties,
out bool castingRequiredForProps))
if (!TryFilterSerializableProps(typeGenSpec, contextSpec, out Dictionary<string, PropertyGenerationSpec>? serializableProperties))
{
// Type uses configuration that doesn't support fast-path: emit a stub that just throws.
GenerateFastPathFuncHeader(writer, typeGenSpec, serializeMethodName, skipNullCheck: true);
Expand Down Expand Up @@ -754,7 +750,13 @@ private void GenerateFastPathFuncForObject(SourceWriter writer, ContextGeneratio
_propertyNames.TryAdd(runtimePropName, propNameVarName);

DefaultCheckType defaultCheckType = GetDefaultCheckType(contextSpec, propertyGenSpec);
string? objectExpr = castingRequiredForProps ? $"(({propertyGenSpec.DeclaringType.FullyQualifiedName}){ValueVarName})" : ValueVarName;

// For properties whose declared type differs from that of the serialized type
// perform an explicit cast -- this is to account for hidden properties or diamond ambiguity.
string? objectExpr = propertyGenSpec.DeclaringType != typeGenSpec.TypeRef
? $"(({propertyGenSpec.DeclaringType.FullyQualifiedName}){ValueVarName})"
: ValueVarName;

string propValueExpr = $"{objectExpr}.{propertyGenSpec.NameSpecifiedInSourceCode}";

switch (defaultCheckType)
Expand Down Expand Up @@ -1353,12 +1355,10 @@ private static bool IsGenerationModeSpecified(TypeGenerationSpec typeSpec, JsonS
public static bool TryFilterSerializableProps(
TypeGenerationSpec typeSpec,
ContextGenerationSpec contextSpec,
[NotNullWhen(true)] out Dictionary<string, PropertyGenerationSpec>? serializableProperties,
out bool castingRequiredForProps)
[NotNullWhen(true)] out Dictionary<string, PropertyGenerationSpec>? serializableProperties)
{
Debug.Assert(typeSpec.PropertyGenSpecs != null);

castingRequiredForProps = false;
serializableProperties = new Dictionary<string, PropertyGenerationSpec>();
HashSet<string>? ignoredMembers = null;

Expand Down Expand Up @@ -1391,10 +1391,6 @@ public static bool TryFilterSerializableProps(
continue;
}

// Using properties from an interface hierarchy -- require explicit casting when
// getting properties in the fast path to account for possible diamond ambiguities.
castingRequiredForProps |= typeSpec.TypeRef.TypeKind is TypeKind.Interface && propGenSpec.PropertyType != typeSpec.TypeRef;

string memberName = propGenSpec.MemberName!;

// The JsonPropertyNameAttribute or naming policy resulted in a collision.
Expand Down Expand Up @@ -1452,12 +1448,10 @@ public static bool TryFilterSerializableProps(
}

Debug.Assert(typeSpec.PropertyGenSpecs.Count >= serializableProperties.Count);
castingRequiredForProps |= typeSpec.PropertyGenSpecs.Count > serializableProperties.Count;
return true;

ReturnFalse:
serializableProperties = null;
castingRequiredForProps = false;
return false;
}

Expand Down
Loading