On Thu, Sep 26, 2019 at 11:02 PM Marc Dzaebel <[email protected]> wrote:
>
> 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.
No. There is still a problem with type matching, the way you wrote it:
> 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()); } };
This ONLY enables polymorphic type handling for declared type of
`Throwable` but...
> typeResolver.init(JsonTypeInfo.Id.CLASS, null);
> typeResolver.inclusion(JsonTypeInfo.As.PROPERTY);
> mapper.setDefaultTyping(typeResolver); // comment this
> String json=mapper.writeValueAsString(new Exception("Test"));
type here is runtime type, `Exception` (you could force that with
`writerFor(type)`), and
> System.out.println(mapper.readValue(json, Object.class).getClass());
> // prints LinkedHashMap
here you declare base type to be `Object`.
Neither is `Throwable`. This is why Type Id is not used, nor expected.
And I still do not understand quite why not simply enable default
typing for `DefaultTyping.NON_FINAL`: that should match both types
used here.
There is no reason to sub-class `DefaultTypeResolverBuilder`.
-+ 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/CAL4a10iZz0nXAr_gEv4BdGNj7VzJ1LiPfDmzzuULw0mtsci0zQ%40mail.gmail.com.