Skip to content

Update source-gen APIs according to review #59042

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
merged 4 commits into from
Sep 17, 2021
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 @@ -30,11 +30,6 @@ sealed class JsonSourceGenerationOptionsAttribute : JsonAttribute
/// </summary>
public bool IgnoreReadOnlyProperties { get; set; }

/// <summary>
/// Specifies whether to ignore custom converters provided at run time.
/// </summary>
public bool IgnoreRuntimeCustomConverters { get; set; }

/// <summary>
/// Specifies whether to include fields for serialization and deserialization.
/// </summary>
Expand Down
213 changes: 111 additions & 102 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ private sealed class Parser
private const string SystemTextJsonNamespace = "System.Text.Json";
private const string JsonConverterAttributeFullName = "System.Text.Json.Serialization.JsonConverterAttribute";
private const string JsonConverterFactoryFullName = "System.Text.Json.Serialization.JsonConverterFactory";
private const string JsonArrayFullName = "System.Text.Json.Nodes.JsonArray";
private const string JsonElementFullName = "System.Text.Json.JsonElement";
private const string JsonExtensionDataAttributeFullName = "System.Text.Json.Serialization.JsonExtensionDataAttribute";
private const string JsonNodeFullName = "System.Text.Json.Nodes.JsonNode";
private const string JsonObjectFullName = "System.Text.Json.Nodes.JsonObject";
private const string JsonValueFullName = "System.Text.Json.Nodes.JsonValue";
private const string JsonIgnoreAttributeFullName = "System.Text.Json.Serialization.JsonIgnoreAttribute";
private const string JsonIgnoreConditionFullName = "System.Text.Json.Serialization.JsonIgnoreCondition";
private const string JsonIncludeAttributeFullName = "System.Text.Json.Serialization.JsonIncludeAttribute";
Expand Down Expand Up @@ -80,8 +83,11 @@ private sealed class Parser
private readonly Type? _guidType;
private readonly Type? _uriType;
private readonly Type? _versionType;
private readonly Type? _jsonArrayType;
private readonly Type? _jsonElementType;
private readonly Type? _jsonNodeType;
private readonly Type? _jsonObjectType;
private readonly Type? _jsonValueType;

// Unsupported types
private readonly Type _typeType;
Expand Down Expand Up @@ -193,8 +199,11 @@ public Parser(Compilation compilation, in JsonSourceGenerationContext sourceGene
_guidType = _metadataLoadContext.Resolve(typeof(Guid));
_uriType = _metadataLoadContext.Resolve(typeof(Uri));
_versionType = _metadataLoadContext.Resolve(typeof(Version));
_jsonArrayType = _metadataLoadContext.Resolve(JsonArrayFullName);
_jsonElementType = _metadataLoadContext.Resolve(JsonElementFullName);
_jsonNodeType = _metadataLoadContext.Resolve(JsonNodeFullName);
_jsonObjectType = _metadataLoadContext.Resolve(JsonObjectFullName);
_jsonValueType = _metadataLoadContext.Resolve(JsonValueFullName);

// Unsupported types.
_typeType = _metadataLoadContext.Resolve(typeof(Type));
Expand Down Expand Up @@ -565,14 +574,6 @@ private static bool TryGetClassDeclarationList(INamedTypeSymbol typeSymbol, [Not
}
}
break;
case nameof(JsonSourceGenerationOptionsAttribute.IgnoreRuntimeCustomConverters):
{
if (bool.TryParse(propertyValueStr, out bool value))
{
options.IgnoreRuntimeCustomConverters = value;
}
}
break;
case nameof(JsonSourceGenerationOptionsAttribute.IncludeFields):
{
if (bool.TryParse(propertyValueStr, out bool value))
Expand Down Expand Up @@ -640,7 +641,7 @@ private TypeGenerationSpec GetOrAddTypeGenerationSpec(Type type, JsonSourceGener
string? converterInstatiationLogic = null;
bool implementsIJsonOnSerialized = false;
bool implementsIJsonOnSerializing = false;
bool hasEncounteredInitOnlyProperties = false;
bool hasInitOnlyProperties = false;
bool hasTypeFactoryConverter = false;
bool hasPropertyFactoryConverters = false;

Expand Down Expand Up @@ -972,10 +973,10 @@ void CacheMemberHelper()
_implicitlyRegisteredTypes.Add(dataExtensionPropGenSpec);
}

if (!hasEncounteredInitOnlyProperties && spec.CanUseSetter && spec.IsInitOnlySetter)
if (!hasInitOnlyProperties && spec.CanUseSetter && spec.IsInitOnlySetter)
{
_sourceGenerationContext.ReportDiagnostic(Diagnostic.Create(InitOnlyPropertyDeserializationNotSupported, Location.None, new string[] { type.Name }));
hasEncounteredInitOnlyProperties = true;
hasInitOnlyProperties = true;
}

if (spec.HasJsonInclude && (!spec.CanUseGetter || !spec.CanUseSetter || !spec.IsPublic))
Expand Down Expand Up @@ -1454,8 +1455,11 @@ private void PopulateKnownTypes()
AddTypeIfNotNull(_knownTypes, _guidType);
AddTypeIfNotNull(_knownTypes, _uriType);
AddTypeIfNotNull(_knownTypes, _versionType);
AddTypeIfNotNull(_knownTypes, _jsonArrayType);
AddTypeIfNotNull(_knownTypes, _jsonElementType);
AddTypeIfNotNull(_knownTypes, _jsonNodeType);
AddTypeIfNotNull(_knownTypes, _jsonObjectType);
AddTypeIfNotNull(_knownTypes, _jsonValueType);

_knownUnsupportedTypes.Add(_typeType);
_knownUnsupportedTypes.Add(_serializationInfoType);
Expand Down
9 changes: 0 additions & 9 deletions src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,6 @@ internal class TypeGenerationSpec
public bool HasPropertyFactoryConverters { get; private set; }
public bool HasTypeFactoryConverter { get; private set; }

public string FastPathSerializeMethodName
{
get
{
Debug.Assert(GenerateSerializationLogic);
return $"{TypeInfoPropertyName}Serialize";
}
}

public string? ImmutableCollectionBuilderName
{
get
Expand Down
Loading