1
0

Part 2 extra: Jackson 3 introspects permits, so @JsonSubTypes can go

This commit is contained in:
2026-08-04 17:30:29 +00:00
parent 52b2365f69
commit cab6dae3b3

View File

@@ -0,0 +1,40 @@
package com.ankurm.jackson3.part2modernjava;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import tools.jackson.databind.json.JsonMapper;
/**
* BEYOND THE POST — the Jackson-3-only shortcut.
*
* Jackson 3 introspects the `permits` clause of a sealed type, so @JsonSubTypes
* can be dropped as long as each permitted type carries @JsonTypeName. That
* removes the parallel registry which, in Jackson 2, silently drifts out of sync
* with `permits` whenever someone adds a subtype.
*
* Note there is NO @JsonSubTypes anywhere in this file.
*/
public class C04SealedAutoDiscovery {
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "shapeType")
public sealed interface Shape permits Circle, Rectangle, Triangle { }
@JsonTypeName("circle") public record Circle(double radius) implements Shape { }
@JsonTypeName("rectangle") public record Rectangle(double width, double height) implements Shape { }
// Added later. In Jackson 2 this line alone would break deserialisation until
// someone remembered to also add it to @JsonSubTypes. Here it just works.
@JsonTypeName("triangle") public record Triangle(double base, double height) implements Shape { }
public static void main(String[] args) {
JsonMapper mapper = JsonMapper.builder().build();
for (Shape original : new Shape[] {
new Circle(5.0), new Rectangle(10.0, 4.0), new Triangle(3.0, 6.0) }) {
String json = mapper.writeValueAsString(original);
Shape restored = mapper.readValue(json, Shape.class);
System.out.printf("%-24s -> %-52s -> %s%n",
original.getClass().getSimpleName(), json, restored);
}
}
}