'kotlinx serialization custom serlializer for custom List (RealmList) class

I'm using the Realm database in Kotlin on Android along with some data that we're receiving over the wire using JSON, using kotlinx.serialization as the JSON library.

I'd like to write a custom serializer for RealmList<String>.

An example class would look like:

@Serializable
open class Session(): RealmObject() {
    @PrimaryKey
    @SerialName("session_id")
    var sessionId: String = ""
    
    @SerialName("user_ids")
    var userIds: RealmList<String>
//...
}

The JSON for the RealmList<String> is the same as a List<String>:

{
    "session_id": "session_23848923498",
    "user_ids": [
        "user_o823uo234u",
        "user_23i2332984",
        "user_23480192849"
    ]
}

The docs for writing a custom serializer at https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md are pretty good, and i've been able to write various custom serializers for some of our classes. But in this case it's more like I'm looking to write a Serializer for a List collection, not a specific custom class.

Any ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source