class GetBusinessMapResponseDTODeserializer : JsonDeserializer<GetBusinessMapResponseDTO> {
override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): GetBusinessMapResponseDTO {
...
}
}
@Provides
@Singleton
fun provideRetrofit() : Retrofit {
val gsonBuilder = GsonBuilder()
.registerTypeAdapter(
GetBusinessMapResponseDTODeserializer::class.java,
GetBusinessMapResponseDTODeserializer())
.create()
val retrofit = Retrofit.Builder()
.baseUrl(IamHereBackendAPI.BASE_URL)
.addCallAdapterFactory(ResultAdapterFactory())
.addConverterFactory(GsonConverterFactory.create(gsonBuilder))
.build()
return retrofit
}
@POST("getAllBusinessesByFilterMap")
suspend fun getBusinessMapByFilter(
@Body getBusinessMapRequest: GetBusinessMapRequest
) : RequestResult<GetBusinessMapResponseDTO>
Hello! Can you please tell me why Gson does not want to use a custom deserializer? I thought the problem was that the response from the api was wrapped in the RequestResult class, but apparently this is not the problem
implementation "com.squareup.retrofit2:retrofit:2.9.0" implementation "com.squareup.retrofit2:converter-gson:2.9.0"
The problem is with your registerTypeAdapter
call. The first argument is the class you want to register the adapter for. So instead of providing GetBusinessMapResponseDTODeserializer::class.java
you should provide GetBusinessMapResponseDTO::class.java
as argument:
val gsonBuilder = GsonBuilder()
.registerTypeAdapter(
GetBusinessMapResponseDTO::class.java,
GetBusinessMapResponseDTODeserializer())
.create()