You're right, forgot init(), however, the following code prints 
"LinkedHashMap". If I uncomment setDefaultTyping(..) it prints "Exception" 
correctly. So still, polymorphic serialisation can't be controlled for an 
Object target type. 

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import 
com.fasterxml.jackson.databind.ObjectMapper.DefaultTypeResolverBuilder;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.fasterxml.jackson.databind.json.JsonMapper;
import 
com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;

public class Main4 {
    public static void main(String[] args) throws java.io.IOException, 
ClassNotFoundException {
        ObjectMapper mapper = JsonMapper.builder().activateDefaultTyping(
                LaissezFaireSubTypeValidator.instance, 
DefaultTyping.NON_FINAL, As.PROPERTY).build();
        DefaultTypeResolverBuilder typeResolver = 
                new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) {
            @Override public boolean useForType(JavaType t) { return 
Throwable.class.isAssignableFrom(t.getRawClass()); } };
        typeResolver.init(JsonTypeInfo.Id.CLASS, null);
        typeResolver.inclusion(JsonTypeInfo.As.PROPERTY);
        mapper.setDefaultTyping(typeResolver); *// comment this*
        String json=mapper.writeValueAsString(new Exception("Test"));
        System.out.println(mapper.readValue(json, 
Object.class).getClass());  *// prints LinkedHashMap*
    }
}

By the way, you might replace NoCheckSubTypeValidator with 
LaissezFaireSubTypeValidator and delete NoCheckSubTypeValidator, as it it 
completely equivalent.

Thanks, Marc

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/2ec3cf6a-bdac-4b44-b23a-8e908b090314%40googlegroups.com.

Reply via email to