Skip to content

Deserialize interfaces using System.Text.Json.JsonSerializer #87538

@douglasg14b

Description

@douglasg14b

#30956 seems to have been the original request for this, however it's closed as completed, but there is still no support for JSON Serialization of interfaces as of .Net 7...?

Following through this chain of issues #30956 -> #30083 -> #63747 -> #71346


It's suggested in the implementation noted in #63747 that the following would allow for interface de-serialization:

[JsonPolymorphic(UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor)]
[JsonDerivedType(typeof(Node))]
interface INode 
{
    string Value { get; set; }
}

class Node : INode
{
    public string Value { get; set; }
}

// Should be able to de-serialize to this
class MyClass 
{
    public INode Value { get; set; }
}

Hhowever doing something similar to this results in a pretty clear exception:

System.NotSupportedException: Deserialization of interface types is not supported

All threads I could find related to this feature are closed as completed. So here I am!

Is this a feature already tracked?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions