Or alternatively use a JAX-RS inbound filter that will replace the
entity input stream with a sequence input stream adding say
'{"root":' and } to the top and the end of the original sequence
Sergey
On 16/09/14 22:34, Sergey Beryozkin wrote:
Hi
On 16/09/14 17:52, Dennis Kieselhorst wrote:
Hi,
I tried to convert a JSON response from CouchDB
(.../_all_docs?include_docs=true) to XML, unfortunately it's incomplete
(contains only <total_rows>2</total_rows>).
It's reproducible with DOM4JProviderTest:
@Test
public void testReadJSONArrayConvertToXML() throws Exception {
final String json =
"{\"total_rows\":2,\"offset\":0,\"rows\":[\n" +
"{\"id\":\"[email protected]\",\"value\":{\"rev\":\"1-2a93d664b63033378e7896a2d89d9e3a\"},\"doc\":{\"_id\":\"[email protected]\",\"_rev\":\"1-2a93d664b63033378e7896a2d89d9e3a\",\"email\":\"[email protected]\"}},\n"
+
"{\"id\":\"[email protected]\",\"value\":{\"rev\":\"1-00340568a529de9f0af61a41ed111158\"},\"doc\":{\"_id\":\"[email protected]\",\"_rev\":\"1-00340568a529de9f0af61a41ed111158\",\"email\":\"[email protected]\"}}\n"
+
"]}";
String str = convertJsonToXml(json);
// starts with the xml PI
assertTrue(str.contains("[email protected]"));
}
private String convertJsonToXml(final String xml) throws
IOException {
DOM4JProvider p = new DOM4JProvider();
p.setProviders(new ProvidersImpl(createMessage()));
org.dom4j.Document dom = p.readFrom(org.dom4j.Document.class,
org.dom4j.Document.class,
new Annotation[]{},
MediaType.APPLICATION_JSON_TYPE, new
MetadataMap<String, String>(),
new ByteArrayInputStream(xml.getBytes("UTF-8")));
return dom.asXML();
}
How can I fix this? Should I open a JIRA issue for it?
DOM4JProvider delegates to JSONProvider to create DOM from JSON and as
such the JSON sequence needs to have a root and in this case it is a
rootless sequence.
So to fix it, also register an instance of JSONProvider and set its
wrapperName property to whatever you prefer, say "root", and probably
its supportUnwrapped property to boolean.
HTH, Sergey
Cheers
Dennis
--
View this message in context:
http://cxf.547215.n5.nabble.com/Incomplete-JSON-to-XML-conversion-by-DOM4JProvider-tp5748793.html
Sent from the cxf-user mailing list archive at Nabble.com.
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com