You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Text.Json.JsonSerializer cannot deal with base interfaces #47753
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:
public interface IBase
{
int Count { get; set; }
}
public interface IDerived : IBase
{
string Name { get; set; }
}
public class Implementation : IDerived
{
public int Count { get; set; }
public string Name { get; set; }
}
internal static class Program
{
private static void Main(string[] args)
{
var obj = new Implementation {Count = 1, Name = "Test"};
// {"Count":1,"Name":"Test"}
System.Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(obj));
// {"Name":"Test"} <-- Count is not serialized, because it's defined in a base interface
System.Console.WriteLine(System.Text.Json.JsonSerializer.Serialize((IDerived)obj));
}
}
When serializing the object as IDerived then only the properties that are defined in this interface are exported. I could imagine this behaviour when IDerived wasn't derived from IBase and when Implementation would derive from both base interfaces. But the properties of IDerived 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.
@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:
public class Base
{
public int Count { get; set; }
}
public class Derived : Base
{
public string Name { get; set; }
}
internal static class Program
{
private static void Main(string[] args)
{
var obj = new Derived {Count = 1, Name = "Test"};
// {"Count":1,"Name":"Test"}
System.Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(obj));
}
}
In this case the serializer does serialize base properties, but only in case of interfaces it doesn't.
#29937 is more or less the original issue about polymorphic support in general. #45189 tracks the work for .net 6.0 #41749 tracks the discovery of properties for interfaces.
Activity
Tornhoof commentedon Feb 2, 2021
This is documented behaviour: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism (first few lines of text).
In general this is probably a duplicate of #29937
ramondeklein commentedon Feb 2, 2021
@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.
Tornhoof commentedon Feb 2, 2021
#29937 is more or less the original issue about polymorphic support in general.
#45189 tracks the work for .net 6.0
#41749 tracks the discovery of properties for interfaces.
ramondeklein commentedon Feb 2, 2021
Seems to be a duplicate of #41749, so I'll close this issue.
layomia commentedon Feb 2, 2021
Closing as dup of #41749.