I query an API that has a strange serialization pattern. Some properties may either be null (not existing) or an array of objects (default) or even just an object. It depends on how many items are in the list.
{
persons: null
}
or
{
persons: { name: "lastname1, firstname1" }
}
or
{
persons: [{ name: "lastname1, firstname1" }, { name: "lastname2, firstname2" }]
}
Now I want to deserialize this using json.net. But I don't know how to define my model. When I define persons property as List<Person> case 1 and 3 deserialize fine, but case 2 fails; when I define it as a Person, case 1 and 2 deserialize fine. The best would be, when I could define it as a list of persons and write something that instructs json.net to deserialize it correctly. Now json.net has some concepts to intercept serialization and deserialization. What's the best approach to solve this?