Jackson produces invalid field names from Kotlin value classes

Viewed 593

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 Answers

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.

Related