How to properly serialize classes with RealmLists of embedded objects

Hi,

I’m struggling to properly serialize my model that includes a realmList of embedded objects.

Here is my data model:

@Serializable
class InventoryItem: EmbeddedRealmObject {
    var upc: String = ""
    var quantity: Long = 0
    var quantityType: String = ""
    var retail: Long = 0
    var cost: Long = 0
    var totalRetail: Long = 0
    var totalCost: Long = 0
}
@Serializable
class InventoryBatch: RealmObject {
    @PrimaryKey
    var _id: ObjectId = ObjectId()
    var dateTime: RealmInstant = RealmInstant.now()
    @Serializable(RealmListKSerializer::class)
    var inventoryItems: RealmList<InventoryItem> = realmListOf()
    var inventoryType: String = ""
    var notes: String? = null
    var status: String? = null
    var store: String = ""
    var tenantId: String = ""
    var transferStore: String? = null
    var user: String = ""
}

My understanding from the Docs is that I should just need to add the @Serializable tag annotation to the class and register the serializer for the property. However when I query inventory batches I get the following error:

kotlinx.serialization.SerializationException: Class 'RealmResultsImpl' is not registered for polymorphic serialization in the scope of 'RealmResults'.
                                                                                                    To be registered automatically, class 'RealmResultsImpl' has to be '@Serializable', and the base class 'RealmResults' has to be sealed and '@Serializable'.

Here is my query:

var inventoryBatch: RealmResults<InventoryBatch> = inventoryRealm.query<InventoryBatch>("status == $0", "open").find()

What am I doing wrong here?

Hi, I’m continuing to struggle with this, does anyone have a solution?

You’re returning a RealmResults object, which isn’t a serializable type. You’ll need to convert the output to a serializable type (e.g. to a list). And my guess is that you’ll also need to serialize the dateTime property.

FWIW, I tested out annotating the dateTime property with @Serializable(RealmInstantKSerializer::class), then running your query on some created objects.

var inventoryBatch: RealmResults<InventoryBatch> = 
       inventoryRealm.query<InventoryBatch>("status == $0", "open").find()

println(Json.encodeToString(inventoryBatch.toList()))

successfully output:

[
  {
    "_id": {
      "$oid": "66034b541cedbb10dcae78df"
    },
    "dateTime": {
      "$date": {
        "$numberLong": "1711491924707"
      }
    },
    "inventoryItems": [
      {
        "upc": "1234567890",
        "quantity": 10,
        "quantityType": "each",
        "retail": 100,
        "cost": 50,
        "totalRetail": 1000,
        "totalCost": 500
      }
    ],
    "inventoryType": "stock",
    "status": "open",
    "store": "Main Store",
    "tenantId": "123456",
    "user": "John Doe"
  }
]

Thanks Cory,

Thats mostly right, it was actually the datTime property causing an issue the whole time. I switched from the realmInstant type and everything is working fine now.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.