I am marshalling an object to json using Moxy 2.6.0-M3 (and have the same issue with 2.5.1). The object extends a generic class. I don't want the type element output, so I tried following the example in Blaise Doughan's article called "Ignoring Inheritance with @XmlTransient" (sorry, can't link to it because it's not on eclipse.org.)
That procedure works if the class marked @XmlTransient is not generic, but if it is generic, the type element is always output.
Here's an admittedly artificial example:
public class AB {
@XmlElement
String a = "A";
@XmlElement
String b = "B";
}
@XmlTransient
public class Value {
@XmlElement
@XmlPath(".")
AB value = new AB();
}
@XmlRootElement
public class Record extends Value {
@XmlElement
int id = 1;
}
I marshal a record using the following code:
JAXBContext jaxbContext =
org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(
new Class[]{ Record.class, AB.class}, null);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
Record record = new Record();
marshaller.marshal(record, System.out);
and get the following output:
Now if I try the following:
@XmlTransient
public class ValueGeneric<T> {
@XmlElement
@XmlPath(".")
T value;
}
@XmlRootElement
public class RecordAB extends ValueGeneric<AB> {
@XmlElement
int id = 1;
}
with this marshalling code (basically the same as above but registering RecordAB.class):
JAXBContext jaxbContext =
org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(
new Class[]{RecordAB.class, AB.class}, null);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
RecordAB recordAB = new RecordAB();
recordAB.value = new AB();
marshaller.marshal(recordAB, System.out);
it marshals as:
{"type":"ab","a":"A","b":"B","id":1}
I want it to marshal like the first one, without the type element.
I don't need to unmarshal, so it's OK if the type information gets lost.
Something similar happens if I output XML; the root element for RecordAB has the xsi:type attribute set to "ab".
Anyone have an idea of how to prevent the type element from being output?