You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I am using .NET (AOT) with the System.Text.Json source generator.
Right now, to enable static reflection (source-generated serialization), I have to manually list every DTO/model type inside a custom JsonSerializerContext, for example:
This becomes hard to maintain as the number of models grows, because I have to write all DTO/model at the same place while all DTO/model distributed in various files.
Is there a way to annotate each model class directly so that the generator automatically adds it to my MyJsonSerializerContext? For example something like:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I am using .NET (AOT) with the
System.Text.Jsonsource generator.Right now, to enable static reflection (source-generated serialization), I have to manually list every DTO/model type inside a custom
JsonSerializerContext, for example:This becomes hard to maintain as the number of models grows, because I have to write all DTO/model at the same place while all DTO/model distributed in various files.
Is there a way to annotate each model class directly so that the generator automatically adds it to my
MyJsonSerializerContext? For example something like:And then the source generator would automatically include it in the specified context.
The ways I thought of
Method 1
Declare partial class
MyJsonSerializerContextat in various files:However, it does not work, I got some compiler errors:
Method 2
One class corresponds to one JsonSerializerContext.
However, I still have to regester all JsonSerializerContexts, the situation has not changed substantially.
What I am looking for
JsonIncludeInContextmentioned aboveOR
JsonSerializerContextin the distributed file with the sameJsonSerializerContextOR
Environment
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions