Basically I want an easy way to do this:
Let’s say I have a data class that corresponds to a Couchbase document e.g.:
@Serializable
data class DataClassDerivedFromCouchbaseDocument(
@SerialName("item_id") val id: Int,
@SerialName("product_code") val code: String,
@SerialName("item_description") val description: String
)
Where the Document is like this:
{"item_id": 234235235, "product_code": "AKDNADJA", "description": "Product Number 2343"}
Is it possible to have something like:
inline fun <reified T> deserialize(document: Document): T {
return document.deserialize(T::class.java)
}
fun main() {
val document = database.getDocument("test")
val dataClass: DataClassDerivedFromCouchbaseDocument = document.deserialize()
// OR
val dataClass = document.deserialize<DataClassDerivedFromCouchbaseDocument>()
// OR
val dataClass = document.deserialize(DataClassDerivedFromCouchbaseDocument::class.java)
assert(document.getInt("item_id") == dataClass.id)
assert(document.getString("product_code") == dataClass.code)
assert(document.getString("item_description") == dataClass.description)
}
This would save a lot of time to developers. You can also give the option to ignore the values that are not specified in the data class like what KotlinX Serialization does.