Skip to content

Commit

Permalink
Add JSON src-gen support for deserializing with parameterized ctors (#…
Browse files Browse the repository at this point in the history
…56354)

* Add JSON src-gen support for deserializing with parameterized ctors

* Address API review and PR feedback

* Fix issue with init-only properties

* Make misc fixes for build
  • Loading branch information
layomia committed Aug 6, 2021
1 parent ce3447c commit 91021fe
Show file tree
Hide file tree
Showing 82 changed files with 1,785 additions and 740 deletions.
4 changes: 2 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
</ItemGroup>
<PropertyGroup>
<!-- For source generator support we need to target a pinned version in order to be able to run on older versions of Roslyn -->
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>3.8.0</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisVersion>3.8.0</MicrosoftCodeAnalysisVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>3.9.0</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisVersion>3.9.0</MicrosoftCodeAnalysisVersion>
</PropertyGroup>
<PropertyGroup>
<!-- Code analysis dependencies -->
Expand Down
11 changes: 11 additions & 0 deletions src/libraries/System.Text.Json/Common/JsonConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Text.Json
{
internal static partial class JsonConstants
{
// The maximum number of parameters a constructor can have where it can be supported by the serializer.
public const int MaxParameterCount = 64;
}
}
81 changes: 81 additions & 0 deletions src/libraries/System.Text.Json/Common/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Reflection;
using System.Collections.Generic;
#if !BUILDING_SOURCE_GENERATOR
using System.Diagnostics.CodeAnalysis;
#endif
Expand Down Expand Up @@ -229,5 +230,85 @@ public static bool IsVirtual(this PropertyInfo? propertyInfo)
Debug.Assert(propertyInfo != null);
return propertyInfo != null && (propertyInfo.GetMethod?.IsVirtual == true || propertyInfo.SetMethod?.IsVirtual == true);
}

public static bool IsKeyValuePair(this Type type, Type? keyValuePairType = null)
{
if (!type.IsGenericType)
{
return false;
}

// Work around not being able to use typeof(KeyValuePair<,>) directly during compile-time src gen type analysis.
keyValuePairType ??= typeof(KeyValuePair<,>);

Type generic = type.GetGenericTypeDefinition();
return generic == keyValuePairType;
}

public static bool TryGetDeserializationConstructor(
#if !BUILDING_SOURCE_GENERATOR
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
#endif
this Type type,
bool useDefaultCtorInAnnotatedStructs,
out ConstructorInfo? deserializationCtor)
{
ConstructorInfo? ctorWithAttribute = null;
ConstructorInfo? publicParameterlessCtor = null;
ConstructorInfo? lonePublicCtor = null;

ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);

if (constructors.Length == 1)
{
lonePublicCtor = constructors[0];
}

foreach (ConstructorInfo constructor in constructors)
{
if (HasJsonConstructorAttribute(constructor))
{
if (ctorWithAttribute != null)
{
deserializationCtor = null;
return false;
}

ctorWithAttribute = constructor;
}
else if (constructor.GetParameters().Length == 0)
{
publicParameterlessCtor = constructor;
}
}

// For correctness, throw if multiple ctors have [JsonConstructor], even if one or more are non-public.
ConstructorInfo? dummyCtorWithAttribute = ctorWithAttribute;

constructors = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (ConstructorInfo constructor in constructors)
{
if (HasJsonConstructorAttribute(constructor))
{
if (dummyCtorWithAttribute != null)
{
deserializationCtor = null;
return false;
}

dummyCtorWithAttribute = constructor;
}
}

// Structs will use default constructor if attribute isn't used.
if (useDefaultCtorInAnnotatedStructs && type.IsValueType && ctorWithAttribute == null)
{
deserializationCtor = null;
return true;
}

deserializationCtor = ctorWithAttribute ?? publicParameterlessCtor ?? lonePublicCtor;
return true;
}
}
}
2 changes: 1 addition & 1 deletion src/libraries/System.Text.Json/gen/ClassType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ internal enum ClassType
Enumerable = 4,
Dictionary = 5,
Nullable = 6,
Enum = 7,
Enum = 7
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal sealed class ContextGenerationSpec

public List<TypeGenerationSpec> RootSerializableTypes { get; } = new();

public HashSet<TypeGenerationSpec>? NullableUnderlyingTypes { get; } = new();
public HashSet<TypeGenerationSpec>? ImplicitlyRegisteredTypes { get; } = new();

public List<string> ContextClassDeclarationList { get; init; }

Expand Down

0 comments on commit 91021fe

Please sign in to comment.