In my Jersey project, I am using MOXy to marshal to/from JSON. One of the classes I want to to marshal is an array of strings that could be empty.
class Data
{
@XmlElement(nillable = true) public String[] array;
}
In the case where the array is empty, I would like the output to be:
{
"array" : []
}
however it looks like MOXy is filtering out empty arrays from the output. How can I get it to include empty arrays in the output?
I was thinking about adding JSONConfiguration.mapped().array(“array”).build() to the MOXy provider constructor, but this doesn’t seem to make a difference (and I’m not even sure that it’s the correct solution).
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
ORIGINAL ANSWER
The following enhancement request has been entered. You can use the link below to track our progress on this issue.
UPDATE
Starting with the May 19, 2012 EclipseLink 2.4.0 label you can set the following property to get the behaviour you are looking for.
You can download nightly EclipseLink labels from the following location:
Root
On the following class we have three
Listproperties. Two are of theListobjects are empty and one is null. Note that theemptyChoiceListfield is mapped with@XmlElements. The@XmlElementsannotation states that the possible node names arefooandbar.Demo
The following code demonstrates how to specify the
MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONSproperty.Output
Now in the output the empty lists are marshalled as empty arrays. For the field that was mapped with
@XmlElememtsthe node name that was specified was used in the JSON representation.