With the simplified example below:
I get the following, as expected:
{"person":{"name":"john","tags":["tag1","tag2"]}}
However, if I only set one tag, I get this:
{"person":{"name":"john","tags":"tag1"}}
And I was expecting to get this:
{"person":{"name":"john","tags":["tag1"]}}
That is, jettison has removed the array for tags, because there is only one element in the array.
I think this is pretty unsafe.
How to force jettison to write an array, even if there is only one element?
Note: I am aware that there are other alternatives to jettison, such as StAXON. However, here I am asking how to achieve this using Jettison. Please do not suggest another alternative to jettison.
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.*;
import java.io.*;
import javax.xml.bind.*;
import javax.xml.stream.XMLStreamWriter;
import org.codehaus.jettison.mapped.*;
public class JettisonTest {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Person.class);
Person person = new Person();
person.name = "john";
person.tags.add("tag1");
person.tags.add("tag2");
Configuration config = new Configuration();
MappedNamespaceConvention con = new MappedNamespaceConvention(config);
Writer writer = new OutputStreamWriter(System.out);
XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(person, xmlStreamWriter);
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Person {
String name;
List<String> tags = new ArrayList<String>();
}