I keep finding solutions how to do it with moshi u...
# serialization
a
I keep finding solutions how to do it with moshi using:
Copy code
val typed = Types.newParameterizedType(Envelope::class.java, type)
however I'd like to avoid pulling moshi in just to be able to work around the reflection definition
­čžÁ 1
n
this channel is for kotlinx.serialization .. not quite retrofit.. in ktx-serialization your envelope class would be fine as is.. aside from missing the annotation.. and use it as
Envelope.serializer(ResponseClass.serializer())
a
Yeah I know Its not retrofit related question. The key problem is how to define this:
Copy code
val response : Envelope<type> = json.decodeFromString(...)
I have no idea how to properly tell kotlinx.serialization about the type i get dynamically
e
when there is a generic, the serializer function takes a parameter for the serializer of each generic type parameter:
Copy code
Envelope.serializer(type.serializer())
kotlinx.serialization is designed for reflection-less usage firstly, so looking up a serializer at runtime is not really the primary usage
but if you can construct a
KType
, you could
Copy code
json.serializersModule.serializer(ktype)
once you have a serializer, use
json.decodeFromString("...", serializer)
a
Copy code
Envelope.serializer(type.javaClass.kotlin.serializer())
that one worked. Thanks a ton!