Skip to content

Commit 118c530

Browse files
authored
Clean up JSON source gen APIs (#54527)
* Clean up JSON source gen APIs * Address review feedback * Simplify GenerateX computation
1 parent 333c4e7 commit 118c530

19 files changed

+338
-147
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ enum JsonKnownNamingPolicy
2121
/// <summary>
2222
/// Specifies that the built-in <see cref="Json.JsonNamingPolicy.CamelCase"/> be used to convert JSON property names.
2323
/// </summary>
24-
BuiltInCamelCase = 1
24+
CamelCase = 1
2525
}
2626
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#if !BUILDING_SOURCE_GENERATOR
45
using System.Text.Json.Serialization.Metadata;
6+
#endif
57

68
namespace System.Text.Json.Serialization
79
{
@@ -10,7 +12,13 @@ namespace System.Text.Json.Serialization
1012
/// when serializing and deserializing instances of the specified type and types in its object graph.
1113
/// </summary>
1214
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
13-
public sealed class JsonSerializableAttribute : JsonAttribute
15+
16+
#if BUILDING_SOURCE_GENERATOR
17+
internal
18+
#else
19+
public
20+
#endif
21+
sealed class JsonSerializableAttribute : JsonAttribute
1422
{
1523
/// <summary>
1624
/// Initializes a new instance of <see cref="JsonSerializableAttribute"/> with the specified type.
@@ -28,7 +36,8 @@ public JsonSerializableAttribute(Type type) { }
2836
public string? TypeInfoPropertyName { get; set; }
2937

3038
/// <summary>
31-
/// Determines what the source generator should generate for the type.
39+
/// Determines what the source generator should generate for the type. If the value is <see cref="JsonSourceGenerationMode.Default"/>,
40+
/// then the setting specified on <see cref="JsonSourceGenerationOptionsAttribute.GenerationMode"/> will be used.
3241
/// </summary>
3342
public JsonSourceGenerationMode GenerationMode { get; set; }
3443
}

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ namespace System.Text.Json.Serialization
1515
enum JsonSourceGenerationMode
1616
{
1717
/// <summary>
18-
/// Instructs the JSON source generator to generate serialization logic and type metadata to fallback to
19-
/// when the run-time options are not compatible with the indicated <see cref="JsonSerializerOptionsAttribute"/>.
18+
/// When specified on <see cref="JsonSourceGenerationOptionsAttribute.GenerationMode"/>, indicates that both type-metadata initialization logic
19+
/// and optimized serialization logic should be generated for all types. When specified on <see cref="JsonSerializableAttribute.GenerationMode"/>,
20+
/// indicates that the setting on <see cref="JsonSourceGenerationOptionsAttribute.GenerationMode"/> should be used.
2021
/// </summary>
21-
/// <remarks>
22-
/// This mode supports all <see cref="JsonSerializer"/> features.
23-
/// </remarks>
24-
MetadataAndSerialization = 0,
22+
Default = 0,
2523

2624
/// <summary>
2725
/// Instructs the JSON source generator to generate type-metadata initialization logic.
@@ -32,7 +30,7 @@ enum JsonSourceGenerationMode
3230
Metadata = 1,
3331

3432
/// <summary>
35-
/// Instructs the JSON source generator to generate serialization logic.
33+
/// Instructs the JSON source generator to generate optimized serialization logic.
3634
/// </summary>
3735
/// <remarks>
3836
/// This mode supports only a subset of <see cref="JsonSerializer"/> features.

src/libraries/System.Text.Json/Common/JsonSerializerOptionsAttribute.cs renamed to src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace System.Text.Json.Serialization
1313
#else
1414
public
1515
#endif
16-
class JsonSerializerOptionsAttribute : JsonAttribute
16+
class JsonSourceGenerationOptionsAttribute : JsonAttribute
1717
{
1818
/// <summary>
1919
/// Specifies the default ignore condition.
@@ -43,11 +43,16 @@ class JsonSerializerOptionsAttribute : JsonAttribute
4343
/// <summary>
4444
/// Specifies a built-in naming polices to convert JSON property names with.
4545
/// </summary>
46-
public JsonKnownNamingPolicy NamingPolicy { get; set; }
46+
public JsonKnownNamingPolicy PropertyNamingPolicy { get; set; }
4747

4848
/// <summary>
4949
/// Specifies whether JSON output should be pretty-printed.
5050
/// </summary>
5151
public bool WriteIndented { get; set; }
52+
53+
/// <summary>
54+
/// Specifies the source generation mode for types that don't explicitly set the mode with <see cref="JsonSerializableAttribute.GenerationMode"/>.
55+
/// </summary>
56+
public JsonSourceGenerationMode GenerationMode { get; set; }
5257
}
5358
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace System.Text.Json.SourceGeneration
1313
/// </summary>
1414
internal sealed class ContextGenerationSpec
1515
{
16-
public JsonSerializerOptionsAttribute SerializerOptions { get; init; }
16+
public JsonSourceGenerationOptionsAttribute GenerationOptions { get; init; }
1717

1818
public Type ContextType { get; init; }
1919

20-
public List<TypeGenerationSpec>? RootSerializableTypes { get; init; }
20+
public List<TypeGenerationSpec> RootSerializableTypes { get; } = new();
2121

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

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,10 @@ private string GenerateFastPathFuncForObject(
637637
bool canBeNull,
638638
List<PropertyGenerationSpec>? properties)
639639
{
640-
JsonSerializerOptionsAttribute options = _currentContext.SerializerOptions;
640+
JsonSourceGenerationOptionsAttribute options = _currentContext.GenerationOptions;
641641

642642
// Add the property names to the context-wide cache; we'll generate the source to initialize them at the end of generation.
643-
string[] runtimePropNames = GetRuntimePropNames(properties, options.NamingPolicy);
643+
string[] runtimePropNames = GetRuntimePropNames(properties, options.PropertyNamingPolicy);
644644
_currentContext.RuntimePropertyNames.UnionWith(runtimePropNames);
645645

646646
StringBuilder sb = new();
@@ -855,7 +855,7 @@ private string DetermineRuntimePropName(string clrPropName, string? jsonPropName
855855
{
856856
runtimePropName = jsonPropName;
857857
}
858-
else if (namingPolicy == JsonKnownNamingPolicy.BuiltInCamelCase)
858+
else if (namingPolicy == JsonKnownNamingPolicy.CamelCase)
859859
{
860860
runtimePropName = JsonNamingPolicy.CamelCase.ConvertName(clrPropName);
861861
}
@@ -890,7 +890,7 @@ private string GenerateForType(TypeGenerationSpec typeMetadata, string metadataI
890890

891891
private string WrapWithCheckForCustomConverterIfRequired(string source, string typeCompilableName, string typeFriendlyName, string numberHandlingNamedArg)
892892
{
893-
if (_currentContext.SerializerOptions.IgnoreRuntimeCustomConverters)
893+
if (_currentContext.GenerationOptions.IgnoreRuntimeCustomConverters)
894894
{
895895
return source;
896896
}
@@ -933,9 +933,9 @@ private string GetRootJsonContextImplementation()
933933

934934
private string GetLogicForDefaultSerializerOptionsInit()
935935
{
936-
JsonSerializerOptionsAttribute options = _currentContext.SerializerOptions;
936+
JsonSourceGenerationOptionsAttribute options = _currentContext.GenerationOptions;
937937

938-
string? namingPolicyInit = options.NamingPolicy == JsonKnownNamingPolicy.BuiltInCamelCase
938+
string? namingPolicyInit = options.PropertyNamingPolicy == JsonKnownNamingPolicy.CamelCase
939939
? $@"
940940
PropertyNamingPolicy = {JsonNamingPolicyTypeRef}.CamelCase"
941941
: null;
@@ -953,7 +953,7 @@ private string GetLogicForDefaultSerializerOptionsInit()
953953

954954
private string GetFetchLogicForRuntimeSpecifiedCustomConverter()
955955
{
956-
if (_currentContext.SerializerOptions.IgnoreRuntimeCustomConverters)
956+
if (_currentContext.GenerationOptions.IgnoreRuntimeCustomConverters)
957957
{
958958
return "";
959959
}

0 commit comments

Comments
 (0)