
) : data class val linkText: val url: val linkIcon: String? = null,Īt first I made my models match the JSON values as I didn’t have to specify since KotlinX Serialization will just match the field name.

Final Models after adding and public sealed interface CampaignContent data class val imageUrl: val text: val subtext: String, In the example below I added the data class. This is helpful for me as the backend did not use fully qualified names, and I had set them explicitly. If you don’t want to use the fully qualified class name as the class type, then you can put a annotation to the class and it will use that name instead of the fully qualified class name. public sealed interface CampaignContent serializer. This endpoint returns a strongly typed campaign, and I wanted to represent this in JSON. In this post I’ll walk you through an example of how I configured KotlinX Serialization to work for my use case. While all of those libraries are great, I had a requirement of creating a multi-platform library, and went with KotlinX Serialization.

There are a bunch of options for serializing JSON in Java like Moshi, Gson and Jackson. I wanted to serialize these sealed interface Kotlin models to/from JSON over HTTP.

I heavily use sealed interfaces to model result objects in Kotlin as they allow me to create a type of classes that can be handled using exhaustive when statements, similar to an enum, but also each type can contain its own properties.
