Hey 👋I had a weird behavior just now and wanted to understand it:
data class Example(@get:JsonProperty("element") val elements: List<Element> = listOf())
I remembered somehow that you need to add the get: part to a JsonProperty annotation such that it would be at the getter and not the field from Javas point of view, or else it woudn’t work right (which seems to be not the case after all), but this code above resulted in the following exception:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `int` from Array value (token `JsonToken.START_ARRAY`)
Now I strongly assume this is because List has a method
with parameter of type
, but is this really how this annotation format should work? How would I even attach an annotation to the java getter in this case then?
I fixed it now by removing the get:, just curious 🤔
1 year ago
is an example of a use-site target; it restricts what the annotation applies to
In the context of Kotlin classes, declaring
parameters does many things: creates a constructor parameter, creates a backing field for that parameter, and creates getters & setters
means that the
annotation will only be applied to the getter, not any of the other contexts
1 year ago
Yeah I assumed that would be the case, but in my example above, the annotation seems to be applied to the List::get method, not elements get(), which returns the List. Just seems like an edge case when the annotated property has a method get() in addition to the get method from the property itself.