I have a problem when using an array of interfaces in a class that I want to marshal/unmarshal to/from JSON.
Here's some example code:
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import org.eclipse.persistence.jaxb.JAXBContext;
public class TestMain {
public static interface Test {
String getVal();
}
public static final class TestImpl implements Test {
@Override
@XmlValue
public String getVal() {
return "HELLO";
}
}
@XmlRootElement(name = "TestJSON")
@XmlAccessorType(XmlAccessType.NONE)
public static final class TestJSON {
@XmlElement(name = "Selection", type = TestImpl.class)
public Test[] getTestInt() {
return new TestImpl[] {
new TestImpl(), new TestImpl()
};
}
}
public static void main(String[] args) throws Exception {
Marshaller m = JAXBContext.newInstance(TestJSON.class).createMarshaller();
m.setProperty("eclipselink.media-type", "application/json");
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(new TestJSON(), System.out);
}
}
Here's the output I get:
{
"TestJSON" : {
"Selection" : [ "test.TestMain$TestImpl@49fc609f", "test.TestMain$TestImpl@cd2dae5" ]
}
}
And here's what I would expect and which I can get if I use a List<Test>
instead of an array:
{
"TestJSON" : {
"Selection" : [ "HELLO", "HELLO" ]
}
}
Is this something MOXy can't handle, or am I missing something?
I using version 2.5.1