You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2013/04/17 13:18:37 UTC
svn commit: r1468840 - in /cxf/branches/2.7.x-fixes: ./
rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/
systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Author: sergeyb
Date: Wed Apr 17 11:18:37 2013
New Revision: 1468840
URL: http://svn.apache.org/r1468840
Log:
Merged revisions 1468837,1468839 via svnmerge from
https://svn.apache.org/repos/asf/cxf/trunk
........
r1468837 | sergeyb | 2013-04-17 12:14:07 +0100 (Wed, 17 Apr 2013) | 1 line
[CXF-4971] Updating JSONProvider to check a context property too when deciding whether to keep the root or not
........
r1468839 | sergeyb | 2013-04-17 12:14:43 +0100 (Wed, 17 Apr 2013) | 1 line
[CXF-4971] The system test
........
Modified:
cxf/branches/2.7.x-fixes/ (props changed)
cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
svn:mergeinfo = /cxf/trunk:1468837-1468839
Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified: cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java?rev=1468840&r1=1468839&r2=1468840&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java (original)
+++ cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java Wed Apr 17 11:18:37 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 {
Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1468840&r1=1468839&r2=1468840&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Wed Apr 17 11:18:37 2013
@@ -142,6 +142,14 @@ public class BookStore {
}
@GET
+ @Path("/dropjsonroot")
+ @Produces("application/json")
+ public Book getBookDropJsonRoot(@Context MessageContext mc) throws BookNotFoundFault {
+ mc.put("drop.json.root.element", "true");
+ return doGetBook("123");
+ }
+
+ @GET
@Path("/booknames/123")
@Produces("application/bar")
public byte[] getBookName123() {
Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1468840&r1=1468839&r2=1468840&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java (original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Wed Apr 17 11:18:37 2013
@@ -1310,6 +1310,15 @@ public class JAXRSClientServerBookTest e
}
@Test
+ public void testDropJSONRootDynamically() {
+ WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/dropjsonroot");
+ wc.accept("application/json");
+ String response = wc.get(String.class);
+ // with root: {"Book":{"id":123,"name":"CXF in Action"}}
+ assertEquals("{\"id\":123,\"name\":\"CXF in Action\"}", response);
+ }
+
+ @Test
public void testFormattedJSON() {
WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/books/123");
wc.accept("application/json");