0

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?

SuperDuperTango
  • 1,398
  • 1
  • 14
  • 32
  • Forgive the question as perhaps I don't understand but `List` is a Realm managed list of Strings - the JSON shown in the question is not a String; it's a key: value pair property of `String: String` followed by what appears to be a key: value pair of `String: Array` – Jay Apr 15 '22 at 16:51
  • Hi @Jay Thanks. In your first line, do you mean "I don't understand but `RealmList` is..."? In any case, the JSON in the example shows an object that has two properties: a string named `session_id`, and an Array of Strings called `user_ids`. I'm asking how to deserialize the second `user_ids` parameter as a `RealmList` (as the kotlin code in the top example shows). It would be simple to decode this structure if I wanted `user_ids` to be a `List`, but I want to deserialize `user_ids` as a `RealmList` (which is a Collection type, not a `List` – SuperDuperTango Apr 16 '22 at 17:05
  • did u have to create custom serializers for realm objects or how did you make this work? – Mahmoud Omara May 19 '22 at 10:57
  • @MahmoudOmaraI haven't had time to finish this yet, but the feature in my app that this is attached to is coming up in a week or so, so I'll likely be looking at this again pretty soon – SuperDuperTango May 20 '22 at 16:03

0 Answers0