On Wed, Sep 25, 2019 at 12:35 AM Marc Dzaebel <[email protected]> wrote:
>
> Hi Tatu,
>
> Using only ObjectMapper#enableDefaultTyping() does not serialize into a 
> polymorphic Json-String
> 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);

^^^ why?

What is the purpose of custom TypeResolverBuilder here?
Especially one that ONLY adds type information for something declared
as `Throwable`.

No, this should not work the way code expects here.

Just use existing criteria.

>         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":
>  ...

Now: while I am not sure I fully understand the question here, there
IS one important fix, included in 2.10.0.pr3:

https://github.com/FasterXML/jackson-databind/issues/1093

which should now support use of `Object.class` as the target type WITH
polymorphic deserialization.

So you may want to try your original code with 2.10.0.pr3. Same fix
will be in 2.10.0.
It would have prevented earlier usage.

-+ Tatu +-

-- 
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/CAL4a10jcGpgtw9UiUSpFUYLVgU9XdccvGWWgbAfBqyPxbtj9mg%40mail.gmail.com.

Reply via email to