Skip to content

System.Text.Json fails on deserializing into sub-subclasses of system collections #31009

@FyiurAmron

Description

@FyiurAmron

When trying to deserialize into a class that has a class being a valid deserialization target for a collection higher in the class inheritance graph than its direct parent, the deserialization fails.

Example code:

namespace JsonProblem {

using System.Collections.Generic;
using System.Text.Json;

class ListString : List<string> {
}

class MyList<T> : List<T> {
}

class MyMyList<T> : MyList<T> {
}

class MyListString : MyList<string> {
}

class Program {
    const string JSON = "[\"test\"]";

    static void Main( string[] args ) {
        JsonSerializer.Deserialize( JSON, typeof(string[]) ); // works
        JsonSerializer.Deserialize( JSON, typeof(List<string>) ); // works
        JsonSerializer.Deserialize( JSON, typeof(ListString) ); // also works
        JsonSerializer.Deserialize( JSON, typeof(MyList<string>) ); // works
        JsonSerializer.Deserialize( JSON, typeof(MyMyList<string>) ); // doesn't work
        JsonSerializer.Deserialize( JSON, typeof(MyListString) ); // doesn't work
    }
}

}

All the above classes worked OK in Json.NET - IMVHO, the deserializer should handle them, which it doesn't.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions