Hi Tatu,
1. Using only ObjectMapper#enableDefaultTyping() does not serialize into
a polymorphic Json-String
2. Using only ObjectMapper#enableDefaultTyping() does not deserialize a
polymorphic Json-String --> error
Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array
to contain As.WRAPPER_ARRAY type information for class java.lang.Object
So the question remains. How to serialize/deserialize an Object
polymorphically.
Here is the code, that demonstrates the problem:
import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
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 MapValues2 {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
final ObjectMapper serializer = new ObjectMapper(), deserializer =
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);
serializer.setDefaultTyping(typeResolver);
deserializer.enableDefaultTyping();
String polyJson = serializer.writeValueAsString(new Exception());
System.out.println("Excpetion:\n" + polyJson);
System.out.println("1: Read poly:" +
deserializer.readValue(polyJson, Object.class).getClass()); // error
}
}
Exception in thread "main"
com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected
token (START_OBJECT), expected START_ARRAY: need JSON Array to contain
As.WRAPPER_ARRAY type information for class java.lang.Object
at [Source:
(String)"{"@class":"java.lang.Exception","cause":null,"stackTrace":["[Ljava.lang.StackTraceElement;",[{"@class":"java.lang.StackTraceElement","methodName":"main","fileName":"MapValues2.java","lineNumber":25,"className":
...
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/1d0bd637-c314-4479-9f75-edf3e3168e5d%40googlegroups.com.