Anton111111
Anton111111

Reputation: 686

Moshi and annotation @field:Json(name="some_field") and Json(name="some_field")

After upgrading retrofit to version 2.11.0 and moshi to version 1.15.1, everything broke for me. Previously, I used the annotation @field:Json(name="some_field") in models. But now for models that are used in Retrofit, I have to use @Json(name="some_field"), and in models that I use for Moshi adapter toJson/fromJson, I have to use @field:Json(name="some_field").

This is how I create the adapter and only @field:Json(name="some_field") works in it:

 private val watchProvidersJsonAdapter: JsonAdapter<List<WatchProviderData>> =
        Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build().adapter(
            Types.newParameterizedType(
                List::class.java,
                WatchProviderData::class.java
            )
        )

For Retrofit, I create it like this and only @Json(name="some_field") works in it:

private val api = Retrofit.Builder()
    .addConverterFactory(
        MoshiConverterFactory.create(
            Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build()
        )
    )
    .baseUrl("https://api.themoviedb.org")
    .client(
        OkHttpClient.Builder()
            .apply {
                addInterceptor(AuthTheMovieDBInterceptor())
                if (BuildConfig.DEBUG) {
                    addInterceptor(HttpLoggingInterceptor().apply {
                        setLevel(
                            HttpLoggingInterceptor.Level.BODY
                        )
                    })
                }
            }
            .build()
    )
    .build()
    .create(API::class.java)

Why do I have to use different annotations?

Upvotes: 0

Views: 58

Answers (0)

Related Questions