Reputation: 1772
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.
Upvotes: 22
Views: 18867
Reputation: 3660
Jackson itself does not provide a direct tool like JAXB's xjc for generating classes from an XSD.
You can use tools like jsonschema2pojo to generate POJOs from JSON Schema or JSON, which will use Jackson annotations
Upvotes: 1
Reputation: 9150
For generating Java code from JSON (schema), how about jsonschema2pojo?
Just a note: XML is to JSON as XML Schema (XSD) is to JSON Schema.
Upvotes: 0
Reputation: 130
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.
Upvotes: -2