- Notifications
You must be signed in to change notification settings - Fork5.2k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
ghost commentedMay 25, 2021
Tagging subscribers to this area:@eiriktsarpalis,@layomia Issue DetailsContributes to#51945. Users kick off source generation by providing a partial, derived context class; and indicate serializable types and run-time options (optional): namespaceSystem.Text.Json.SourceGeneration.Tests{[JsonSerializerOptions(DefaultIgnoreCondition=JsonIgnoreCondition.WhenWritingDefault,IgnoreReadOnlyProperties=true,IgnoreRuntimeCustomConverters=true,NamingPolicy=JsonKnownNamingPolicy.BuiltInCamelCase)][JsonSerializable(typeof(JsonMessage),GenerationMode=JsonSourceGenerationMode.Serialization)]internalpartialclassJsonContext:JsonSerializerContext{}publicclassJsonMessage{publicstringMessage{get;set;}publicintLength=>Message?.Length??0;// Read-only property}} Generated code (click to view)// <auto-generated/>namespaceSystem.Text.Json.SourceGeneration.Tests{[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Text.Json.SourceGeneration","6.0.0.0")]internalpartialclassJsonContext:global::System.Text.Json.Serialization.JsonSerializerContext{privatestaticglobal::System.Text.Json.JsonSerializerOptionss_defaultOptions{get;}=newglobal::System.Text.Json.JsonSerializerOptions(){DefaultIgnoreCondition=global::System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault,IgnoreReadOnlyFields=false,IgnoreReadOnlyProperties=true,IncludeFields=false,WriteIndented=false,PropertyNamingPolicy=global::System.Text.Json.JsonNamingPolicy.CamelCase};privatestaticglobal::System.Text.Json.SourceGeneration.Tests.JsonContexts_defaultContext;publicstaticglobal::System.Text.Json.SourceGeneration.Tests.JsonContextDefault=>s_defaultContext??=newglobal::System.Text.Json.SourceGeneration.Tests.JsonContext(newglobal::System.Text.Json.JsonSerializerOptions(s_defaultOptions));privatestaticglobal::System.Text.Json.JsonEncodedTextmessagePropName=global::System.Text.Json.JsonEncodedText.Encode("message");privatestaticglobal::System.Text.Json.JsonEncodedTextlengthPropName=global::System.Text.Json.JsonEncodedText.Encode("length");publicJsonContext():base(null,s_defaultOptions){}publicJsonContext(global::System.Text.Json.JsonSerializerOptionsoptions):base(options,s_defaultOptions){}publicoverrideglobal::System.Text.Json.Serialization.Metadata.JsonTypeInfoGetTypeInfo(global::System.Typetype){if(type==typeof(global::System.Text.Json.SourceGeneration.Tests.JsonMessage)){returnthis.JsonMessage;}returnnull!;}privateglobal::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Text.Json.SourceGeneration.Tests.JsonMessage>_JsonMessage;publicglobal::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Text.Json.SourceGeneration.Tests.JsonMessage>JsonMessage{get{if(_JsonMessage==null){global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Text.Json.SourceGeneration.Tests.JsonMessage>objectInfo=global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateObjectInfo<global::System.Text.Json.SourceGeneration.Tests.JsonMessage>();_JsonMessage=objectInfo;global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.InitializeObjectInfo(objectInfo,Options,createObjectFunc:static()=>newglobal::System.Text.Json.SourceGeneration.Tests.JsonMessage(),propInitFunc:null,default,serializeFunc:JsonMessageSerialize);}return_JsonMessage;}}privatestaticvoidJsonMessageSerialize(global::System.Text.Json.Utf8JsonWriterwriter,global::System.Text.Json.SourceGeneration.Tests.JsonMessagevalue){if(value==null){writer.WriteNullValue();return;}writer.WriteStartObject();if(value.Message!=null){writer.WriteString(messagePropName,value.Message);}writer.WriteEndObject();}}} To use generated serialization code: Interacting with generated func directly: usingMemoryStreamms=new();Utf8JsonWriterwriter=new(ms);JsonContext.Default.JsonMessage.Serialize!(writer,newJsonMessage{Message="Hello, world!"});writer.Flush(); Interacting via JsonSerializer.Serialize(newJsonMessage{Message="Hello, world!"},JsonContext.Default.JsonMessage); FYI@pranavkm@SteveSandersonMS@terrajobst@ericstj@jkotas@stephentoub@davidfowl@SamMonoRT@CoffeeFlux
|
ghost commentedMay 25, 2021
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
...ries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoInternalOfT.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoInternalOfT.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextTests.csShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
| using ReferencedAssembly; | ||
| using @JsonSerializable = System.Runtime.Serialization.ContractNamespaceAttribute; | ||
| using @JsonSerializable = System.Runtime.Serialization.CollectionDataContractAttribute; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Why use S.R.S.CollectionDataContractAttribute here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I changed to this because, likeJsonSerializableAttribute, it is applied toclasses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This test is modeling type aliasing. In this case, we have two attributes switching names. The test makes sure that our check for[JsonSerializable(Type)] guards against such tricks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I see. A comment here about that would be useful. Thanks
.../System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.csShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoInternalOfT.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
...ries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.csShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
.../System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonSerializableAttribute.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/libraries/System.Net.Http.Json/tests/FunctionalTests/JsonContext/JsonContext.csShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...t.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ystem.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonMetadataServicesConverter.csShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ystem.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonMetadataServicesConverter.csShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ystem.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonMetadataServicesConverter.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
ericstj left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Lgtm. Let’s get this in so we can get a preview s out. Any additional feedback can be addressed in follow up prs.
ericstj commentedMay 27, 2021
All these PGO failures were caused by#53301,@AndyAyersMS and@BruceForstall have helped get this fixed, but we can't get rid of the badges without pushing new changes / resetting CI. Since we know this PR didn't introduce the PGO failures we can safely ignore those. |
layomia commentedMay 27, 2021
layomia commentedMay 27, 2021
/backport to release/6.0-preview5 |
Started backporting to release/6.0-preview5:https://github.com/dotnet/runtime/actions/runs/881152471 |
Uh oh!
There was an error while loading.Please reload this page.
layomia commentedNov 2, 2021
These breaking changes have already hit customers & the APIs have changed further since then -dotnet/docs#26200. |
Uh oh!
There was an error while loading.Please reload this page.
Contributes to#51945.
Contributes to#52279.
Users kick off source generation by providing a partial, derived context class; and indicate serializable types and run-time options (optional):
Generated code (click to view)
To use generated serialization code:
Interacting with generated func directly:
Interacting via
JsonSerializer:or
FYI@pranavkm@SteveSandersonMS@terrajobst@ericstj@jkotas@stephentoub@davidfowl@SamMonoRT@CoffeeFlux