If you used JAXB to generate your classes, you can have something like @XmlElements with different types for parsing a list.
Now, if you also use the same JAXB classes with Jersey / Jackson, you can improve the class metadata by adding @JsonTypeInfo and @JsonSubTypes to describe how to format the list / array name of objects.
While @JsonTypeInfo describes the type to be added, @JsonSubTypes provides options for a nested collection. For example, As.PROPERTY to define an output property, as shown in the example below, where a list of entities that can have elements of different types, including the type itself ("Form") in addition to the other two types, "Field" and "Table" .
public class Form { @XmlElements({ @XmlElement(name = "field", type = Field.class), @XmlElement(name = "form", type = Form.class), @XmlElement(name = "table", type = Table.class) }) @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "obj") @JsonSubTypes({ @JsonSubTypes.Type(value = Field.class), @JsonSubTypes.Type(value = Form.class), @JsonSubTypes.Type(value = Table.class) }) @Generated(value = "com.sun.tools.internal.xjc.Driver", date = "2013-11-11T02:08:36-08:00", comments = "JAXB RI v2.2.4-2") @JsonProperty("entities") protected List<Object> fieldOrFormOrTable;
Serializing an object using standard Jack Jackson default serializers with metadata added will be as follows:
"entities": [ { "obj": "Table", "row": { "id": 1, "fields": [ { "id": "DEBUGARY", "type": "Text", "kind": "user" } ] }, "id": "DBGARRAY" }, { "obj": "field", "id": "IDBG", "type": "Text", "kind": "user" }, ..., ..., ...]