Skip to content

Commit

Permalink
Clean up JSON source gen APIs (dotnet#54527)
Browse files Browse the repository at this point in the history
* Clean up JSON source gen APIs

* Address review feedback

* Simplify GenerateX computation
  • Loading branch information
layomia authored Jun 25, 2021
1 parent 333c4e7 commit 118c530
Show file tree
Hide file tree
Showing 19 changed files with 338 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ enum JsonKnownNamingPolicy
/// <summary>
/// Specifies that the built-in <see cref="Json.JsonNamingPolicy.CamelCase"/> be used to convert JSON property names.
/// </summary>
BuiltInCamelCase = 1
CamelCase = 1
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !BUILDING_SOURCE_GENERATOR
using System.Text.Json.Serialization.Metadata;
#endif

namespace System.Text.Json.Serialization
{
Expand All @@ -10,7 +12,13 @@ namespace System.Text.Json.Serialization
/// when serializing and deserializing instances of the specified type and types in its object graph.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class JsonSerializableAttribute : JsonAttribute

#if BUILDING_SOURCE_GENERATOR
internal
#else
public
#endif
sealed class JsonSerializableAttribute : JsonAttribute
{
/// <summary>
/// Initializes a new instance of <see cref="JsonSerializableAttribute"/> with the specified type.
Expand All @@ -28,7 +36,8 @@ public JsonSerializableAttribute(Type type) { }
public string? TypeInfoPropertyName { get; set; }

/// <summary>
/// Determines what the source generator should generate for the type.
/// Determines what the source generator should generate for the type. If the value is <see cref="JsonSourceGenerationMode.Default"/>,
/// then the setting specified on <see cref="JsonSourceGenerationOptionsAttribute.GenerationMode"/> will be used.
/// </summary>
public JsonSourceGenerationMode GenerationMode { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ namespace System.Text.Json.Serialization
enum JsonSourceGenerationMode
{
/// <summary>
/// Instructs the JSON source generator to generate serialization logic and type metadata to fallback to
/// when the run-time options are not compatible with the indicated <see cref="JsonSerializerOptionsAttribute"/>.
/// When specified on <see cref="JsonSourceGenerationOptionsAttribute.GenerationMode"/>, indicates that both type-metadata initialization logic
/// and optimized serialization logic should be generated for all types. When specified on <see cref="JsonSerializableAttribute.GenerationMode"/>,
/// indicates that the setting on <see cref="JsonSourceGenerationOptionsAttribute.GenerationMode"/> should be used.
/// </summary>
/// <remarks>
/// This mode supports all <see cref="JsonSerializer"/> features.
/// </remarks>
MetadataAndSerialization = 0,
Default = 0,

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

/// <summary>
/// Instructs the JSON source generator to generate serialization logic.
/// Instructs the JSON source generator to generate optimized serialization logic.
/// </summary>
/// <remarks>
/// This mode supports only a subset of <see cref="JsonSerializer"/> features.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace System.Text.Json.Serialization
#else
public
#endif
class JsonSerializerOptionsAttribute : JsonAttribute
class JsonSourceGenerationOptionsAttribute : JsonAttribute
{
/// <summary>
/// Specifies the default ignore condition.
Expand Down Expand Up @@ -43,11 +43,16 @@ class JsonSerializerOptionsAttribute : JsonAttribute
/// <summary>
/// Specifies a built-in naming polices to convert JSON property names with.
/// </summary>
public JsonKnownNamingPolicy NamingPolicy { get; set; }
public JsonKnownNamingPolicy PropertyNamingPolicy { get; set; }

/// <summary>
/// Specifies whether JSON output should be pretty-printed.
/// </summary>
public bool WriteIndented { get; set; }

/// <summary>
/// Specifies the source generation mode for types that don't explicitly set the mode with <see cref="JsonSerializableAttribute.GenerationMode"/>.
/// </summary>
public JsonSourceGenerationMode GenerationMode { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/libraries/System.Text.Json/gen/ContextGenerationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace System.Text.Json.SourceGeneration
/// </summary>
internal sealed class ContextGenerationSpec
{
public JsonSerializerOptionsAttribute SerializerOptions { get; init; }
public JsonSourceGenerationOptionsAttribute GenerationOptions { get; init; }

public Type ContextType { get; init; }

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,10 @@ private string GenerateFastPathFuncForObject(
bool canBeNull,
List<PropertyGenerationSpec>? properties)
{
JsonSerializerOptionsAttribute options = _currentContext.SerializerOptions;
JsonSourceGenerationOptionsAttribute options = _currentContext.GenerationOptions;

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

StringBuilder sb = new();
Expand Down Expand Up @@ -855,7 +855,7 @@ private string DetermineRuntimePropName(string clrPropName, string? jsonPropName
{
runtimePropName = jsonPropName;
}
else if (namingPolicy == JsonKnownNamingPolicy.BuiltInCamelCase)
else if (namingPolicy == JsonKnownNamingPolicy.CamelCase)
{
runtimePropName = JsonNamingPolicy.CamelCase.ConvertName(clrPropName);
}
Expand Down Expand Up @@ -890,7 +890,7 @@ private string GenerateForType(TypeGenerationSpec typeMetadata, string metadataI

private string WrapWithCheckForCustomConverterIfRequired(string source, string typeCompilableName, string typeFriendlyName, string numberHandlingNamedArg)
{
if (_currentContext.SerializerOptions.IgnoreRuntimeCustomConverters)
if (_currentContext.GenerationOptions.IgnoreRuntimeCustomConverters)
{
return source;
}
Expand Down Expand Up @@ -933,9 +933,9 @@ private string GetRootJsonContextImplementation()

private string GetLogicForDefaultSerializerOptionsInit()
{
JsonSerializerOptionsAttribute options = _currentContext.SerializerOptions;
JsonSourceGenerationOptionsAttribute options = _currentContext.GenerationOptions;

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

private string GetFetchLogicForRuntimeSpecifiedCustomConverter()
{
if (_currentContext.SerializerOptions.IgnoreRuntimeCustomConverters)
if (_currentContext.GenerationOptions.IgnoreRuntimeCustomConverters)
{
return "";
}
Expand Down
Loading

0 comments on commit 118c530

Please sign in to comment.