9

When a Kotlin class has a property that is one of the new @JvmInline value classes, Jackson converts it to JSON with a weird suffix on the field name.

e.g.

@JvmInline
value class ModelName(val value: String)
data class MyDto(val modelName: ModelName)

Jackson will produce JSON that looks like this:

{
    "modelName-11MJ8YI": "Some Model Name"
}

I've tried adding a @JsonProperty("modelName") annotation but it doesn't make a difference.

1 Answer 1

7

Probably, upgrading jackson-module-kotlin will solve this problem. https://github.com/FasterXML/jackson-module-kotlin/issues/356

If you can't upgrade, naming the getter as @get:JvmName("getModelName") should also solve the problem.

The reason for this problem is that the name of the method (getter) for the value class in Kotlin has a random suffix.

2
  • Yes, a schoolboy error on my part. Thanks.
    – KevinS
    Jun 13, 2021 at 22:24
  • 5
    Upgrading jackson-module-kotlin didn't fix it for me at first because I was using a custom ObjectMapper. I was able to fix it by creating an instance of com.fasterxml.jackson.module.kotlin.jacksonObjectMapper instead of com.fasterxml.jackson.databind.ObjectMapper (just in case someone stumbles on the same issue).
    – helios35
    Jul 21, 2022 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.