Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly configure an ObjectMapper to handle kotlin-specific integer types? #573

Closed
amorozov opened this issue Jun 22, 2022 · 2 comments
Labels

Comments

@amorozov
Copy link

amorozov commented Jun 22, 2022

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)

@aSemy
Copy link

aSemy commented Oct 21, 2022

Because unsigned numbers are value classes, this issue (and #591) are dependent on #199

@k163377
Copy link
Contributor

k163377 commented Feb 19, 2023

As you say, deserialization of value class is not yet supported.
Progress will be reported in #199, so this issue is closed.

@k163377 k163377 closed this as completed Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants