cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1452288 - in /cxf/trunk/rt/rs/extensions/providers/src: main/java/org/apache/cxf/jaxrs/provider/json/ main/java/org/apache/cxf/jaxrs/provider/json/utils/ test/java/org/apache/cxf/jaxrs/provider/json/
Date Mon, 04 Mar 2013 13:12:49 GMT
Author: sergeyb
Date: Mon Mar  4 13:12:49 2013
New Revision: 1452288

URL: http://svn.apache.org/r1452288
Log:
[CXF-4872] Dropping a root element if index is equal to zero

Modified:
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/utils/JSONUtils.java
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java?rev=1452288&r1=1452287&r2=1452288&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
Mon Mar  4 13:12:49 2013
@@ -490,7 +490,7 @@ public class JSONProvider<T> extends Abs
     protected XMLStreamWriter createWriter(Object actualObject, Class<?> actualClass,

         Type genericType, String enc, OutputStream os, boolean isCollection) throws Exception
{
         
-        QName qname = getQName(actualClass, genericType, actualObject);
+        QName qname = actualClass == Document.class ? null : getQName(actualClass, genericType,
actualObject);
         if (ignoreNamespaces && (isCollection  || dropRootElement)) {        
             qname = new QName(qname.getLocalPart());
         }

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/utils/JSONUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/utils/JSONUtils.java?rev=1452288&r1=1452287&r2=1452288&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/utils/JSONUtils.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/utils/JSONUtils.java
Mon Mar  4 13:12:49 2013
@@ -90,14 +90,13 @@ public final class JSONUtils {
                 for (String key : arrayKeys) {
                     xsw.serializeAsArray(key);
                 }
-            } else {
+            } else if (qname != null) {
                 String key = getKey(convention, qname);
                 xsw.serializeAsArray(key);
             }
         }
         XMLStreamWriter writer = !writeXsiType || dropRootElement 
-            ? new IgnoreContentJettisonWriter(xsw, writeXsiType, 
-                                              dropRootElement ? qname : null) : xsw;
+            ? new IgnoreContentJettisonWriter(xsw, writeXsiType, dropRootElement) : xsw;
         
         return writer;
     }    
@@ -217,14 +216,15 @@ public final class JSONUtils {
     private static class IgnoreContentJettisonWriter extends DelegatingXMLStreamWriter {
         
         private boolean writeXsiType;
-        private QName ignoredQName;
+        private boolean dropRootElement;
         private boolean rootDropped;
         private int index; 
                 
-        public IgnoreContentJettisonWriter(XMLStreamWriter writer, boolean writeXsiType,
QName qname) {
+        public IgnoreContentJettisonWriter(XMLStreamWriter writer, boolean writeXsiType,

+                                           boolean dropRootElement) {
             super(writer);
             this.writeXsiType = writeXsiType;
-            ignoredQName = qname;
+            this.dropRootElement = dropRootElement;
         }
         
         public void writeAttribute(String prefix, String uri,
@@ -240,8 +240,7 @@ public final class JSONUtils {
         @Override
         public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException
{
             index++;
-            if (ignoredQName != null && ignoredQName.getLocalPart().equals(local)

-                && ignoredQName.getNamespaceURI().equals(uri)) {
+            if (dropRootElement && index - 1 == 0) {
                 rootDropped = true;
                 return;
             }

Modified: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java?rev=1452288&r1=1452287&r2=1452288&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
Mon Mar  4 13:12:49 2013
@@ -446,6 +446,21 @@ public class JSONProviderTest extends As
     }
     
     @Test
+    public void testDropRootElementFromDocument() throws Exception {
+        JSONProvider<Document> p = new JSONProvider<Document>();
+        Document doc = DOMUtils.readXml(new StringReader("<a><b>2</b></a>"));
+        p.setDropRootElement(true);
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        
+        p.writeTo(doc, Document.class, Document.class, new Annotation[]{}, 
+                  MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(),
os);
+        
+        String s = os.toString();
+        assertEquals("{\"b\":2}", s);
+        
+    }
+    
+    @Test
     public void testWriteQualifiedCollection() throws Exception {
         String data = "{\"ns1.tag\":[{\"group\":\"b\",\"name\":\"a\"}"
             + ",{\"group\":\"d\",\"name\":\"c\"}]}";
@@ -950,7 +965,7 @@ public class JSONProviderTest extends As
             new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(),
bos);
         String expected = 
             "{\"tagholders\":["
-            + "{\"tagholder\":{\"attr\":\"attribute\",\"thetag\":{\"group\":\"B\",\"name\":\"A\"}}}"
+            + "{\"attr\":\"attribute\",\"thetag\":{\"group\":\"B\",\"name\":\"A\"}}"
             + "]}";
         assertEquals(expected, bos.toString());
     }



Mime
View raw message