Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moshi-Kotlin compatibility #106

Open
acampayo opened this issue Feb 7, 2020 · 1 comment
Open

Moshi-Kotlin compatibility #106

acampayo opened this issue Feb 7, 2020 · 1 comment

Comments

@acampayo
Copy link

acampayo commented Feb 7, 2020

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

@es0329
Copy link

es0329 commented Feb 7, 2020

@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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants