If you want to ensure at compile time that the value is serializable with Kotlin Serialization, then you have to explicitly take the serialization strategy.
Notice the two overloads of Json.encodeToString
. One will only tell you at runtime if the class is not serializable because it dynamically looks for the serializer (and might fail). The other takes the serializer explicitly so there’s no way for it to be missing at runtime.
You’ll need to model you method signatures like this to ensure compile time checking:
fun <T> doSomeSerializing(serializer: SerializationStrategy<T>, value: T)
And you’ll need to provide the serializer when calling:
doSomeSerializing(MyDataClass.serialize(), myData)