cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1526445 - in /cxf/trunk/rt/rs/extensions/providers/src: main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
Date Thu, 26 Sep 2013 11:32:24 GMT
Author: sergeyb
Date: Thu Sep 26 11:32:24 2013
New Revision: 1526445

URL: http://svn.apache.org/r1526445
Log:
[CXF-5301] Adding the name of the root to existing arrayKeys if root if array is needed and
the root is dropped in JSON

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/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=1526445&r1=1526444&r2=1526445&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
Thu Sep 26 11:32:24 2013
@@ -32,9 +32,9 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Enumeration;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -60,7 +60,6 @@ import javax.xml.stream.XMLStreamWriter;
 
 import org.w3c.dom.Document;
 
-import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.jaxrs.ext.MessageContext;
@@ -525,12 +524,20 @@ public class JSONProvider<T> extends Abs
         if (!writeNullAsString) {
             config.setWriteNullAsString(writeNullAsString);
         }
-        if (dropRootElement && !dropElementsInXmlStream) {
+        
+        boolean dropRootInJsonStream = dropRootElement && !dropElementsInXmlStream;
+        if (dropRootInJsonStream) {
             config.setDropRootElement(true);
         }
-        if (ignoreNamespaces && serializeAsArray && arrayKeys == null) {
-            arrayKeys = CastUtils.cast((List<?>)Collections.singletonList(qname.getLocalPart()));
+        if (ignoreNamespaces && serializeAsArray && (arrayKeys == null ||
dropRootInJsonStream)) {
+            if (arrayKeys == null) {
+                arrayKeys = new LinkedList<String>();
+            } else if (dropRootInJsonStream) {
+                arrayKeys = new LinkedList<String>(arrayKeys);
+            }
+            arrayKeys.add(qname.getLocalPart());
         }
+        
         XMLStreamWriter writer = JSONUtils.createStreamWriter(os, qname, 
              writeXsiType && !ignoreNamespaces, config, serializeAsArray, arrayKeys,
              isCollection || dropRootNeeded);

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=1526445&r1=1526444&r2=1526445&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
Thu Sep 26 11:32:24 2013
@@ -29,6 +29,7 @@ import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -247,6 +248,30 @@ public class JSONProviderTest extends As
     }
     
     @Test
+    public void testWriteCollectionAsPureArray3() throws Exception {
+        
+        JSONProvider<ReportDefinition> provider 
+            = new JSONProvider<ReportDefinition>();
+        provider.setIgnoreNamespaces(true);
+        provider.setSerializeAsArray(true);
+        provider.setArrayKeys(Arrays.asList("parameterList".split(" ")));
+        provider.setDropRootElement(true);
+        provider.setDropElementsInXmlStream(false);
+        ReportDefinition r = new ReportDefinition();
+        r.setReportName("report");
+        ParameterDefinition paramDef = new ParameterDefinition("param");
+        r.addParameterDefinition(paramDef);
+        
+        Method m = ReportService.class.getMethod("findReport", new Class<?>[]{});
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(r, m.getReturnType(), m.getGenericReturnType(),
+                         new Annotation[0], MediaType.APPLICATION_JSON_TYPE, 
+                         new MetadataMap<String, Object>(), bos);
+        assertTrue(bos.toString().startsWith("[{\"parameterList\":["));
+        
+    }
+    
+    @Test
     public void testWriteBeanNoRootAtJsonLevel() throws Exception {
         JSONProvider<Book> provider = new JSONProvider<Book>();
         provider.setDropRootElement(true);



Mime
View raw message