Java StdTypeResolverBuilder-class And Method Code Example


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

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

public class MyTypeResolver {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDefaultTyping(new StdTypeResolverBuilder().init(JsonTypeInfo.Id.NAME,
                new TypeNameIdResolver()));

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

StdTypeResolverBuilder is used to configure the type resolver for Jackson, which is responsible for resolving the actual Java class to use for a given JSON type. The init method is used to set the type identifier type (in this case, JsonTypeInfo.Id.NAME) and the type identifier resolver (in this case, TypeNameIdResolver). The setDefaultTyping method is used to set the type resolver for the ObjectMapper.

Note that TypeNameIdResolver is a custom class that you need to define and implement and JsonTypeInfo.Id is an enumeration from jackson which have several different options to identify the type of class.