Idea: Deserialization of Couchbase Documents to POJO/Kotlin Data Class

Sample document:

{
    "class_id": 239,
    "message": "Test message",
    "is_available": true,
    "blob_id": {
        "digest": "sha1-xY90CIbNQ2Qo/5enF7RypDxRGNY=",
        "@type": "blob",
        "length": 13228,
        "content_type": "image/jpeg"
    }
}

Kotlin Data Class:

// Optional Serial Name
@Document
data class Sample(@SerialName("class_id") val classId: Int, val message: String): Document

Usage:

override fun onCreate(savedInstanceState: Bundle?) {
    val sample = database.getDocument<Sample>("document_id")
    // Accessible from data class
    val id = sample.classId
    val message = sample.message
    // Accessible from Couchbase document
    val isAvailable = sample.getBoolean("is_available")
    val blob = sample.getBlob("blob_id")
}

Right now I use KotlinX serialization, but a downside is I am not able to access the blobs from the data class.

I’m sorry… Is there a question here? What do you mean “I am not able to access the blobs from the data class”?

Surely you don’t expect that the entire contents of the blob will be in memory??? That would kind of defeat the purpose of a blob, right?

If you can show some sample code demonstrating what you are trying to do, perhaps I can help.

… and, btw, reading the database (file I/O) from the UI thread seems like a pretty terrible idea, FWIW…

This is just a sample code to simplify it. I want a data class that is able to access all the methods of a Couchbase document, basically I just want to add getters for the Couchbase document so I can access some fields quicker.

So, in your data class, wrap the calls that get the blob contents.