18

Similar to JAXB generating JAXB classes to a given XSD, does Jackson provide any utility to generate Jackson classes from XSD or JSON.

JAXB class geberator has generated a set of classes for XSD schema defined. For example, for polymorphic types JAXB has the following annotation to identify the name based on XML Element name.

@XmlElements({
    @XmlElement(name = "Dog", type = Dog.class),
    @XmlElement(name = "Cat", type = Cat.class)
})
protected List<Animal> animal;

Is it possible to create similar classes in Jackson. ie., to identify the type based in XML element name.

dinup24
  • 1,652
  • 3
  • 16
  • 26
  • For the googlers: Although Jackson can handle JAXB annotations, @XmlElements is not well-supported. See https://github.com/FasterXML/jackson-modules-base/issues/127 for details. – koppor Jan 05 '23 at 22:29

1 Answers1

0

Jackson can add such information automatically (see @JsonTypeInfo). For example:

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
protected List<Animal> animal;

Or use that annotation with @JsonSubTypes:

@JsonTypeInfo(
      use = JsonTypeInfo.Id.NAME, 
      include = As.PROPERTY, 
      property = "type")
    @JsonSubTypes({
        @JsonSubTypes.Type(value = Dog.class, name = "Dog"),
        @JsonSubTypes.Type(value = Cat.class, name = "Cat")
    })
protected List<Animal> animal; 

This link is useful.