09 shows which language level each sealed feature needs; 10 runs the Payment model through Jackson 3 (jars fetched and sha1-checked, not committed). Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TF9JWFvJSNm6HVzswzZU5a
26 lines
1.1 KiB
Java
26 lines
1.1 KiB
Java
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
|
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
|
import tools.jackson.databind.json.JsonMapper;
|
|
|
|
public class JsonForgotten {
|
|
// Wallet is a permitted subtype, and the compiler is happy, but nobody told Jackson about it.
|
|
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
|
|
@JsonSubTypes({
|
|
@JsonSubTypes.Type(value = Card.class, name = "card"),
|
|
@JsonSubTypes.Type(value = Upi.class, name = "upi")})
|
|
sealed interface Payment permits Card, Upi, Wallet {}
|
|
record Card(long amount) implements Payment {}
|
|
record Upi(long amount) implements Payment {}
|
|
record Wallet(long amount) implements Payment {}
|
|
|
|
public static void main(String[] args) {
|
|
JsonMapper mapper = JsonMapper.builder().build();
|
|
System.out.println("write : " + mapper.writeValueAsString(new Wallet(500)));
|
|
try {
|
|
mapper.readValue("{\"type\":\"wallet\",\"amount\":500}", Payment.class);
|
|
} catch (Exception e) {
|
|
System.out.println("read : " + e.getClass().getSimpleName() + ": " + e.getMessage().lines().findFirst().orElse(""));
|
|
}
|
|
}
|
|
}
|