I use this to convert a JAXB bean to JSON code:
private String marshall(final Book beanObject) throws Exception
{
JAXBContext context = JAXBContext.newInstance(Book.class);
Marshaller marshaller = context.createMarshaller();
Configuration config = new Configuration();
MappedNamespaceConvention con = new MappedNamespaceConvention(config);
StringWriter jsonDocument = new StringWriter();
XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, jsonDocument);
marshaller.marshal(beanObject, xmlStreamWriter);
return jsonDocument.toString();
}
For my Book class, the output is:
{"bookType":{"chapters":["Genesis","Exodus"],"name":"The Bible","pages":600}}
However, I want the output to be compatible with Jersey:
{"chapters":["Genesis","Exodus"],"name":"The Bible","pages":600}
How can I archive the second JSON notation with above code and get rid of the root element?
My solution:
Switched to Jackson now, there you can set an option for root unwrapping. I'm still interested in Jettison solutions, though, if there are any.