JAXB i dziedziczenie

Próbuję odczytać plik JSON, taki jak:

{
  "a": "abc",
  "data" : {
      "type" : 1,
      ...
  }
}

gdzie ... część jest wymienna w zależności od typu:

{
  "a": "abc",
  "data" : {
      "type" : 1,
      "b" : "bcd"
  }
}

lub:

{
  "a": "abc",
  "data" : {
      "type" : 2,
      "c" : "cde",
      "d" : "def",
  }
}

Przez całe życie nie potrafię znaleźć właściwych adnotacji / klas JAXB, których można użyć, aby to się stało. W razie potrzeby nie mam problemu z przenoszeniem zmiennej typu poza blok danych.

Używam Glassfish 3.1.2.2.

Edytować:

Bazując na kodzie dostarczonym przez Perception, zrobiłem szybką próbę ... ale nie działa w szklanych rybach:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes(
{
    @JsonSubTypes.Type(value = DataSubA.class, name = "1"),
    @JsonSubTypes.Type(value = DataSubB.class, name = "2") 
})
@XmlRootElement
public abstract class Data implements Serializable 
{
    private static final long serialVersionUID = 1L;

    public Data() 
    {
        super();
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class DataSubA 
    extends Data 
{
    private static final long serialVersionUID = 1L;

    @XmlElement
    private BigDecimal expenditure;

    public DataSubA() {
        super();
    }

    public DataSubA(final BigDecimal expenditure) {
        super();
        this.expenditure = expenditure;
    }

    @Override
    public String toString() {
        return String.format("%s[expenditure = %s]\n", 
                             getClass().getSimpleName(), getExpenditure());
    }

    public BigDecimal getExpenditure() {
        return expenditure;
    }

    public void setExpenditure(BigDecimal expenditure) {
        this.expenditure = expenditure;
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class DataSubB 
    extends Data 
{
    private static final long serialVersionUID = 1L;

    @XmlElement
    private String name;

    @XmlElement
    private Integer age;

    public DataSubB() 
    {
        super();
    }

    public DataSubB(final String name, final Integer age) 
    {
        super();
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() 
    {
        return String.format("%s[name = %s, age = %s]\n", 
                             getClass().getSimpleName(), getName(), getAge());
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class DataWrapper
{ 
    @XmlElement
    private Data data;

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }
}

I prosty POST, który zabiera go:

@Stateless
@Path("x")
public class Endpoint
{
    @POST
    @Consumes(
    {
        MediaType.APPLICATION_JSON,
    })
    @Produces(
    {
        MediaType.APPLICATION_JSON,
    })
    public String foo(final DataWrapper wrapper)
    {
        return ("yay");
    }
}

Kiedy przechodzę w JSON, jak:

{
    "data" : 
    {
        "type" : 1,
        "expenditure" : 1
    }
}

Dostaję wiadomość typu:

Can not construct instance of Data, problem: abstract types can only be instantiated with additional type information
 at [Source: org.apache.catalina.connector.CoyoteInputStream@28b92ec1; line: 2, column: 5] (through reference chain: DataWrapper["data"])

questionAnswers(1)

yourAnswerToTheQuestion