-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Developers should have access to System.Text.Json's default internal converters #63791
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsBackground and MotivationCurrently, the default json converters used for objects and collection types are internal to System.Text.Json. The library uses hardcoded conventions to decide what default converters to apply to each type: for instance classes that implement
Currently the best available workaround is for users to either reimplement the default converters, or wrap/cast the values into a type that produces the desired serialization contract. At the same time, we offer no mechanism for users to tweak the serialization behaviour of the default converters other than using attribute annotations (to be ameliorated once #63686 has been implemented). Related issues: ProposalThis story proposes making the default internal converters public, so that users are free to opt in to specific converter strategies for their types. Consider the following example: public class MyClass : IEnumerable<int>, IAsyncEnumerable<int>
{
} By default, members of type [JsonConverter(typeof(JsonObjectConverter))] // serialize as object
// [JsonConverter(typeof(JsonCollectionConverter))] // serialize as collection
// [JsonConverter(typeof(JsonAsyncEnumerableConverter)] // serialize as IAsyncEnumerable
public class MyClass : IEnumerable<int>, IAsyncEnumerable<int>
{
} Moreover, we should consider exposing parameterization on the converters so that users can configure the contract (for example what #38514 is asking for). Progress
|
Related to #67361 , for me the ideal scenario would be that JsonConverter sees that it's an |
Yes, that might be a good convention to honor. Similarly for dictionary types that accept |
We likely won't have time to work on this before .NET 7 is done, moving to 8.0.0 |
Moving to Future as we won't be able to work on this for 8.0 |
What is the suggested workaround in this case? I've a type that wraps an array (private field) that implements IEnumerable of that type. I would like it to be serialised as an object, using a public constructor that accepts an array. The type is intended to be immutable, so it would be counterintuitive to allow for collection overrides e.g. Add. |
The only workaround for now is authoring a custom converter for the type, unfortunately. |
I am currently solving this by deriving such types from I still do not understand what the problem is with implementing a default deserialization converter for types implementing e.g. |
Background and Motivation
Currently, the default json converters used for objects and collection types are internal to System.Text.Json. The library uses hardcoded conventions to decide what default converters to apply to each type: for instance classes that implement
IEnumerable<T>
are automatically treated as collections, and classes that implementIAsyncEnumerable<T>
can only be handled by the async-only IAsyncEnumerable converter. As of today, System.Text.Json offers no mechanism for users to override this convention, and we have received a number of questions from the community about this issue:Currently the best available workaround is for users to either reimplement the default converters, or wrap/cast the values into a type that produces the desired serialization contract. At the same time, we offer no mechanism for users to tweak the serialization behaviour of the default converters other than using attribute annotations (to be ameliorated once #63686 has been implemented). Related issues:
Proposal
This story proposes making the default internal converters public, so that users are free to opt in to specific converter strategies for their types. Consider the following example:
By default, members of type
MyClass
are serialized as IAsyncEnumerables. Users can still force IEnumerable semantics if they cast the instance toIEnumerable<int>
, however there is currently no way to serialize the type as an object. By exposing the default converter types, we can decorate our type definition with attributes:Moreover, we should consider exposing parameterization on the converters so that users can configure the contract (for example what #38514 is asking for).
NB we should update the source generator so that relevant
JsonPropertyAttribute
annotations are recognized and trigger relevant metadata generation, cf. #82001 (comment)Progress
The text was updated successfully, but these errors were encountered: