Hi,

I defined a mapper that serializes Exception's polymorphically, but the 
following simple map serializes without @class attribute in its values:

Map<Integer, Exception> map = new HashMap<>();
map.put(1, new Exception("test"));
mapper.writeValueAsString(map) --> {"1":{"stackTrace":[...], ...}}  // 
missing *@class*

while e.g. Exception is serialized correctly:

mapper.writeValueAsString(new Exception("test")) --> 
{"*@class*":"java.lang.Exception", 
"stackTrace":[...], ...}

Is this intended or is there a setting, that enables transitive polymorphic 
serialisation in Map values? May be I have to write 
serializers/deserializers?

Here is a minimal example:

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
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;

public class MapValues {
    public static void main(String[] args) throws JsonProcessingException {
        final ObjectMapper mapper = new ObjectMapper();
        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);
        System.out.println("Excpetion:\n" + mapper.writeValueAsString(new 
Exception()));

        Map<Integer, Exception> map = new HashMap<>();
        map.put(Integer.valueOf(1), new Exception());
        System.out.println("\nMap:\n" + mapper.writeValueAsString(map));
    }
}

-- 
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/4471e5e1-8b76-4b1a-a81d-9cc423714cb8%40googlegroups.com.

Reply via email to