kamikat / moshi-jsonapi

JSON API v1.0 Specification in Moshi.
MIT License
156 stars 35 forks source link

Moshi-Kotlin Support #83

Open tobiasrohloff opened 5 years ago

tobiasrohloff commented 5 years ago

When creating a Resource model class with Kotlin some features are not supported, like custom-named fields with @Json (see https://github.com/square/moshi/issues/315). However, moshi introduced moshi-kotlin and moshi-kotlin-codegen to fix that. A current limitation is that the superclass needs to be a Kotlin class as well, which is not the case for Resource.

Even without the superclass problem I am unsure whether these libraries work together with this one. The reflection approach needs a separate adapter factory:

val moshi = Moshi.Builder()
    // ... add your own JsonAdapters and factories ...
    .add(KotlinJsonAdapterFactory())
    .build()

Looks like this would require to adjust the ResourceAdapterFactory.

The codegen approach generates adapters at compile time. Maybe this approach could be used together with ResourceAdapterFactory? It is also the recommended one since its footprint is much smaller.

As a workaround for this specific feature (@Json annotation) @field:Json can be used. At least it worked for me. But it is not recommended or tested.

Fintasys commented 4 years ago

Since this issue is still open I gonna comment here. I have the same issue right now with Moshi 1.8 (with Moshi-Kotlin), @Json is ignored on release build but works in Debug. Now I have changed @Json to @field:Json everywhere but now it doesn't work in my tests anymore when I run them in debug mode. Any solutions for this?