jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Auto-detect Java 17 sealed classes without manually specifying them in @JsonSubTypes

Open slutmaker opened this issue 4 years ago • 1 comments

Since sealed classes were included in the java 17 release as a stable feature, I think jackson should support them and automatically detect possible subtypes without the @JsonSubTypes annotation.

Usage example

    @JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
    sealed interface UserEvent permits UserCreatedEvent, UserBlockedEvent {
        int getUserId();
    }

    @Value
    final class UserCreatedEvent implements UserEvent {
        LocalDateTime createdAt;
        int userId;
        //...
    }

    @Value
    final class UserBlockedEvent implements UserEvent {
        LocalDateTime blockedFrom;
        LocalDateTime blockedTo;
        int userId;
        //...
    }

Additional context Similar issue for kotlin sealed classes: https://github.com/FasterXML/jackson-module-kotlin/issues/239

slutmaker avatar Sep 19 '21 09:09 slutmaker