How to use Jackson JsonSubTypes annotation in Kotlin

Turns out it's a bug in the compiler, thanks for reporting it. To work around this issue, you can import JsonSubTypes.Type and use it without qualification:

import org.codehaus.jackson.annotate.JsonSubTypes.Type

JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
JsonSubTypes(
    Type(value = javaClass<Comment>(), name = "CommentNote"),
    Type(value = javaClass<Photo>(), name = "PhotoNote"),
    Type(value = javaClass<Document>(), name = "DocumentNote")
)
abstract class Note : Identifiable {
    [...]

I believe this has been resolved and nowadays you can write it like this:

import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo

@JsonTypeInfo(
   use = JsonTypeInfo.Id.NAME,
   include = JsonTypeInfo.As.PROPERTY,
   property = "type")
   @JsonSubTypes(
       JsonSubTypes.Type(value = Comment::class, name = "CommentNote"),
       JsonSubTypes.Type(value = Photo::class, name = "PhotoNote"),
       JsonSubTypes.Type(value = Document::class, name = "DocumentNote"))
interface Note

Note the missing @ and class notation in the JsonSubTypes.Type.

Tags:

Jackson

Kotlin