-
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]: Attribute on Types to opt-out of default Serialization/Binding behavior when they implement Collection Interfaces #61354
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsBackground and motivationI have come across at least 2 cases in DotNet 6 where Serialization and Deserialization in While the current behavior is absolutely fine for most cases, in some situations where user types implement the aforementioned interfaces, this behavior is wrong. For example, if a type implements My other case was the incorrect treatment of my custom type by API ProposalI cannot recommend anything particular at this point. But ideally an attribute with/without some options would suffice: [AttributeUsage(AttributeTargets.Type | AttributeTargets.Module | AttributeTargets.Assembly, AllowMultiple = false, Inherited = true)]
public class OptOutOfDefaultCollectionSerializationAndBindingBehavior : Attribute {
} API Usage[OptOutOfDefaultCollectionSerializationAndBindingBehavior]
public class MyFancyType : IList<string> {
} Alternative DesignsNo response RisksNo response
|
Duplicate of #1808. We likely won't be using an opt-out attribute, instead we'd let users opt in to different converter strategies using the |
It's not about just |
From the System.Text.Json perspective, we are intentionally not honoring any of the common |
Right, but there are many common binding scenarios (there was even some talk of unifying binder story between ASP.NET, Configuration, CommandLine, etc., and in there such an attribute, or generalized version of it would still be useful. SO it is not just about Serializers either. |
Background and motivation
I have come across at least 2 cases in DotNet 6 where Serialization and Deserialization in
System.Text.Json
as well asMicrosoft.Extensions.Configuration.Binder
were producing incorrect results by treating in a special way types that implement various collection interfaces (IList<T>
andIEnumerable<T>
) in my case.While the current behavior is absolutely fine for most cases, in some situations where user types implement the aforementioned interfaces, this behavior is wrong. For example, if a type implements
IList<T>
but has some additional public properties, it will still be serialized bySystem.Text.Json
effectively as aList<T>
without the additional properties.My other case was the incorrect treatment of my custom type by
Microsoft.Extensions.Configuration.Binder
because it implementedIEnumerable<T>
, which was fixed fortunately, but still highlights the importance of this issue for other current and future binding / (de)serialization scenarios.API Proposal
I cannot recommend anything particular at this point. But ideally an attribute with/without some options would suffice:
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: