I have an interface Data
with a function called toData()
. Different objects inherits from this Data
.
Now I wrote an adapter which transform the result of toData()
into an string
My custom adapter looks like this:
class Base64TypeAdapter {
@FromJson
fun fromJson(value: String?): ByteArray? {
return value?.toByteArray()
}
@ToJson
fun toJson(value: Data?): String? {
return value?.toData()?.let { Base64.encodeToString(it.toByteArray(), Base64.NO_WRAP) }
}
}
I created a Moshi builder
private val proxyManMoshi = Moshi.Builder()
.add(Base64ArrayTypeAdapter())
.add(Base64TypeAdapter())
.add(KotlinJsonAdapterFactory())
.build()
val dataAdapter = proxyManMoshi.adapter<Base64TypeAdapter>(Data::class.java)
But when I call dataAdapter.toJson()
the input parameter is Base64TypeAdapter
instead of Data
.
How do I mount an adapter correctly?