I have following classes:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
JsonSubTypes.Type(value = Speech::class, name = "Speech"),
JsonSubTypes.Type(value = Audio::class, name = "Audio"),
)
sealed class AudioItem
data class Speech(val type: String = "Speech", val contentType: String = "SSML", val content: String) : AudioItem()
data class Audio(val type: String = "Audio", val source: String) : AudioItem()
When I try to convert generic ArrayList
:
to AudioItem
val audio: List<AudioItem> = ObjectMapper().findAndRegisterModules().convertValue(a)
I get:
Cannot construct instance of
AudioItem
(no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
It should work when you use sealed class AudioItem()
(mind the "()"). IntelliJ hints that we can delete "()" but if I do then I can't see any constructor in the "Structure" tab.
Additionally, you don't need to explicitly set the type as in val type: String = "Speech"
. @JsonSubTypes
will do that for you.