jackson-module-kotlin
jackson-module-kotlin copied to clipboard
How to properly configure an ObjectMapper to handle kotlin-specific integer types?
I would like to ask how to properly configure an ObjectMapper to make it capable of deserializing data classes with kotlin-specific integer fields.
Out of the box it works e.g. with raw UInts:
val actual: UInt = objectMapper.readValue("4294967295")
val expected: UInt = 4294967295u
assertThat(actual).isEqualTo(expected)
However, a simple data class like this
data class U(
@JsonProperty("x") // no matter if this annotation exists or not
@JsonDeserialize(using = UIntDeserializer::class) // no matter if this annotation exists or not
val x: UInt
)
val actual: U = objectMapper.readValue("""{"x":4294967295}""")
// val expected: U(4294967295u)
// assertThat(actual).isEqualTo(expected)
The error message is:
Cannot construct instance of `U` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"x":4294967295}"; line: 1, column: 2]
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `U` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"x":4294967295}"; line: 1, column: 2]
at app//com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at app//com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1728)
...
Changing the type of x to Int lets it work as expected (obviously, the upper half of an integer values can not be handled).
So the question is how to properly configure ObjectMapper to handle these cases.
jackson-2.13.3 (the version comes from Spring Boot 2.7.0 BOM)