-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Add JSON source-gen mode that emits serialization logic #53212
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4680404
Add JSON source-gen mode that emits serialization logic
layomia ccb3706
Fix System.Net.Http.Json test issues
layomia 430ac7c
Fix System.Text.Json test issues
layomia 1dd5d73
Make check to determine if fast-path can be used more efficient
layomia 07d2439
Address review feedback
layomia 2dbebc9
Improve derived-JsonSerializerContext detection and support
layomia f855af8
Address review feedback; reenable tests, and simplify object metadata
layomia f937f0c
Fix formatting
layomia 51881ca
Merge remote-tracking branch 'upstream/main' into GenSerializationLogic
layomia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
src/libraries/System.Text.Json/Common/JsonKnownNamingPolicy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Text.Json.Serialization | ||
{ | ||
/// <summary> | ||
/// The <see cref="Json.JsonNamingPolicy"/> to be used at run-time. | ||
/// </summary> | ||
#if BUILDING_SOURCE_GENERATOR | ||
internal | ||
#else | ||
public | ||
#endif | ||
enum JsonKnownNamingPolicy | ||
{ | ||
/// <summary> | ||
/// Specifies that JSON property names should not be converted. | ||
/// </summary> | ||
Unspecified = 0, | ||
|
||
/// <summary> | ||
/// Specifies that the built-in <see cref="Json.JsonNamingPolicy.CamelCase"/> be used to convert JSON property names. | ||
/// </summary> | ||
BuiltInCamelCase = 1 | ||
layomia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/libraries/System.Text.Json/Common/JsonSerializerOptionsAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Text.Json.Serialization | ||
{ | ||
/// <summary> | ||
/// Instructs the System.Text.Json source generator to assume the specified | ||
/// options will be used at run-time via <see cref="JsonSerializerOptions"/>. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] | ||
#if BUILDING_SOURCE_GENERATOR | ||
internal | ||
#else | ||
public | ||
#endif | ||
class JsonSerializerOptionsAttribute : JsonAttribute | ||
{ | ||
/// <summary> | ||
/// Specifies the default ignore condition. | ||
/// </summary> | ||
public JsonIgnoreCondition DefaultIgnoreCondition { get; set; } | ||
|
||
/// <summary> | ||
/// Specifies whether to ignore read-only fields. | ||
/// </summary> | ||
public bool IgnoreReadOnlyFields { get; set; } | ||
|
||
/// <summary> | ||
/// Specifies whether to ignore read-only properties. | ||
/// </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> | ||
public bool IncludeFields { get; set; } | ||
|
||
/// <summary> | ||
/// Specifies a built-in naming polices to convert JSON property names with. | ||
/// </summary> | ||
public JsonKnownNamingPolicy NamingPolicy { get; set; } | ||
|
||
/// <summary> | ||
/// Specifies whether JSON output should be pretty-printed. | ||
/// </summary> | ||
public bool WriteIndented { get; set; } | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/libraries/System.Text.Json/Common/JsonSourceGenerationMode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Text.Json.Serialization | ||
{ | ||
/// <summary> | ||
/// The generation mode for the System.Text.Json source generator. | ||
/// </summary> | ||
[Flags] | ||
#if BUILDING_SOURCE_GENERATOR | ||
internal | ||
#else | ||
public | ||
#endif | ||
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"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// This mode supports all <see cref="JsonSerializer"/> features. | ||
/// </remarks> | ||
MetadataAndSerialization = 0, | ||
|
||
/// <summary> | ||
/// Instructs the JSON source generator to generate type-metadata initialization logic. | ||
/// </summary> | ||
/// <remarks> | ||
/// This mode supports all <see cref="JsonSerializer"/> features. | ||
/// </remarks> | ||
Metadata = 1, | ||
|
||
/// <summary> | ||
/// Instructs the JSON source generator to generate serialization logic. | ||
/// </summary> | ||
/// <remarks> | ||
/// This mode supports only a subset of <see cref="JsonSerializer"/> features. | ||
/// </remarks> | ||
Serialization = 2 | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/libraries/System.Text.Json/gen/ContextGenerationSpec.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.SourceGeneration.Reflection; | ||
|
||
namespace System.Text.Json.SourceGeneration | ||
{ | ||
/// <summary> | ||
/// Represents the set of input types and options needed to provide an | ||
/// implementation for a user-provided JsonSerializerContext-derived type. | ||
/// </summary> | ||
internal sealed class ContextGenerationSpec | ||
{ | ||
public JsonSerializerOptionsAttribute SerializerOptions { get; init; } | ||
|
||
public Type ContextType { get; init; } | ||
|
||
public List<TypeGenerationSpec>? RootSerializableTypes { get; init; } | ||
|
||
public List<string> ContextClassDeclarationList { get; init; } | ||
|
||
/// <summary> | ||
/// Types that we have initiated serialization metadata generation for. A type may be discoverable in the object graph, | ||
/// but not reachable for serialization (e.g. it is [JsonIgnore]'d); thus we maintain a separate cache. | ||
/// </summary> | ||
public HashSet<TypeGenerationSpec> TypesWithMetadataGenerated { get; } = new(); | ||
|
||
/// <summary> | ||
/// Cache of runtime property names (statically determined) found accross the object graph of the JsonSerializerContext. | ||
/// </summary> | ||
public HashSet<string> RuntimePropertyNames { get; } = new(); | ||
|
||
public string ContextTypeRef => $"global::{ContextType.GetUniqueCompilableTypeName()}"; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.