passing an @Serializable object as a parameter instead of KSerializer

is there any way to get around having to pass in the KSerializer into a function, in java we could just use something like T:Serializable, I can’t figure out how to do that with kotlin

this is how I normally do this,

fun <T> writeJsonToTmp(fileName: String, serial: KSerializer<T>, serializableObj: T) {
try {
val myJson = Json { prettyPrint = true }
val json = myJson.encodeToString(serial, serializableObj)
File("/tmp/$fileName").writeText(json)
}catch (e: Exception) {
logger.error(e.message)
}

}

there has to be a way to just note that T extends serializable but I can’t figure it out

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