Skip to content

Moshi-Kotlin compatibility #106

@acampayo

Description

@acampayo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions