Skip to content
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

Clean up JSON source gen APIs #54527

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ 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"/>.
/// Unspecified source generation mode.
/// </summary>
/// <remarks>
/// This mode supports all <see cref="JsonSerializer"/> features.
/// </remarks>
MetadataAndSerialization = 0,
Unspecified = 0,

/// <summary>
/// Instructs the JSON source generator to generate type-metadata initialization logic.
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.
layomia marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public JsonSourceGenerationMode GenerationMode { get; set; } = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization;
layomia marked this conversation as resolved.
Show resolved Hide resolved
}
}
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