JakeWharton/retrofit2-kotlinx-serialization-converter

How to use retrofit converter with custom Serializers ?

anjosc opened this issue · 2 comments

Hi! I'm not sure this is the correct place, but I need to specify the deserializer for a custom filtering list

When this is inside a regular class I'm able to do

@Serializable
data class ListWrapper(
	@Serializable(with = MySpecialListSerializer::class)
	val items : List<Something>
)

and have a

interface Api {
	@GET("v1/listOfSomething")
	fun getListOfSomething(): Call<ListWrapper>
}

However I have the list returned directly as in

fun getListOfSomething(): Call<List<Something>>

and declaring it like this, isn't picked up by retrofit / kxs converter:

fun getListOfSomething(): Call<@Serializable(with = MySpecialListSerializer::class) List<Transaction>>

How can I specify which custom Serializers to use when using retrofit with the retrofit2-kotlinx-serialization-converter ?
Thanks !

Yeah I'm also having problems, specifically with sealed classes.

Annotations on the generic type parameter of the Call interface will be lost because Retrofit is written in Java and does not use Kotlin reflection to obtain the metadata on which that annotation is stored. As a result, you will always get effectively an un-annotated type.

A workaround would be to declare a type such as interface SomethingList : List<Something> which has a @Serializable annotation and a custom serializer which delegates to ListSerializer(Something.serializer()).

There is no way for this converter to work around this limitation, unfortunately. We can't even detect when it's happening to show some kind of error.