Hi everyone I have the following code that I use to serialize into json strings to store in shared preferences
interface Serializer {
public fun <T> serialize(data: T, type: Class<T>): String
public fun <T> deserialize(source: String, type: Class<T>): T?
}
I can later create implementations of this class and serialize my objects using Moshi like the following
class SerializerImpl(
private val moshi: Moshi = Moshi.Builder()
.build(),
) : Serializer {
override fun <T> deserialize(source: String, type: Class<T>): T? = try {
moshi.adapter(type).fromJson(source)
} catch (exception: Exception) {
null
}
override fun <T> serialize(data: T, type: Class<T>): String =
moshi.adapter(type).toJson(data)
}
Can you advice how to migrate this implementation to use kotlinx serialization without having to change the signature of the Serializer interface it seems like I must change the type of my Serializer interface to use KSerializer in order for this to work but I would really like to avoid that.