Kotlin classes with Json annotations
See original GitHub issueThe adapters don’t work when using Kotlin classes with Json(name = "..")
annotations. This is because annotations in Java are not the same as annotations in Kotlin and the adapters find no Json annotation.
The moshi readme states this:
Kotlin classes work with Moshi out of the box, with the exception of annotations. If you need to annotate your Kotlin classes with an @Json annotation or otherwise, you will need to use the moshi-kotlin artifact, and set up Moshi to use its converter factory:
val moshi = Moshi.Builder() .add(KotlinJsonAdapterFactory()) .build()
But this causes the DocumentAdapter
to choose KotlinJsonAdapter
as dataJsonAdapter
instead of a ResourceAdapter
, so the serialization to a json output with attributes, relationships, etc doesn’t work.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Another solution: Just annotate them with
@field:Json
instead of@Json
I ran into this same problem. I was able to solve it with the following