I'm using Jersey 1.0 and I have a problem with the way that arrays are output in JSON. I was wondering if this is normal, or perhaps Jersey is misreading my JAXB beans?
I have XML data that looks like this:
<Contexts>
<Context><name>One</name></Context>
<Context<name>Two</name></Context>
<Contexts>
Since the value of Contexts is an array of Context, I would presume that the JSON would look like this:
{"Contexts":[{"Context":{"Name":"One"}}, {"Context":{"Name":"Two"}}]}
However, the JSON actually looks like this:
{"Contexts":{"Context":[{"Name":"One"},{"Name":"Two"}]}}
Seems to me the (incorrect) XML translation of this JSON would be:
<Contexts>
<Context>
<Name>One</Name>
<Name>Two</Name>
</Context>
<Contexts>
My JAXB Beans, which produce the correct XML are:
@XmlRootElement(name = "Contexts")
@XmlAccessorType(XmlAccessType.FIELD)
public class Contexts {
@XmlElement(name = "Context")
protected List<ContextBean> context;
public List<ContextBean> getContext() {
if (context == null) {
context = new ArrayList<ContextBean>();
}
return context;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Context")
public class ContextBean {
@XmlElement(name = "Name")
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Is the JSON output actually correct, or could Jersey be misreading my "@XmlElement" tag for the Context array?
If everything is behaving as expected, then that poses another problem for me. Using the above example, I have to pass in "Context" to the JSON_ARRAYS property in order to wrap single-element arrays. There are situations in my larger XML schema where "Context" can appear by itself (not in a List), and I do not want it to be wrapped. I would like to be able to send in "Contexts" to be wrapped as an array, but not "Context". Is this possible?
Thanks,
Clark