I read a lot of Blaise Doughan StackOverflow answers and blog posts, and it seemed to me that I understood his examples of using @XmlAnyElement with Object [].
But these same principles do not work with the list - as shown in the example below (partially copied from one of its examples and partially copied from xjc output).
I believe the code below should create JSON:
{"method":"test","status":["value":"500"]}
but instead, it creates JASON:
{"method":"test","value":[" [email protected] "]}
which is not very useful to me.
Can someone help me set up this small object correctly?
package com.mdsh.test; import static org.junit.Assert.assertEquals; import java.io.StringWriter; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Vector; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAnyElement; import javax.xml.bind.annotation.XmlRootElement; import org.eclipse.persistence.jaxb.MarshallerProperties; import org.junit.Test; import org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy; import org.jvnet.jaxb2_commons.lang.ToStringStrategy; import org.jvnet.jaxb2_commons.locator.ObjectLocator; public class JsonRequestTest { @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public static class Request { String method; @XmlAnyElement(lax = true) protected List<Object> any = new Vector<Object>(); public String getMethod() { return this.method; } public void setMethod(final String value) { this.method = value; } public List<Object> getAny() { if (this.any == null) { this.any = new Vector<Object>(); } return this.any; } @Override public String toString() { final ToStringStrategy strategy = JAXBToStringStrategy.INSTANCE; final StringBuilder buffer = new StringBuilder(); append(null, buffer, strategy); return buffer.toString(); } public StringBuilder append(final ObjectLocator locator, final StringBuilder buffer, final ToStringStrategy strategy) { strategy.appendStart(locator, this, buffer); appendFields(locator, buffer, strategy); strategy.appendEnd(locator, this, buffer); return buffer; } public StringBuilder appendFields(final ObjectLocator locator, final StringBuilder buffer, final ToStringStrategy strategy) { { List<Object> theAny; theAny = (((this.any!= null)&&(!this.any.isEmpty()))?getAny():null); strategy.appendField(locator, this, "any", buffer, theAny); } return buffer; } } public static class Status { int value; public int getValue() { return this.value; } public void setValue(final int value) { this.value = value; } } @Test public void testListOfObjects() throws JAXBException { System.setProperty(JAXBContext.class.getName(), "org.eclipse.persistence.jaxb.JAXBContextFactory"); final Map<String, Object> props = new HashMap<String, Object>(); props.put(MarshallerProperties.MEDIA_TYPE, "application/json"); props.put(MarshallerProperties.JSON_INCLUDE_ROOT, false); final JAXBContext ctx = JAXBContext.newInstance( new Class<?>[] { Request.class }, props); final Marshaller m = ctx.createMarshaller(); final StringWriter writer = new StringWriter(); final Request req = new Request(); req.setMethod("test"); final Status stat = new Status(); stat.setValue(500); req.getAny().add(stat); m.marshal(req, writer); assertEquals("{\"method\":\"test\",\"status\":[\"value\":\"500\"]}", writer.toString()); } }