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

Can I deserialize a data class with an unsigned member in the constructor? #591

Closed
hanzo2001 opened this issue Oct 21, 2022 · 3 comments
Closed
Labels

Comments

@hanzo2001
Copy link

Can I deserialize a data class with an unsigned member in the constructor?

Jackson 2.13.4

I am working with some unsigned numbers and I know that I will be deserializing incoming json. I would like to directly map the numbers to their unsigned representation. I checked #525 and #524 and upgraded to 2.13.4

I am seeing an error that does not make sense

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of A (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

My simplified model clearly has a no-arg constructor (like all my other data classes that do work)

data class A(
    var a: String = "",
    var b: ULong = 0U,
)

Am I doing something wrong? Is this a bug?

My test case:

class DataClassConstructorWithUnsignedTest {

    @Test
    fun a() {

        val a1 = A()
        val mapper = jacksonObjectMapper()
        val serialized = mapper.writeValueAsString(a1)
            .also(::println)

        assertDoesNotThrow {
            val deserialized = mapper.readValue(serialized, A::class.java)
                .also(::println)
        }
    }
}

Stack trace

org.opentest4j.AssertionFailedError: Unexpected exception thrown: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `A` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"a":"","b":0}"; line: 1, column: 2]
	at DataClassConstructorWithUnsignedTest.a(DataClassConstructorWithUnsignedTest.kt:17)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `A` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{"a":"","b":0}"; line: 1, column: 2]
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1904)
	at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1349)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1415)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:351)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:184)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3629)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3597)
	at DataClassConstructorWithUnsignedTest$a$1.invoke(DataClassConstructorWithUnsignedTest.kt:19)

@hanzo2001
Copy link
Author

hanzo2001 commented Oct 21, 2022

I see that someone else asked this in #573

@hanzo2001
Copy link
Author

This question is subordinate to a longer discussion 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

2 participants