deserializing subtypes #305
-
|
See the following code: import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import tools.jackson.databind.json.JsonMapper;
@JsonTypeInfo(
use = JsonTypeInfo.Id.SIMPLE_NAME, // add a "type" property
include = JsonTypeInfo.As.PROPERTY
)
@JsonSubTypes({
@JsonSubTypes.Type(Animal.Dog.class),
@JsonSubTypes.Type(Animal.Cat.class)
})
sealed interface Animal {
static void main() {
System.out.println(new JsonMapper()
//language=JSON
.readValue("""
{
"@type": "Dog",
"name": "Rex",
"age": 5
}""",
Animal.class
)); // Dog[name=Rex, age=5]
}
record Dog(String name, int age) implements Animal {
}
record Cat(String name, boolean indoor) implements Animal {
}
}is there a way to not mention |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Have a look at https://github.com/sigpwned/jackson-modules-java-17-sealed-classes I'm travelling so don't have good web access. I vaguely recall that the above code was merged in Jackson 3 but if you use Jackson 2, you will need to use that sealed classes extension. |
Beta Was this translation helpful? Give feedback.
-
|
See FasterXML/jackson-databind#5025 -- support is in 3.0.0 I think. |
Beta Was this translation helpful? Give feedback.
See FasterXML/jackson-databind#5025 -- support is in 3.0.0 I think.