Skip to content

Commit 52324fe

Browse files
[release/6.0-rc2] Update source-gen APIs according to review (#59243)
* Update source-gen APIs according to review * Rename fast-path func name and add src-gen/JsonNode interop support * Address review feedback * Disable src-gen tests in browser target OS Co-authored-by: Layomi Akinrinade <[email protected]>
1 parent 70f8bb2 commit 52324fe

39 files changed

+1075
-1069
lines changed

src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ sealed class JsonSourceGenerationOptionsAttribute : JsonAttribute
3030
/// </summary>
3131
public bool IgnoreReadOnlyProperties { get; set; }
3232

33-
/// <summary>
34-
/// Specifies whether to ignore custom converters provided at run time.
35-
/// </summary>
36-
public bool IgnoreRuntimeCustomConverters { get; set; }
37-
3833
/// <summary>
3934
/// Specifies whether to include fields for serialization and deserialization.
4035
/// </summary>

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Lines changed: 111 additions & 102 deletions
Large diffs are not rendered by default.

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ private sealed class Parser
2525
private const string SystemTextJsonNamespace = "System.Text.Json";
2626
private const string JsonConverterAttributeFullName = "System.Text.Json.Serialization.JsonConverterAttribute";
2727
private const string JsonConverterFactoryFullName = "System.Text.Json.Serialization.JsonConverterFactory";
28+
private const string JsonArrayFullName = "System.Text.Json.Nodes.JsonArray";
2829
private const string JsonElementFullName = "System.Text.Json.JsonElement";
2930
private const string JsonExtensionDataAttributeFullName = "System.Text.Json.Serialization.JsonExtensionDataAttribute";
31+
private const string JsonNodeFullName = "System.Text.Json.Nodes.JsonNode";
3032
private const string JsonObjectFullName = "System.Text.Json.Nodes.JsonObject";
33+
private const string JsonValueFullName = "System.Text.Json.Nodes.JsonValue";
3134
private const string JsonIgnoreAttributeFullName = "System.Text.Json.Serialization.JsonIgnoreAttribute";
3235
private const string JsonIgnoreConditionFullName = "System.Text.Json.Serialization.JsonIgnoreCondition";
3336
private const string JsonIncludeAttributeFullName = "System.Text.Json.Serialization.JsonIncludeAttribute";
@@ -80,8 +83,11 @@ private sealed class Parser
8083
private readonly Type? _guidType;
8184
private readonly Type? _uriType;
8285
private readonly Type? _versionType;
86+
private readonly Type? _jsonArrayType;
8387
private readonly Type? _jsonElementType;
88+
private readonly Type? _jsonNodeType;
8489
private readonly Type? _jsonObjectType;
90+
private readonly Type? _jsonValueType;
8591

8692
// Unsupported types
8793
private readonly Type _typeType;
@@ -193,8 +199,11 @@ public Parser(Compilation compilation, in JsonSourceGenerationContext sourceGene
193199
_guidType = _metadataLoadContext.Resolve(typeof(Guid));
194200
_uriType = _metadataLoadContext.Resolve(typeof(Uri));
195201
_versionType = _metadataLoadContext.Resolve(typeof(Version));
202+
_jsonArrayType = _metadataLoadContext.Resolve(JsonArrayFullName);
196203
_jsonElementType = _metadataLoadContext.Resolve(JsonElementFullName);
204+
_jsonNodeType = _metadataLoadContext.Resolve(JsonNodeFullName);
197205
_jsonObjectType = _metadataLoadContext.Resolve(JsonObjectFullName);
206+
_jsonValueType = _metadataLoadContext.Resolve(JsonValueFullName);
198207

199208
// Unsupported types.
200209
_typeType = _metadataLoadContext.Resolve(typeof(Type));
@@ -565,14 +574,6 @@ private static bool TryGetClassDeclarationList(INamedTypeSymbol typeSymbol, [Not
565574
}
566575
}
567576
break;
568-
case nameof(JsonSourceGenerationOptionsAttribute.IgnoreRuntimeCustomConverters):
569-
{
570-
if (bool.TryParse(propertyValueStr, out bool value))
571-
{
572-
options.IgnoreRuntimeCustomConverters = value;
573-
}
574-
}
575-
break;
576577
case nameof(JsonSourceGenerationOptionsAttribute.IncludeFields):
577578
{
578579
if (bool.TryParse(propertyValueStr, out bool value))
@@ -640,7 +641,7 @@ private TypeGenerationSpec GetOrAddTypeGenerationSpec(Type type, JsonSourceGener
640641
string? converterInstatiationLogic = null;
641642
bool implementsIJsonOnSerialized = false;
642643
bool implementsIJsonOnSerializing = false;
643-
bool hasEncounteredInitOnlyProperties = false;
644+
bool hasInitOnlyProperties = false;
644645
bool hasTypeFactoryConverter = false;
645646
bool hasPropertyFactoryConverters = false;
646647

@@ -972,10 +973,10 @@ void CacheMemberHelper()
972973
_implicitlyRegisteredTypes.Add(dataExtensionPropGenSpec);
973974
}
974975

975-
if (!hasEncounteredInitOnlyProperties && spec.CanUseSetter && spec.IsInitOnlySetter)
976+
if (!hasInitOnlyProperties && spec.CanUseSetter && spec.IsInitOnlySetter)
976977
{
977978
_sourceGenerationContext.ReportDiagnostic(Diagnostic.Create(InitOnlyPropertyDeserializationNotSupported, Location.None, new string[] { type.Name }));
978-
hasEncounteredInitOnlyProperties = true;
979+
hasInitOnlyProperties = true;
979980
}
980981

981982
if (spec.HasJsonInclude && (!spec.CanUseGetter || !spec.CanUseSetter || !spec.IsPublic))
@@ -1454,8 +1455,11 @@ private void PopulateKnownTypes()
14541455
AddTypeIfNotNull(_knownTypes, _guidType);
14551456
AddTypeIfNotNull(_knownTypes, _uriType);
14561457
AddTypeIfNotNull(_knownTypes, _versionType);
1458+
AddTypeIfNotNull(_knownTypes, _jsonArrayType);
14571459
AddTypeIfNotNull(_knownTypes, _jsonElementType);
1460+
AddTypeIfNotNull(_knownTypes, _jsonNodeType);
14581461
AddTypeIfNotNull(_knownTypes, _jsonObjectType);
1462+
AddTypeIfNotNull(_knownTypes, _jsonValueType);
14591463

14601464
_knownUnsupportedTypes.Add(_typeType);
14611465
_knownUnsupportedTypes.Add(_serializationInfoType);

src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,6 @@ internal class TypeGenerationSpec
7272
public bool HasPropertyFactoryConverters { get; private set; }
7373
public bool HasTypeFactoryConverter { get; private set; }
7474

75-
public string FastPathSerializeMethodName
76-
{
77-
get
78-
{
79-
Debug.Assert(GenerateSerializationLogic);
80-
return $"{TypeInfoPropertyName}Serialize";
81-
}
82-
}
83-
8475
public string? ImmutableCollectionBuilderName
8576
{
8677
get

0 commit comments

Comments
 (0)