JSON Deserialization with an array of polymorphic objects
You have not added any settings upon deserialization. You need to apply settings with TypeNameHandling
set to Object
or All
.
Like this:
JsonConvert.DeserializeObject(
returnedStringFromClient,
typeof(Scoresheet),
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});
Documentation: TypeNameHandling setting
Use this JsonKnownTypes, similar way to do that:
[JsonConverter(typeof(JsonKnownTypesConverter<BaseClass>))]
[JsonKnownType(typeof(Base), "base")]
[JsonKnownType(typeof(Derived), "derived")]
public class Base
{
public string Name;
}
public class Derived : Base
{
public string Something;
}
Now when you serialize object in json will be add "$type"
with "base"
and "derived"
value and it will be use for deserialize
Serialized list example:
[
{"Name":"some name", "$type":"base"},
{"Name":"some name", "Something":"something", "$type":"derived"}
]