You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Dennis Kieselhorst <ma...@dekies.de> on 2014/09/16 18:52:06 UTC

Incomplete JSON to XML conversion by DOM4JProvider

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\":\"test2@test.de\",\"value\":{\"rev\":\"1-2a93d664b63033378e7896a2d89d9e3a\"},\"doc\":{\"_id\":\"test2@test.de\",\"_rev\":\"1-2a93d664b63033378e7896a2d89d9e3a\",\"email\":\"test2@test.de\"}},\n"
+
               
"{\"id\":\"test@test.de\",\"value\":{\"rev\":\"1-00340568a529de9f0af61a41ed111158\"},\"doc\":{\"_id\":\"test@test.de\",\"_rev\":\"1-00340568a529de9f0af61a41ed111158\",\"email\":\"test@test.de\"}}\n"
+
                "]}";
        String str = convertJsonToXml(json);
        // starts with the xml PI
        assertTrue(str.contains("test@test.de"));
    }

    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?

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.

Re: Incomplete JSON to XML conversion by DOM4JProvider

Posted by Dennis Kieselhorst <ma...@dekies.de>.
Hi Sergey,

thanks for your suggestions. In my view the current default behaviour should
be improved. Instead of silently producing incomplete XML I would expect an
exception or at least a warning.

Cheers
Dennis



--
View this message in context: http://cxf.547215.n5.nabble.com/Incomplete-JSON-to-XML-conversion-by-DOM4JProvider-tp5748793p5748829.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Incomplete JSON to XML conversion by DOM4JProvider

Posted by Sergey Beryozkin <sb...@gmail.com>.
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\":\"test2@test.de\",\"value\":{\"rev\":\"1-2a93d664b63033378e7896a2d89d9e3a\"},\"doc\":{\"_id\":\"test2@test.de\",\"_rev\":\"1-2a93d664b63033378e7896a2d89d9e3a\",\"email\":\"test2@test.de\"}},\n"
>>
>> +
>>
>> "{\"id\":\"test@test.de\",\"value\":{\"rev\":\"1-00340568a529de9f0af61a41ed111158\"},\"doc\":{\"_id\":\"test@test.de\",\"_rev\":\"1-00340568a529de9f0af61a41ed111158\",\"email\":\"test@test.de\"}}\n"
>>
>> +
>>                  "]}";
>>          String str = convertJsonToXml(json);
>>          // starts with the xml PI
>>          assertTrue(str.contains("test@test.de"));
>>      }
>>
>>      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

Re: Incomplete JSON to XML conversion by DOM4JProvider

Posted by Sergey Beryozkin <sb...@gmail.com>.
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\":\"test2@test.de\",\"value\":{\"rev\":\"1-2a93d664b63033378e7896a2d89d9e3a\"},\"doc\":{\"_id\":\"test2@test.de\",\"_rev\":\"1-2a93d664b63033378e7896a2d89d9e3a\",\"email\":\"test2@test.de\"}},\n"
> +
>
> "{\"id\":\"test@test.de\",\"value\":{\"rev\":\"1-00340568a529de9f0af61a41ed111158\"},\"doc\":{\"_id\":\"test@test.de\",\"_rev\":\"1-00340568a529de9f0af61a41ed111158\",\"email\":\"test@test.de\"}}\n"
> +
>                  "]}";
>          String str = convertJsonToXml(json);
>          // starts with the xml PI
>          assertTrue(str.contains("test@test.de"));
>      }
>
>      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.
>