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.
cc @layomia it appears that deserialization only works for one additional level of inheritance with generics.
Re-opening until fixed for 3.1
This issue has been fixed in release/3.1 (https://github.com/dotnet/corefx/pull/41523).
@FyiurAmron, can you please use the 3.1-preview1 (or master) build and verify the issue has been fixed?
https://www.nuget.org/packages/System.Text.Json/4.7.0-preview1.19504.10
You can reference the latest master System.Text.Json NuGet package from https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json
@ahsonkhan @layomia verified fixed & working OK in https://www.nuget.org/packages/System.Text.Json/4.7.0-preview1.19504.10
Closing as fix is in 3.1, some tests are remaining to be merged once branch opens but we can let the PR track those.