How do I generate code to “deserialize” Couchbase documents/dictionaries to data classes?

What I want to do is explained here

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.:


@CouchbaseDocument data class DataClassDerivedFromCouchbaseDocument( @FieldName("item_id") val id: Int, @FieldName("product_code") val code: String, @FieldName("item_description") val description: String, val quantity: Int )

Where the Document is like this:

{"item_id": 234235235, "product_code": "AKDNADJA", "description": "Product Number 2343", "quantity": 242}

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) assert(document.getInt("quantity") == dataClass.quantity) }

How do I generate code for this? Thank you. I got this idea from KotlinX Serialization which is what I use for JSON strings. But it seems that it doesn’t use KAPT or annotation processing

submitted by /u/VincentJoshuaET
[link] [comments]