I saw https://github.com/FasterXML/jackson-databind/issues/336 and in the 
end they've repeatedly asked to take this here, so here I am. TLDR - if you 
have an abstract class (or interface) with @JsonTypeInfo(...) and put a 
bunch of those into a collection the type information of each element will 
not be serialized when you serialize the collection (and same thing with 
maps). I don't understand the reasoning given in the ticket - this is only 
an issue on serialization when the implementation information is clearly 
available. You have concrete implementation class of the object to 
serialize since you've obviously managed to create the object. Of course 
you know all of its superclasses and implemented interfaces and whatever 
annotations come with those. I say again, this is only an issue on 
serialization, if you write JSON for a collection with type information in 
each element it will get deserialized no problem without any kind of hacks 
as expected. Here's an example:

package foo;


import java.util.Map;


import com.fasterxml.jackson.annotation.JsonGetter;

import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.annotation.JsonTypeInfo;

import com.fasterxml.jackson.annotation.JsonTypeInfo.As;

import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

import com.fasterxml.jackson.databind.ObjectMapper;


public class Test {


  @JsonTypeInfo (include = As.PROPERTY, use = Id.CLASS)

  public static abstract class Foo {

    @Override

    public String toString () {

      return "." + this.getClass ().getSimpleName ();

    }


    //@JsonGetter ("@class")

    //private String type () {

    //  return getClass ().getName ();

    //}

  }


  public static class Bar extends Foo {

    private @JsonProperty String lol = "lol";

  }


  private Map<String, Foo> foo;

  private Map<String, Bar> bar;


  public static void main (String[] args) throws Exception {

    ObjectMapper m = new ObjectMapper ();

    Foo f = new Bar ();

    System.out.println ("1) " + m.writeValueAsString (f));

    System.out.println ("2) " + m.readValue ("{\"@class\":\"foo.Test$Bar\"}", 
Foo.class));

    Object o = m.readValue ("{\"b\":{\"@class\":\"foo.Test$Bar\"}}",

                            m.constructType (Test.class.getDeclaredField (
"bar").getGenericType ()));

    o = m.readValue ("{\"f\":{\"@class\":\"foo.Test$Bar\"}}",

                     m.constructType (Test.class.getDeclaredField 
("foo").getGenericType 
()));

    System.out.println ("3) " + o);

    System.out.println ("4) " + m.writeValueAsString (o));

    System.out.println ("5) " + m.readValue (m.writeValueAsString (o),

                                             m.constructType 
(Test.class.getDeclaredField 
("foo").getGenericType ())));

  }

}


The last sysout does not work unless you comment in the @JsonGetter (the 
hack to make it work) but then the type information appears twice if you 
just serialize the object itself - not as being part of a collection

-- 
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 post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to