Skip to content

Commit

Permalink
Address API review and PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
layomia committed Aug 4, 2021
1 parent 7b84fe6 commit e02ace7
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 290 deletions.
180 changes: 64 additions & 116 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public ConstructorInfoWrapper(IMethodSymbol ctor, MetadataLoadContextInternal me

public override Type DeclaringType => _ctor.ContainingType.AsType(_metadataLoadContext);

public override MethodAttributes Attributes => throw new NotImplementedException();
private MethodAttributes? _attributes;

public override MethodAttributes Attributes => _attributes ??= _ctor.GetMethodAttributes();

public override RuntimeMethodHandle MethodHandle => throw new NotSupportedException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,7 @@ public MethodInfoWrapper(IMethodSymbol method, MetadataLoadContextInternal metad

private MethodAttributes? _attributes;

public override MethodAttributes Attributes
{
get
{
if (!_attributes.HasValue)
{
_attributes = default(MethodAttributes);

if (_method.IsAbstract)
{
_attributes |= MethodAttributes.Abstract;
}

if (_method.IsStatic)
{
_attributes |= MethodAttributes.Static;
}

if (_method.IsVirtual || _method.IsOverride)
{
_attributes |= MethodAttributes.Virtual;
}

switch (_method.DeclaredAccessibility)
{
case Accessibility.Public:
_attributes |= MethodAttributes.Public;
break;
case Accessibility.Private:
_attributes |= MethodAttributes.Private;
break;
case Accessibility.Internal:
_attributes |= MethodAttributes.Assembly;
break;
}
}

return _attributes.Value;
}
}
public override MethodAttributes Attributes => _attributes ??= _method.GetMethodAttributes();

public override RuntimeMethodHandle MethodHandle => throw new NotSupportedException();

Expand Down
35 changes: 35 additions & 0 deletions src/libraries/System.Text.Json/gen/Reflection/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,40 @@ public static IEnumerable<INamedTypeSymbol> BaseTypes(this INamedTypeSymbol type
t = t.BaseType;
}
}

public static MethodAttributes GetMethodAttributes(this IMethodSymbol methodSymbol)
{
MethodAttributes attributes = default(MethodAttributes);

if (methodSymbol.IsAbstract)
{
attributes |= MethodAttributes.Abstract;
}

if (methodSymbol.IsStatic)
{
attributes |= MethodAttributes.Static;
}

if (methodSymbol.IsVirtual || methodSymbol.IsOverride)
{
attributes |= MethodAttributes.Virtual;
}

switch (methodSymbol.DeclaredAccessibility)
{
case Accessibility.Public:
attributes |= MethodAttributes.Public;
break;
case Accessibility.Private:
attributes |= MethodAttributes.Private;
break;
case Accessibility.Internal:
attributes |= MethodAttributes.Assembly;
break;
}

return attributes;
}
}
}
24 changes: 16 additions & 8 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,7 @@ public static partial class JsonMetadataServices
public static JsonTypeInfo<TCollection> CreateIReadOnlyDictionaryInfo<TCollection, TKey, TValue>(System.Text.Json.JsonSerializerOptions options, System.Func<TCollection> createObjectFunc, JsonTypeInfo keyInfo, JsonTypeInfo valueInfo, System.Text.Json.Serialization.JsonNumberHandling numberHandling, System.Action<Utf8JsonWriter, TCollection>? serializeFunc) where TCollection : System.Collections.Generic.IReadOnlyDictionary<TKey, TValue> where TKey : notnull { throw null; }
public static JsonTypeInfo<TCollection> CreateISetInfo<TCollection, TElement>(System.Text.Json.JsonSerializerOptions options, System.Func<TCollection>? createObjectFunc, System.Text.Json.Serialization.Metadata.JsonTypeInfo elementInfo, System.Text.Json.Serialization.JsonNumberHandling numberHandling, System.Action<Utf8JsonWriter, TCollection>? serializeFunc) where TCollection : System.Collections.Generic.ISet<TElement> { throw null; }
public static System.Text.Json.Serialization.Metadata.JsonTypeInfo<TCollection> CreateListInfo<TCollection, TElement>(System.Text.Json.JsonSerializerOptions options, System.Func<TCollection>? createObjectFunc, System.Text.Json.Serialization.Metadata.JsonTypeInfo elementInfo, System.Text.Json.Serialization.JsonNumberHandling numberHandling, System.Action<System.Text.Json.Utf8JsonWriter, TCollection>? serializeFunc) where TCollection : System.Collections.Generic.List<TElement> { throw null; }
public static System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> CreateObjectInfo<T>(System.Text.Json.JsonSerializerOptions options, System.Func<T>? createObjectFunc, System.Func<System.Text.Json.Serialization.JsonSerializerContext, System.Text.Json.Serialization.Metadata.JsonPropertyInfo[]>? propInitFunc, System.Text.Json.Serialization.JsonNumberHandling numberHandling, System.Action<System.Text.Json.Utf8JsonWriter, T>? serializeFunc) where T : notnull { throw null; }
public static System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> CreateObjectInfo<T>(System.Text.Json.JsonSerializerOptions options, System.Func<object[], T>? createObjectWithArgsFunc, System.Func<System.Text.Json.Serialization.JsonSerializerContext, System.Text.Json.Serialization.Metadata.JsonPropertyInfo[]>? propInitFunc, Func<System.Text.Json.Serialization.Metadata.JsonParameterClrInfo[]>? ctorParamInitFunc, System.Text.Json.Serialization.JsonNumberHandling numberHandling, System.Action<System.Text.Json.Utf8JsonWriter, T>? serializeFunc) where T : notnull { throw null; }
public static System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> CreateObjectInfo<T>(System.Text.Json.JsonSerializerOptions options, System.Text.Json.Serialization.Metadata.JsonObjectInfoValues<T> objectInfo) where T : notnull { throw null; }
public static System.Text.Json.Serialization.Metadata.JsonPropertyInfo CreatePropertyInfo<T>(System.Text.Json.JsonSerializerOptions options, bool isProperty, bool isPublic, bool isVirtual, System.Type declaringType, System.Text.Json.Serialization.Metadata.JsonTypeInfo propertyTypeInfo, System.Text.Json.Serialization.JsonConverter<T>? converter, System.Func<object, T>? getter, System.Action<object, T>? setter, System.Text.Json.Serialization.JsonIgnoreCondition? ignoreCondition, bool hasJsonInclude, System.Text.Json.Serialization.JsonNumberHandling? numberHandling, string propertyName, string? jsonPropertyName) { throw null; }
public static JsonTypeInfo<TCollection> CreateQueueInfo<TCollection, TElement>(System.Text.Json.JsonSerializerOptions options, System.Func<TCollection>? createObjectFunc, System.Text.Json.Serialization.Metadata.JsonTypeInfo elementInfo, System.Text.Json.Serialization.JsonNumberHandling numberHandling, System.Action<Utf8JsonWriter, TCollection>? serializeFunc) where TCollection : System.Collections.Generic.Queue<TElement> { throw null; }
public static JsonTypeInfo<TCollection> CreateStackInfo<TCollection, TElement>(System.Text.Json.JsonSerializerOptions options, System.Func<TCollection>? createObjectFunc, System.Text.Json.Serialization.Metadata.JsonTypeInfo elementInfo, System.Text.Json.Serialization.JsonNumberHandling numberHandling, System.Action<Utf8JsonWriter, TCollection>? serializeFunc) where TCollection : System.Collections.Generic.Stack<TElement> { throw null; }
Expand All @@ -996,13 +995,22 @@ public static partial class JsonMetadataServices
public static System.Text.Json.Serialization.JsonConverter<T> GetEnumConverter<T>(System.Text.Json.JsonSerializerOptions options) where T : struct { throw null; }
public static System.Text.Json.Serialization.JsonConverter<T?> GetNullableConverter<T>(System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> underlyingTypeInfo) where T : struct { throw null; }
}
public struct JsonParameterClrInfo
public sealed partial class JsonObjectInfoValues<T>
{
public object? DefaultValue { readonly get { throw null; } set { } }
public bool HasDefaultValue { readonly get { throw null; } set { } }
public string Name { readonly get { throw null; } set { } }
public System.Type ParameterType { readonly get { throw null; } set { } }
public int Position { readonly get { throw null; } set { } }
public System.Func<System.Text.Json.Serialization.Metadata.JsonParameterInfoValues[]>? ConstructorParameterMetadataInitializer { get; init; }
public System.Text.Json.Serialization.JsonNumberHandling NumberHandling { get; init; }
public System.Func<T>? ObjectCreator { get; init; }
public System.Func<object[], T>? ObjectWithParameterizedConstructorCreator { get; init; }
public System.Func<System.Text.Json.Serialization.JsonSerializerContext, System.Text.Json.Serialization.Metadata.JsonPropertyInfo[]>? PropertyMetadataInitializer { get; init; }
public System.Action<System.Text.Json.Utf8JsonWriter, T>? SerializeHandler { get; init; }
}
public sealed class JsonParameterInfoValues
{
public object? DefaultValue { get { throw null; } init { } }
public bool HasDefaultValue { get { throw null; } init { } }
public string Name { get { throw null; } init { } }
public System.Type ParameterType { get { throw null; } init { } }
public int Position { get { throw null; } init { } }
}
public abstract partial class JsonPropertyInfo
{
Expand Down
5 changes: 3 additions & 2 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<Compile Include="..\Common\JsonCamelCaseNamingPolicy.cs" Link="Common\System\Text\Json\JsonCamelCaseNamingPolicy.cs" />
<Compile Include="..\Common\JsonNamingPolicy.cs" Link="Common\System\Text\Json\JsonNamingPolicy.cs" />
<Compile Include="..\Common\JsonAttribute.cs" Link="Common\System\Text\Json\Serialization\JsonAttribute.cs" />
<Compile Include="..\Common\JsonConstants.cs" Link ="Common\System\Text\Json\JsonConstants.cs" />
<Compile Include="..\Common\JsonConstants.cs" Link="Common\System\Text\Json\JsonConstants.cs" />
<Compile Include="..\Common\JsonHelpers.cs" Link="Common\System\Text\Json\JsonHelpers.cs" />
<Compile Include="..\Common\JsonIgnoreCondition.cs" Link="Common\System\Text\Json\Serialization\JsonIgnoreCondition.cs" />
<Compile Include="..\Common\JsonKnownNamingPolicy.cs" Link="Common\System\Text\Json\Serialization\JsonKnownNamingPolicy.cs" />
Expand Down Expand Up @@ -111,6 +111,7 @@
<Compile Include="System\Text\Json\Serialization\Metadata\FSharpCoreReflectionProxy.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.Collections.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.Converters.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonObjectInfoValues.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoInternalOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.cs" />
Expand Down Expand Up @@ -221,7 +222,7 @@
<Compile Include="System\Text\Json\Serialization\JsonStringEnumConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonUnknownTypeHandling.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\GenericMethodHolder.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonParameterClrInfo.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonParameterInfoValues.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonParameterInfo.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonParameterInfoOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonPropertyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,54 +104,16 @@ public static JsonPropertyInfo CreatePropertyInfo<T>(
/// Creates metadata for a complex class or struct.
/// </summary>
/// <param name="options">The <see cref="JsonSerializerOptions"/> to initialize the metadata with.</param>
/// <param name="createObjectFunc">Provides a mechanism to create an instance of the class or struct when deserializing, using a parameterless constructor.</param>
/// <param name="propInitFunc">Provides a mechanism to initialize metadata for properties and fields of the class or struct.</param>
/// <param name="serializeFunc">Provides a serialization implementation for instances of the class or struct which assumes options specified by <see cref="JsonSourceGenerationOptionsAttribute"/>.</param>
/// <param name="numberHandling">Specifies how number properties and fields should be processed when serializing and deserializing.</param>
/// <param name="objectInfo">Provides serialization metadata about an object type with constructors, properties, and fields.</param>
/// <typeparam name="T">The type of the class or struct.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <paramref name="options"/> and <paramref name="propInitFunc"/> are both null.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="objectInfo"/> is null.</exception>
/// <returns>A <see cref="JsonTypeInfo{T}"/> instance representing the class or struct.</returns>
public static JsonTypeInfo<T> CreateObjectInfo<T>(
JsonSerializerOptions options,
Func<T>? createObjectFunc,
Func<JsonSerializerContext, JsonPropertyInfo[]>? propInitFunc,
JsonNumberHandling numberHandling,
Action<Utf8JsonWriter, T>? serializeFunc)
where T : notnull
=> new JsonTypeInfoInternal<T>(
options,
createObjectFunc,
propInitFunc,
numberHandling,
serializeFunc);

/// <summary>
/// Creates metadata for a complex class or struct.
/// </summary>
/// <param name="options">The <see cref="JsonSerializerOptions"/> to initialize the metadata with.</param>
/// <param name="createObjectWithArgsFunc">Provides a mechanism to create an instance of the class or struct when deserializing, using a parameterized constructor.</param>
/// <param name="propInitFunc">Provides a mechanism to initialize metadata for properties and fields of the class or struct.</param>
/// <param name="ctorParamInitFunc">Provides a mechanism to initialize metadata for a parameterized constructor of the class or struct to be used when deserializing.</param>
/// <param name="serializeFunc">Provides a serialization implementation for instances of the class or struct which assumes options specified by <see cref="JsonSourceGenerationOptionsAttribute"/>.</param>
/// <param name="numberHandling">Specifies how number properties and fields should be processed when serializing and deserializing.</param>
/// <typeparam name="T">The type of the class or struct.</typeparam>
/// <exception cref="InvalidOperationException">Thrown when <paramref name="options"/> and <paramref name="propInitFunc"/> are both null.</exception>
/// <returns>A <see cref="JsonTypeInfo{T}"/> instance representing the class or struct.</returns>
public static JsonTypeInfo<T> CreateObjectInfo<T>(
JsonSerializerOptions options,
Func<object[], T>? createObjectWithArgsFunc,
Func<JsonSerializerContext, JsonPropertyInfo[]>? propInitFunc,
Func<JsonParameterClrInfo[]>? ctorParamInitFunc,
JsonNumberHandling numberHandling,
Action<Utf8JsonWriter, T>? serializeFunc)
where T : notnull
=> new JsonTypeInfoInternal<T>(
options,
createObjectWithArgsFunc,
propInitFunc,
ctorParamInitFunc,
numberHandling,
serializeFunc);
public static JsonTypeInfo<T> CreateObjectInfo<T>(JsonSerializerOptions options, JsonObjectInfoValues<T> objectInfo) where T : notnull
{
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(objectInfo);
return new JsonTypeInfoInternal<T>(options, objectInfo);
}

/// <summary>
/// Creates metadata for a primitive or a type with a custom converter.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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.Serialization.Metadata
{
/// <summary>
/// Provides serialization metadata about an object type with constructors, properties, and fields.
/// </summary>
/// <typeparam name="T">The object type to serialize or deserialize.</typeparam>
public sealed class JsonObjectInfoValues<T>
{
/// <summary>
/// Provides a mechanism to create an instance of the class or struct when deserializing, using a parameterless constructor.
/// </summary>
public Func<T>? ObjectCreator { get; init; }

/// <summary>
/// Provides a mechanism to create an instance of the class or struct when deserializing, using a parameterized constructor.
/// </summary>
public Func<object[], T>? ObjectWithParameterizedConstructorCreator { get; init; }

/// <summary>
/// Provides a mechanism to initialize metadata for properties and fields of the class or struct.
/// </summary>
public Func<JsonSerializerContext, JsonPropertyInfo[]>? PropertyMetadataInitializer { get; init; }

/// <summary>
/// Provides a mechanism to initialize metadata for a parameterized constructor of the class or struct to be used when deserializing.
/// </summary>
public Func<JsonParameterInfoValues[]>? ConstructorParameterMetadataInitializer { get; init; }

/// <summary>
/// Specifies how number properties and fields should be processed when serializing and deserializing.
/// </summary>
public JsonNumberHandling NumberHandling { get; init; }

/// <summary>
/// Provides a serialization implementation for instances of the class or struct which assumes options specified by <see cref="JsonSourceGenerationOptionsAttribute"/>.
/// </summary>
public Action<Utf8JsonWriter, T>? SerializeHandler { get; init; }
}
}
Loading

0 comments on commit e02ace7

Please sign in to comment.