Hey, I have a problem where this class: ```@Seria...
# kvision
u
Hey, I have a problem where this class:
Copy code
@Serializable
data class EntryRecord(
    val id: Int?,
    @Contextual val creationDate: Date?,
    val contractType: String,
    val contractNumber: String,
    val pricePrepaid: Int,
    var pricePrepaidConfirmed: Boolean,
    val priceTotal: Int,
    var priceTotalConfirmed: Boolean,
    val additionalInfo: String?
)
Is serialized to this string:
Copy code
"{"_id":null,"_creationDate":null,"_contractType":"Test","_contractNumber":"Test","_pricePrepaid":20099,"_pricePrepaidConfirmed":false,"_priceTotal":400095,"_priceTotalConfirmed":false,"_additionalInfo":""}"
Why is kotlin adding
_
in front of every field name? How do I get rid of this?
r
How do you serialize it? I suppose you are using native JS serialization and you should use kotlinx.serialization.
u
Em, I'm using this:
Copy code
fun addRecord(record: EntryRecord): Promise<dynamic> {
        return RestClient().requestDynamic("/api/v1/records") {
            method = <http://HttpMethod.POST|HttpMethod.POST>
            data = record
        }
    }
Is there a better way?
r
Copy code
RestClient().requestDynamic("/api/v1/records", data) {
    method = <http://HttpMethod.POST|HttpMethod.POST>
}
When you put your data as parameter, it will be automatically serialized by kotlinx.serialization.
When setting with
RestRequestConfig
(like in your example) it will be passed directly to fetch API if you won't provide a serializer as well.
This is how requestDynamic with a typed parameter is defined:
Copy code
inline fun <reified V : Any> RestClient.requestDynamic(
    url: String,
    data: V,
    crossinline block: RestRequestConfig<dynamic, V>.() -> Unit = {}
): Promise<RestResponse<dynamic>> {
    return receive<dynamic, V>(url) {
        block.invoke(this)
        this.data = data
        this.serializer = serializer()
    }
}
u
I see, thanks a lot