question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kotlin classes with Json annotations

See original GitHub issue

The 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:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
Kaned1ascommented, Dec 13, 2017

Another solution: Just annotate them with @field:Json instead of @Json

1reaction
bricestaceycommented, Nov 7, 2017

I ran into this same problem. I was able to solve it with the following

    class MoshiJsonNameMapping : JsonNameMapping {
        override fun getJsonName(field: Field): String {
            val foo = field.kotlinProperty?.findAnnotation<Json>()
            return foo?.name ?: field.name
        }
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Usage of Jackson @JsonProperty annotation for kotlin data ...
@JsonProperty annotations in your code are all put on private fields within your data class and by default Jackson doesn't scan private ...
Read more >
Jackson Support for Kotlin - Baeldung
We'll explore how to serialize and deserialize both Objects and Collections. We'll also make use of @JsonProperty and @JsonInclude annotations.
Read more >
Annotations | Kotlin
In Java, an annotation type is a form of an interface, so you can implement it and use an instance. As an alternative...
Read more >
Parsing data with Jackson using kotlin | ProAndroidDev
Jackson is one of the famous library to parse XML or JSON data though with Kotlin, some key factors are required to avoid...
Read more >
Painless JSON with Kotlin and jackson - Mario Fernandez
We will be using data classes to represent the entities that will get converted to a from JSON. They are the equivalent of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found