-
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
System.Text.Json.JsonSerializer cannot deal with base interfaces #47753
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsDescriptionIt seems that
When serializing the object as I am familiar that the behaviour has changed (compared to the Newtonsoft.Json serializer), but this behaviour is very unexpected and doesn't seem right. The properties of the base interface are part of
|
This is documented behaviour: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism (first few lines of text). |
@Tornhoof The documentation describes the serialization of properties definied in derived objects, where my example has issues serializing properties of the base interface. This issue doesn't have anything to do with polymorphism and therefore isn't a duplicate of #29937. When I don't use interfaces, but use concrete classes, then it does serialize the base properties:
In this case the serializer does serialize base properties, but only in case of interfaces it doesn't. |
Seems to be a duplicate of #41749, so I'll close this issue. |
Closing as dup of #41749. |
Description
It seems that
System.Text.Json.JsonSerializer
only serializes the properties of the top-level interface, instead of all the underlying interface. Suppose the following code:When serializing the object as
IDerived
then only the properties that are defined in this interface are exported. I could imagine this behaviour whenIDerived
wasn't derived fromIBase
and whenImplementation
would derive from both base interfaces. But the properties ofIDerived
should include also the base properties to make sure you can serialize the object appropriately.I am familiar that the behaviour has changed (compared to the Newtonsoft.Json serializer), but this behaviour is very unexpected and doesn't seem right. The properties of the base interface are part of
IDerived
and should be serialized. I couldn't think of a reason not to (de)serialize the base properties, but if it's breaking existing code, then please add a property to opt-in for base-property serialization.The text was updated successfully, but these errors were encountered: