Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON marshaling of object arrays with JAXB
    primarykey
    data
    text
    <p>Using MOXy I'm trying to marshal a java class like this to JSON:</p> <pre class="lang-java prettyprint-override"><code>@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Request { String method; @XmlAnyElement(lax=true) Object[] arguments; } </code></pre> <p>I would expect something like:</p> <pre class="lang-json prettyprint-override"><code>{ "method": "test", "arguments": ["a", "b"] } </code></pre> <p>but the JSON output results to:</p> <pre class="lang-json prettyprint-override"><code>{ "method": "test", "value": ["a", "b"] } </code></pre> <p>Where is the <code>value</code> coming from? </p> <p>If I put a <code>@XmlElementWrapper</code> over the arguments field, it gets even worse:</p> <pre class="lang-json prettyprint-override"><code>{ "method":"test", "arguments":"a""value":["b"] } </code></pre> <p>My JUnit <code>TestCase</code> looks like this:</p> <pre class="lang-java prettyprint-override"><code>import static org.junit.Assert.assertEquals; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import javax.xml.bind.*; import javax.xml.bind.annotation.*; import org.junit.Test; public class JsonRequestTest { @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public static class Request { String method; @XmlAnyElement(lax=true) Object[] arguments; } // InvocationRequest @Test public void testObjectArray() throws JAXBException { System.setProperty(JAXBContext.class.getName(), "org.eclipse.persistence.jaxb.JAXBContextFactory"); Map&lt;String, Object&gt; props= new HashMap&lt;String, Object&gt;(); props.put("eclipselink.media-type", "application/json"); props.put("eclipselink.json.include-root", false); JAXBContext ctx = JAXBContext.newInstance(new Class&lt;?&gt;[]{Request.class},props); Marshaller m = ctx.createMarshaller(); StringWriter writer = new StringWriter(); Request req = new Request(); req.method="test"; req.arguments = new Object[]{"a","b"}; m.marshal(req, writer); assertEquals("{\"method\":\"test\", \"arguments\":[\"a\",\"b\"]}", writer.toString()); } } // class JsonRequestTest </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload