moshi-jsonapi
moshi-jsonapi copied to clipboard
Moshi-Kotlin compatibility
Hi,
I'm trying to integrate your moshi-jsonapi in my Android Kotlin project, but I'm having an issue with the compatibility with the moshi-kotlin library (working with Retrofit). Basically, I'm extending your Resource object in my Kotlin classes and I've added the JsonapiInterceptor, but it seems there is a problem parsing the data, the library is not able to parse the attributes (in our case the "name") . I have implemented this like that:
val jsonApiAdapter: JsonAdapter.Factory = ResourceAdapterFactory.builder()
.add(OurModel::class.java)
.build()
Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
.add(KotlinJsonAdapterFactory())
.add(jsonApiAdapter)
.build()
OkHttpClient.Builder()
//Other interceptors
.addInterceptor(jsonApiInterceptor)
.build()
and the implementation of my model
@JsonApi(type = "models")
data class OurModel(
val name: String
) : Resource()
and our JSON response
{
"data": [
{
"id": "1",
"type": "models",
"attributes": {
"name": "Nihao"
}
}
]
}
Do you know if I'm doing something wrong? I'm sure the problem is related to the compatibility of your Java project library with Kotlin.
Thanks so much in advance
@acampayo, I've seen it work using the @field:Json
annotation on properties of a Kotlin data class; if you want to try.
@JsonApi(type = "models")
data class OurModel(
@field:Json(name = "name")
val name: String
) : Resource()