Java SubTypeValidator-class And Method Code Example


Here is an example of how the com.fasterxml.jackson.databind.jsontype.impl.SubTypeValidator class might be used:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.SubTypeValidator;

public class MyTypeValidator {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(),
                ObjectMapper.DefaultTyping.NON_FINAL, SubTypeValidator.instance);

        // use the ObjectMapper to read/write your objects
    }
}

SubTypeValidator is used to specify the validation rules that should be applied when trying to deserialize an object of a subtype. This class is an implementation of PolymorphicTypeValidator and can be passed to activateDefaultTyping method of ObjectMapper.

The activateDefaultTyping method is used to enable automatic type detection for the ObjectMapper. The first argument is the PolymorphicTypeValidator to use (in this case, SubTypeValidator.instance) and the second argument is the default typing mode to use (in this case, ObjectMapper.DefaultTyping.NON_FINAL).

The SubTypeValidator class provides a default implementation of PolymorphicTypeValidator that allows any subtype to be deserialized, as long as the subtype is not final and has a default constructor.

It's also worth noting that activateDefaultTyping is a method that has been deprecated in the latest version of jackson and it is recommended to use ObjectMapper#registerModule(com.fasterxml.jackson.databind.module.SimpleModule) instead