I'm trying to deserialize this json:
{
"teaser": [{
"id": "...",
"type": "category",
"url": "https:...",
},{
"id": "...",
"type": "brand",
"url": "https:...",
"videoCount": 1,
},{
"id": "...",
"type": "video",
"url": "https:...",
"headline": "...",
}]
}
It has a list of teasers whereby each teaser is different depending on its type. These would be my objects:
public class StartPage
{
public IList<Teaser> Teaser { get; set; }
}
public abstract class Teaser
{
public string Id { get; set; }
public string Url { get; set; }
}
public class Video : Teaser
{
public string Headline { get; set; }
}
public class Brand : Teaser
{
public int VideoCount { get; set; }
}
I am new to Json.NET and Xamarin and couldn't find a solution for this case yet. Before, when I was using Android Studio and Gson, I could register sybtypes the following way:
RuntimeTypeAdapterFactory<Teaser> teaserRuntimeTypeAdapterFactory = RuntimeTypeAdapterFactory.of(
Teaser.class, "type")
.registerSubtype(Video.class, Teaser.TYPE_VIDEO)
.registerSubtype(Brand.class, Teaser.TYPE_BRAND)
.registerSubtype(Category.class, Teaser.TYPE_CATEGORY);
return new GsonBuilder()
.registerTypeAdapterFactory(teaserRuntimeTypeAdapterFactory);
Is there a similar way to achieve what I want with Json.NET I have overlooked yet?