I have a class with an value class and a sealed cl...
# jackson-kotlin
j
I have a class with an value class and a sealed class together, and don't really understand why they fail.
Copy code
data class FooDto(
    val myId: MyId = MyId(),
    @JsonDeserialize(using = SealedTypeDeserializer::class)
    val type: SealedType = TypeA.FOO,
)
🧵 for more info Having just one of them works fine when deserializing, but together does not work. Any clue?
Copy code
fun main() {
    val om = ObjectMapper().registerKotlinModule()
    val json = om.writeValueAsString(FooDto())
    println(json)
    val obj = om.readValue<FooDto>(json)
    println(obj)
}

data class FooDto(
    val myId: MyId = MyId(),
    @JsonDeserialize(using = SealedTypeDeserializer::class)
    val type: SealedType = TypeA.FOO,
)

@JvmInline
value class MyId(val id: UUID = UUID.randomUUID())

sealed interface SealedType

enum class TypeA: SealedType{
    FOO
}
val types: Map<String, SealedType> = listOf(TypeA.entries).flatten().associateBy { it.name }

class SealedTypeDeserializer : JsonDeserializer<SealedType>() {
    override fun deserialize(p: JsonParser, ctxt: DeserializationContext): SealedType {
        return types[p.text] ?: error("...")
    }
}
Copy code
{"myId":"eedec9c0-2b54-4f58-9f08-5cbba604e51a","type":"FOO"}
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `SealedType` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 55] (through reference chain: no.nav.tilleggsstonader.sak.vilkår.vilkårperiode.FooDto["type"])
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1887)
	at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:414)
	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1375)
	at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:274)
	at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:545)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:570)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:440)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1493)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4905)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3848)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3831)
In SettableBeanProperty
Copy code
Object value =  _valueDeserializer.deserialize(p, ctxt);
uses AbstractSerializer when using both fields in the data class. But when using only
type
SealedTypeDeserializer
is used.
Moving
@JsonDeserialize
to the sealed interface seems to solve the problem.
Copy code
@JsonDeserialize(using = SealedTypeDeserializer::class)
sealed interface SealedType
But shouldnt it be possible to have it in the DTO-class? On field level
And this also works
Copy code
om.registerModule(SimpleModule().also {
    it.addDeserializer(SealedType::class.java, SealedTypeDeserializer())
})
w
I haven't read the whole thing in detail, but have you checked the following issue? https://github.com/FasterXML/jackson-module-kotlin/issues/651
j
Not sure it's the same issue, thanks anyhow!
w
If the issue submitted to databind is a duplicate, please close it.
j
Took a new look, I think it is a duplicate. Thanks! Closed it now
thank you color 1
122 Views