-
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]: Add generic versions of System.Text.Json.Serialization attributes #72936
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsBackground and motivationWith the introduction of generic attributes, some attributes
API Proposalnamespace System.Text.Json
public class JsonConverterAttribute<T> : JsonConverterAttribute
{
public JsonConverterAttribute() : base(typeof(T))
{ }
}
public class JsonDerivedTypeAttribute<T> : JsonDerivedTypeAttribute
{
public JsonDerivedTypeAttribute() : base(typeof(T))
{ }
public JsonDerivedTypeAttribute(string typeDiscriminator) : base(typeof(T), typeDiscriminator)
{ }
public JsonDerivedTypeAttribute(int typeDiscriminator) : base(typeof(T), typeDiscriminator)
{ }
} API Usage[JsonDerivedType<Derived>()]
public class Base
{
public int X { get; set; }
}
public class Derived : Base
{
public int Y { get; set; }
} Alternative DesignsNo response RisksNo response
|
I tried this for |
Closing as duplicate of #71712. See #71712 (comment) for a justification. |
Reopening for consideration in the future. |
Thanks for reconsidering this @eiriktsarpalis! |
Possible API shape: namespace System.Text.Json
public class JsonConverterAttribute<TConverter> : JsonConverterAttribute
where TConverter : JsonConverter
{
public JsonConverterAttribute();
}
public class JsonDerivedTypeAttribute<TDerivedType> : JsonDerivedTypeAttribute
where TDerivedType : class
{
public JsonDerivedTypeAttribute();
public JsonDerivedTypeAttribute(string typeDiscriminator);
public JsonDerivedTypeAttribute(int typeDiscriminator);
}
public class JsonSerializableAttribute<T> : JsonSerialiableAttribute
{
public class JsonSerializableAttribute();
} |
It's strange this didn't make it to .NET 8 at least, given how trivial it would be to add. It's the sort of thing you'd intuitively expect to be there at this point, since C# has supported generic attributes for a while now; it's always ugly to have |
From a cost-benefit perspective adding generic variants of existing attributes introduces duplication while not offering a whole lot of benefit. Other than saving the need to type five characters, they mostly don't enhance type safety and they cannot be used in .NET languages that don't support generic attributes. We've kept this open for future consideration, although it doesn't register very high in our priorities for the time being. |
This reasoning makes no sense; the "duplication" (if we can call it that) that it introduces is minimal and the benefit is just better syntax and ehanced type-safety.
They do though, you can pass any type to the constructor of |
It wouldn't do anything when it comes to enforcing the right converter type though, so I might be annotating a property of type FWIW it's fairly straightforward to define generic attributes in your code, if you need them: public class MyPoco
{
[JsonConverter<JsonStringEnumConverter>]
public BindingFlags Value { get; set; }
}
public class JsonConverterAttribute<T>() : JsonConverterAttribute(typeof(T))
where T : JsonConverter
{ } |
Background and motivation
With the introduction of generic attributes, some attributes in
System.Text.Json.Serialization
are good candidates for generic versions, as all public constructors require a type parameter, namely:JsonConverterAttribute
JsonDerivedTypeAttribute
(introduced in Developers can use System.Text.Json to serialize type hierarchies securely #63747)API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: