1
0

Part 2: sealed hierarchy with explicit @JsonSubTypes registry

This commit is contained in:
2026-08-04 17:30:23 +00:00
parent 5682884b53
commit 52b2365f69

View File

@@ -0,0 +1,57 @@
package com.ankurm.jackson3.part2modernjava;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.json.JsonMapper;
import java.util.List;
/**
* Post: Jackson with Records, Optionals, Sealed Classes
* https://ankurm.com/jackson-java-records-optionals/
* Section: "Jackson with Sealed Classes (Java 17+)"
*
* The explicit-registry form: @JsonTypeInfo plus a hand-maintained @JsonSubTypes.
* Compare with C04SealedAutoDiscovery, which drops the registry entirely.
*/
public class C03SealedWithSubTypes {
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "shapeType")
@JsonSubTypes({
@JsonSubTypes.Type(value = Circle.class, name = "circle"),
@JsonSubTypes.Type(value = Rectangle.class, name = "rectangle")
})
public sealed interface Shape permits Circle, Rectangle { }
public record Circle(double radius) implements Shape { }
public record Rectangle(double width, double height) implements Shape { }
public static void main(String[] args) {
JsonMapper mapper = JsonMapper.builder().build();
String json = "["
+ "{\"shapeType\":\"circle\",\"radius\":5.0},"
+ "{\"shapeType\":\"rectangle\",\"width\":10.0,\"height\":4.0}"
+ "]";
List<Shape> shapes = mapper.readValue(json, new TypeReference<List<Shape>>() { });
// Java 21 pattern matching for switch — exhaustive because Shape is sealed,
// so no default branch is needed and a new permitted type is a compile error.
for (Shape shape : shapes) {
String description = switch (shape) {
case Circle c -> "Circle with radius: " + c.radius();
case Rectangle r -> "Rectangle " + r.width() + " x " + r.height();
};
System.out.println(description);
}
// CAREFUL: writeValueAsString(List<Shape>) loses the discriminator, because
// the runtime type of the list carries no element type for Jackson to read.
// The result does not round-trip. See part5polymorphic/F02 for the full story.
System.out.println("lossy : " + mapper.writeValueAsString(shapes));
System.out.println("correct : " + mapper.writerFor(new TypeReference<List<Shape>>() { })
.writeValueAsString(shapes));
}
}