cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1468837 - /cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
Date Wed, 17 Apr 2013 11:14:07 GMT
Author: sergeyb
Date: Wed Apr 17 11:14:07 2013
New Revision: 1468837

URL: http://svn.apache.org/r1468837
Log:
[CXF-4971] Updating JSONProvider to check a context property too when deciding whether to
keep the root or not

Modified:
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.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=1468837&r1=1468836&r2=1468837&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
Wed Apr 17 11:14:07 2013
@@ -85,7 +85,7 @@ public class JSONProvider<T> extends Abs
     
     private static final String MAPPED_CONVENTION = "mapped";
     private static final String BADGER_FISH_CONVENTION = "badgerfish";
-    
+    private static final String DROP_ROOT_CONTEXT_PROPERTY = "drop.json.root.element";
     static {
         new SimpleConverter();
     }
@@ -490,14 +490,17 @@ 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 = actualClass == Document.class ? null : getQName(actualClass, genericType,
actualObject);
-        if (qname != null && ignoreNamespaces && (isCollection  || dropRootElement))
{        
-            qname = new QName(qname.getLocalPart());
-        }
         if (BADGER_FISH_CONVENTION.equals(convention)) {
             return JSONUtils.createBadgerFishWriter(os);
         }
         
+        boolean dropRootNeeded = isDropRootNeeded();
+        
+        QName qname = actualClass == Document.class ? null : getQName(actualClass, genericType,
actualObject);
+        if (qname != null && ignoreNamespaces && (isCollection  || dropRootNeeded))
{        
+            qname = new QName(qname.getLocalPart());
+        }
+        
         Configuration config = 
             JSONUtils.createConfiguration(namespaceMap, 
                                           writeXsiType && !ignoreNamespaces,
@@ -506,12 +509,24 @@ public class JSONProvider<T> extends Abs
         
         XMLStreamWriter writer = JSONUtils.createStreamWriter(os, qname, 
              writeXsiType && !ignoreNamespaces, config, serializeAsArray, arrayKeys,
-             isCollection || dropRootElement);
+             isCollection || dropRootNeeded);
         writer = JSONUtils.createIgnoreMixedContentWriterIfNeeded(writer, ignoreMixedContent);
         writer = JSONUtils.createIgnoreNsWriterIfNeeded(writer, ignoreNamespaces);
         return createTransformWriterIfNeeded(writer, os);
     }
     
+    protected boolean isDropRootNeeded() {
+        MessageContext mc = getContext();
+        if (mc != null) {
+            Object prop = mc.get(DROP_ROOT_CONTEXT_PROPERTY);
+            if (prop != null) {
+                // means the property has been set explicitly
+                return MessageUtils.isTrue(prop);
+            }
+        }
+        return dropRootElement;
+    }
+    
     protected void marshal(Object actualObject, Class<?> actualClass, 
                            Type genericType, String enc, OutputStream os) throws Exception
{
         



Mime
View raw message