-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
STJ Source Generator fails to do polymorphic serialization for built-in types #58134
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsDescriptionThe System.Text.Json source generator fails to serialize list of objects ( Repointernal static class Program
{
private static void Main()
{
JsonSerializer.Serialize(new List<object>
{
"hey"
}, typeof(List<object>), JsonContext.Default);
}
}
[JsonSerializable(typeof(List<object>))]
[JsonSerializable(typeof(string))] //Commenting this line breaks the program
internal sealed partial class JsonContext : JsonSerializerContext
{
}
|
Agreed that the user experience could be improved here, however the error message:
indicates that this is clearly by design. Given that we have a workaround, recommend moving this to 7.0.0. cc @layomia @steveharter |
Related to #30083. |
Yes it is by design - for polymorphic serialization scenarios, we need to know all possible runtime types ahead of time via What sort of improved user experience would we like to see here - a diagnostic warning when compiling and |
It feels unintuitive to need to add the basic primitives and commonly used built-in types with the It would make sense that these are pre-included or that theres a switch to generate them without the need to manually include them all (which there are dozens of) |
I guess it depends on what "commonly used built-in types" means. We might consider adding common primitives and It makes me think that users will encounter this issue one way or another, so there doesn't seem to be a lot of value in baking in support for what might amount to 20% of commonly used runtime types for |
Considering the rooting problem, I think it would relieve the pain point mostly by just including the primitives and
I'm not sure I follow? As long as the underlying type for
The primitives and |
We won't have time to work on this for .NET 7, moving to Future. |
Closing as by-design: implicit handling by the source generator is avoided. It is beneficial to pay attention to polymorphism occurring in serialization routines. Including primitives by default might have an impact on size-related issues - #77897. There's a tracking issue to consider issuing a diagnostic warning when |
Description
The System.Text.Json source generator fails to serialize list of objects (
List<object>
) that contains only built-in types likestring
. This can be worked around by adding the types to theJsonSerializerContext
with theJsonSerializable
attribute but this feels counterintuitive and doesn't look great and bloats the class. This scenario works just fine without source generators.Repo
The text was updated successfully, but these errors were encountered: