Jackson ObjectMapper not including Type Info for elements in containers... why?

Viewed 68

So I have code that looks like this:

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
    sealed interface Completion {
        @JsonTypeName("Success")
        data class Success(val at: Instant) : Completion

        @JsonTypeName("Failure")
        data class Failure(val at: Instant) : Completion
    }   
    
    val mapper: ObjectMapper = ...

    println(mapper.writeValueAsString(listOf(Completion.Failure(at = Instant.now()))))
    //This last line prints something like  [{"at":"2021-11-09T22:02:17.238467Z"}]

    println(mapper.writeValueAsString(Completion.Failure(at = Instant.now()))))
    //This last one prints it correctly, with the @type field

What an I doing with my ObjectMapper that once I place an object inside a container, it will use the wrong codec.

0 Answers
Related