Moshi: @field:Json annotation not working and @Json annotation not detected in reflection

Created on 22 Jan 2019  路  4Comments  路  Source: square/moshi

I am getting this error, "com.squareup.moshi.JsonDataException: Required value 'pizzaPlaces' missing at $", when I use @field:Json.

It works when I use @Json but it is not detectable by reflection as there is no "Target" defined.

I use moshi-kotlin 1.8.0 and also have added KotlinJsonAdapterFactory.

data class PizzaPlacesDto(
      @field:Json(name = "pizza_places")
      var pizzaPlaces: PizzaPlaces
)

I know this can be solved by using both the annotations but that would be a little cumbersome.

Most helpful comment

Just to chip in on this old issue in case anyone else ends up here from Google search.

We were having similar issue. @field:Json(name = "channel_name") was not working since moving to use KotlinJsonAdapterFactory. Removing field resolved the issue for us @Json(name = "channel_name") 馃憤

Thanks for the suggestion @ZacSweers (from #857).

All 4 comments

@kapil93 please use: kapt("com.squareup.moshi:moshi-kotlin-codegen:1.8.0")

@JsonClass(generateAdapter = true)
data class PizzaPlacesDto(
    @Json(name = "pizza_places")
    var pizzaPlaces: PizzaPlaces
)

This works in our tests for the KotlinJsonAdapterFactory. Can you make a reproducible test case that demonstrates the problem? Are you sure the KotlinJsonAdapterFactory is added to the Moshi instance that you are using?

Nothing actionable here. Feel free to reopen if you can offer a repro case

Just to chip in on this old issue in case anyone else ends up here from Google search.

We were having similar issue. @field:Json(name = "channel_name") was not working since moving to use KotlinJsonAdapterFactory. Removing field resolved the issue for us @Json(name = "channel_name") 馃憤

Thanks for the suggestion @ZacSweers (from #857).

Was this page helpful?
0 / 5 - 0 ratings