-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[API Proposal]: Allow C# models to be decorated with an attribute to indicate required code generation, not JsonSerializerContext #73297
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsBackground and motivationI've been looking at enabling trimming for some of our projects now we've migrated to .NET 6, and have found the current design for configuring the supported types on a Some of our projects have a reasonable, though not excessive, number of models that can be both serialized and deserialized from a third party API. This could be a single instance of the entity, or a list, which means that for every model we want to work with, we need to remember to decorate our context class as: [JsonSerializable(typeof(Model1))]
[JsonSerializable(typeof(IList<Model1>))]
[JsonSerializable(typeof(Model2))]
[JsonSerializable(typeof(IList<Model2>))]
[JsonSerializable(typeof(Model3))]
[JsonSerializable(typeof(IList<Model3>))]
internal partial class ThirdPartyModelSerializationContext : JsonSerializerContext
{
} The number of interaction points we have with the API will undoubtedly grow, and every time a new model is required, it's necessary to remember that there's another class somewhere that needs to have its list of attributes updated. It the model class itself could be decorated with an attribute indicating the context that it needs to be part of, it would be much easier to remember, e.g.: internal partial class ThirdPartyModelSerializationContext : JsonSerializerContext
{
}
[JsonSerializerContext(typeof(ThirdPartyModelSerializationContext)]
public class Model1
{
/// ...
} API Proposalpublic sealed class JsonSerializerContextAttribute : JsonAttribute
{
public JsonSerializerContextAttribute(Type jsonSerializerContextType, params Type[] additionalRootTypes);
} API Usage// Use the existing approach for the edge case where a model must *only* be serializable as part of a list and
// serialization should throw an error if someone tries to serialize an entity outside of a list type
[JsonSerializable(typeof(IList<Model3>))]
internal partial class ThirdPartyModelSerializationContext : JsonSerializerContext
{
}
// A model that only is only ever (de)serialized as itself
[JsonSerializerContext(typeof(ThirdPartyModelSerializationContext)]
public class Model1
{
/// ...
}
// A model that could be deserialized as itself or a list
[JsonSerializerContext(typeof(ThirdPartyModelSerializationContext), typeof(IList<Model2>)]
public class Model2
{
/// ...
} Alternative DesignsNo response RisksNo response
|
It might be something we might consider, that being said there are a couple of drawbacks with that approach:
|
This can still be quite efficient on Roslyn 4.0 already (use |
Background and motivation
I've been looking at enabling trimming for some of our projects now we've migrated to .NET 6, and have found the current design for configuring the supported types on a
JsonSerializerContext
to be quite cumbersome.Some of our projects have a reasonable, though not excessive, number of models that can be both serialized and deserialized from a third party API. This could be a single instance of the entity, or a list, which means that for every model we want to work with, we need to remember to decorate our context class as:
The number of interaction points we have with the API will undoubtedly grow, and every time a new model is required, it's necessary to remember that there's another class somewhere that needs to have its list of attributes updated.
It the model class itself could be decorated with an attribute indicating the context that it needs to be part of, it would be much easier to remember, e.g.:
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: