Java DefaultBaseTypeLimitingValidator-class And Method Code Example

Here is an example of how to use the DefaultBaseTypeLimitingValidator class in the com.fasterxml.jackson.databind package to limit the base types that can be used for deserialization:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;

public class Example {
    public static void main(String[] args) throws Exception {
        String json = "{\"type\":\"dog\",\"name\":\"Fido\",\"bark\":true}";

        ObjectMapper mapper = new ObjectMapper();

        // Create a default base type limiting validator
        PolymorphicTypeValidator ptv = DefaultBaseTypeLimitingValidator.instance
        // deserialize JSON
        Animal animal = mapper.readValue(json, Animal.class);

        // Use the deserialized object

abstract class Animal {
    protected String name;
    public Animal(String name) { = name;
    public String getName() {
        return name;
class Dog extends Animal {
    private boolean bark;
    public Dog(String name, boolean bark) {
        this.bark = bark;
    public boolean isBark() {
        return bark;
class Cat extends Animal {
    private boolean meow;
    public Cat(String name, boolean meow) {
        this.meow = meow;
    public boolean isMeow() {
        return meow;
class Animals {

In this example, an instance of the ObjectMapper class is used to deserialize a JSON string into an instance of the Animals class.