ghedeon
10/21/2019, 11:54 AMkotlin.Result
class?
Let say I want to write a custom call adapter for Retrofit that wraps all my requests in Result
class. Right now serialization converter is failing with Can't locate argument-less serializer for class kotlin.Result
which sounds reasonable. How would one workaround this situation?spand
10/21/2019, 12:48 PMThrowable
no ?ghedeon
10/21/2019, 12:54 PMResult<T>
now, where T
is an actual object to deserialize from json and Result
is something that kotlin serialization is not aware of. As you can imagine, it's one way.Nikky
10/21/2019, 12:55 PMghedeon
10/21/2019, 12:57 PMNotImplemented
exception or something.Nikky
10/21/2019, 12:58 PMval <T> T.result: KSerializer<Result<T>> = ResultSerializer<T>(this)
for the shorthandNikky
10/21/2019, 12:59 PMResultSerializer
has the child serializer and can call itghedeon
10/21/2019, 1:01 PMNikky
10/21/2019, 1:03 PMNikky
10/21/2019, 1:05 PMclass ResultSerializer<T: Any>(val childSerializer: KSerializer<Result<T>>) : KSerializer<Result<T>> {
override val descriptor: SerialDescriptor = SerialClassDescImpl(Result::class.simpleName!!)
override fun deserialize(decoder: Decoder): Result<T> {
TODO()
}
override fun serialize(encoder: Encoder, obj: Result<T>) {
encoder.encodeSerializableValue(childSerializer,obj)
}
}
ghedeon
10/21/2019, 1:05 PMfun foo(): Bar
where Bar
must be serializable. And now it's fun foo(): Result<Bar>
.Nikky
10/21/2019, 1:06 PMNikky
10/21/2019, 1:07 PMghedeon
10/21/2019, 1:07 PM.addConverterFactory(Json.asConverterFactory(MediaType.get("application/json")))
Nikky
10/21/2019, 1:07 PMNikky
10/21/2019, 1:09 PMNikky
10/21/2019, 1:10 PMNikky
10/21/2019, 1:10 PMNikky
10/21/2019, 1:11 PMNikky
10/21/2019, 1:12 PM