On Tue, Aug 8, 2017 at 11:24 AM, Jothisubaramaniam P <[email protected]> wrote: > Thank you for the answer. > > The following also worked. i.e. I removed the explicit supply of type > information to the list attributes. > > public class Pets > { > @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, defaultImpl = Dog.class) > List<Dog> doggies; > > @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, defaultImpl = Cat.class) > List<Cat> kitties; > > > } > > As per the details that you have shared, this is also expected to work and > the type determination logic will work in this case as well. Is this > correct?
Yes, that should work. -+ Tatu +- > > On Monday, 6 March 2017 19:44:56 UTC-5, Tatu Saloranta wrote: >> >> On Fri, Mar 3, 2017 at 3:16 AM, Jothisubaramaniam P <[email protected]> >> wrote: >> > There is one set of definition as follows: >> > >> > @JsonTypeInfo( >> > use = JsonTypeInfo.Id.NAME, >> > include = JsonTypeInfo.As.EXISTING_PROPERTY, >> > property = "type", >> > visible = true) >> > @JsonSubTypes({ >> > @JsonSubTypes.Type(value = Dog.class, name = "dog"), >> > @JsonSubTypes.Type(value = Cat.class, name = "cat")}) >> > public abstract class Animal implements Serializable{ >> > >> > AnimalType type; >> > >> > } >> > >> > public class Dog extends Animal { AnimalType type = new >> > AnimalType("dog"); } >> > public class Cat extends Animal { AnimalType type = new >> > AnimalType("cat"); } >> > >> > There is another set of definition as follows: >> > >> > public class Pets >> > { >> > @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, defaultImpl = >> > Dog.class) >> > Doggies doggies; >> > >> > @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, defaultImpl = >> > Cat.class) >> > Kitties kitties; >> > >> > public static class Doggies extends ArrayList<Dog> { >> > >> > } >> > >> > public static class Kitties extends ArrayList<Cat>{ >> > >> > } >> > } >> > >> > The actual data on the wire is as follows(the important thing is it does >> > NOT >> > have a "type" field but otherwise is json compatible with Dog/Cat >> > types): >> > >> > { >> > "doggies": [ >> > ] >> > "kitties": [ >> > ] >> > } >> > >> > I tried to use RestTemplate to receive the data: >> > >> > restTemplate.exchange(....Pets.class); >> > >> > It worked! But the question is - is it working out of chance? or is it >> > the >> > right approach to Jackson? >> > >> > The reason I ask this question is - how does Jackson make use of >> > 'defaultImpl'? It is working on an array at the time but somehow takes >> > the >> > suggestion of "defaultImpl" to be used on the array element (rather than >> > the >> > array itself)? >> >> Ok, pertinent points are: >> >> 1. @JsonTypeInfo can be used both on classes (types) and properties; >> annotation on property overrides one in class >> 2. When applied on property, @JsonTypeInfo does indeed affect contents >> for Lists, arrays and Maps and NOT the container (value) itself >> >> The second part is bit different from some other annotations, where >> there is clear(er) distinction between value, content and key >> modifiers (like, @JsonDeserialize having "as", "contentAs" and "keyAs" >> properties). This was influenced by the way JAXB works, and since it >> seemed (at the time) that there is not all that much benefit in having >> polymorphic type information for collection/map itself, as that is >> mostly compile time fluff in Java. >> >> Also: if your data has literally empty arrays, no elements, >> polymorphic type does not matter that much. >> But it should still work even if there were elements; they should use >> `defaultImpl` from most specific annotation (per-property override). >> >> -+ 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 post to this group, send email to [email protected]. > For more options, visit https://groups.google.com/d/optout. -- 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 post to this group, send email to [email protected]. For more options, visit https://groups.google.com/d/optout.
