'Failed to generate the Json adapter with Moshi

I'm receiving a response from BE and the response is a base64 encoded image. The response looks like this: {"image":"/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/ ...} (whole response here: https://pastebin.com/ViFTAhRw)

Looks like a property named image followed by a string. So I've created my model class:

@JsonClass(generateAdapter = true)
data class ApiBase64Image(
    @field:Json(name = "image") val imageString: String?
) {

    fun toDomain(): Base64Image {
        return Base64Image(imageString.orEmpty())
    }
}

And finally, my DI object:

@Module
@InstallIn(SingletonComponent::class)
object ApiModule {

    @Provides
    @Singleton
    fun provideApi(builder: Retrofit.Builder): MyApi {
        return builder
            .build()
            .create(MyApi::class.java)
    }

    @Provides
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit.Builder {
        return Retrofit.Builder()
            .baseUrl(ApiConstants.BASE_ENDPOINT)
            .client(okHttpClient)
            .addConverterFactory(MoshiConverterFactory.create())
    }

    @Provides
    fun provideOkHttpClient(
        authenticationInterceptor: AuthenticationInterceptor
    ): OkHttpClient {
        return OkHttpClient.Builder()
            .addInterceptor(authenticationInterceptor)
            .build()
    }
}

This code, however, does not work as I'm receiving errors: Unable to create converter for class ... .ApiBase64Image Failed to find the generated JsonAdapter class for class ... .ApiBase64Image

I'm not sure what is giving Moshi problems. Is it the data class serialization? Or my DI setup? Or something else entirely?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source