CAMEL-8389: camel-jackson - Allow to configure jackson object mapper from XML 
DSL


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/654e0390
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/654e0390
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/654e0390

Branch: refs/heads/master
Commit: 654e03900a513f706fa82060d339926c83cc1ffd
Parents: 2777d9b
Author: Claus Ibsen <davscl...@apache.org>
Authored: Sun Feb 22 09:50:11 2015 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun Feb 22 09:50:11 2015 +0100

----------------------------------------------------------------------
 .../camel/model/dataformat/JsonDataFormat.java  | 22 ++++----
 .../component/jackson/JacksonDataFormat.java    | 53 ++++++++++++--------
 .../jackson/SpringJacksonEnableFeatureTest.java | 44 ++++++++++++++++
 .../jackson/SpringJacksonEnableFeatureTest.xml  | 39 ++++++++++++++
 4 files changed, 126 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/654e0390/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java
 
b/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java
index 30f9fe0..f645998 100644
--- 
a/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java
+++ 
b/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java
@@ -16,11 +16,9 @@
  */
 package org.apache.camel.model.dataformat;
 
-import java.util.Set;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
-import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
@@ -66,10 +64,10 @@ public class JsonDataFormat extends DataFormatDefinition {
     private String moduleClassNames;
     @XmlAttribute
     private String moduleRefs;
-    @XmlElement
-    private Set<String> enableFeatures;
-    @XmlElement
-    private Set<String> disableFeatures;
+    @XmlAttribute
+    private String enableFeatures;
+    @XmlAttribute
+    private String disableFeatures;
 
     public JsonDataFormat() {
         super("json");
@@ -223,7 +221,7 @@ public class JsonDataFormat extends DataFormatDefinition {
         this.moduleRefs = moduleRefs;
     }
 
-    public Set<String> getEnableFeatures() {
+    public String getEnableFeatures() {
         return enableFeatures;
     }
 
@@ -232,12 +230,14 @@ public class JsonDataFormat extends DataFormatDefinition {
      * <p/>
      * The features should be a name that matches a enum from 
<tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
      * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or 
<tt>com.fasterxml.jackson.databind.MapperFeature</tt>
+     * <p/>
+     * Multiple features can be separated by comma
      */
-    public void setEnableFeatures(Set<String> enableFeatures) {
+    public void setEnableFeatures(String enableFeatures) {
         this.enableFeatures = enableFeatures;
     }
 
-    public Set<String> getDisableFeatures() {
+    public String getDisableFeatures() {
         return disableFeatures;
     }
 
@@ -246,8 +246,10 @@ public class JsonDataFormat extends DataFormatDefinition {
      * <p/>
      * The features should be a name that matches a enum from 
<tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
      * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or 
<tt>com.fasterxml.jackson.databind.MapperFeature</tt>
+     * <p/>
+     * Multiple features can be separated by comma
      */
-    public void setDisableFeatures(Set<String> disableFeatures) {
+    public void setDisableFeatures(String disableFeatures) {
         this.disableFeatures = disableFeatures;
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/654e0390/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
 
b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
index ef1016f..c857a81 100644
--- 
a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
+++ 
b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
@@ -21,10 +21,9 @@ import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -65,8 +64,8 @@ public class JacksonDataFormat extends ServiceSupport 
implements DataFormat, Cam
     private boolean allowJmsType;
     private boolean useList;
     private boolean enableJaxbAnnotationModule;
-    private Set<String> enableFeatures;
-    private Set<String> disableFeatures;
+    private String enableFeatures;
+    private String disableFeatures;
 
     /**
      * Use the default Jackson {@link ObjectMapper} and {@link Map}
@@ -294,7 +293,7 @@ public class JacksonDataFormat extends ServiceSupport 
implements DataFormat, Cam
         this.allowJmsType = allowJmsType;
     }
 
-    public Set<String> getEnableFeatures() {
+    public String getEnableFeatures() {
         return enableFeatures;
     }
 
@@ -302,11 +301,11 @@ public class JacksonDataFormat extends ServiceSupport 
implements DataFormat, Cam
      * Set of features to enable on the Jackson {@link ObjectMapper}.
      * The features should be a name that matches a enum from {@link 
SerializationFeature}, {@link DeserializationFeature}, or {@link MapperFeature}.
      */
-    public void setEnableFeatures(Set<String> enableFeatures) {
+    public void setEnableFeatures(String enableFeatures) {
         this.enableFeatures = enableFeatures;
     }
 
-    public Set<String> getDisableFeatures() {
+    public String getDisableFeatures() {
         return disableFeatures;
     }
 
@@ -314,50 +313,56 @@ public class JacksonDataFormat extends ServiceSupport 
implements DataFormat, Cam
      * Set of features to disable on the Jackson {@link ObjectMapper}.
      * The features should be a name that matches a enum from {@link 
SerializationFeature}, {@link DeserializationFeature}, or {@link MapperFeature}.
      */
-    public void setDisableFeatures(Set<String> disableFeatures) {
+    public void setDisableFeatures(String disableFeatures) {
         this.disableFeatures = disableFeatures;
     }
 
     public void enableFeature(SerializationFeature feature) {
         if (enableFeatures == null) {
-            enableFeatures = new HashSet<String>();
+            enableFeatures = feature.name();
+        } else {
+            enableFeatures += "," + feature.name();
         }
-        enableFeatures.add(feature.name());
     }
 
     public void enableFeature(DeserializationFeature feature) {
         if (enableFeatures == null) {
-            enableFeatures = new HashSet<String>();
+            enableFeatures = feature.name();
+        } else {
+            enableFeatures += "," + feature.name();
         }
-        enableFeatures.add(feature.name());
     }
 
     public void enableFeature(MapperFeature feature) {
         if (enableFeatures == null) {
-            enableFeatures = new HashSet<String>();
+            enableFeatures = feature.name();
+        } else {
+            enableFeatures += "," + feature.name();
         }
-        enableFeatures.add(feature.name());
     }
 
     public void disableFeature(SerializationFeature feature) {
         if (disableFeatures == null) {
-            disableFeatures = new HashSet<String>();
+            disableFeatures = feature.name();
+        } else {
+            disableFeatures += "," + feature.name();
         }
-        disableFeatures.add(feature.name());
     }
 
     public void disableFeature(DeserializationFeature feature) {
         if (disableFeatures == null) {
-            disableFeatures = new HashSet<String>();
+            disableFeatures = feature.name();
+        } else {
+            disableFeatures += "," + feature.name();
         }
-        disableFeatures.add(feature.name());
     }
 
     public void disableFeature(MapperFeature feature) {
         if (disableFeatures == null) {
-            disableFeatures = new HashSet<String>();
+            disableFeatures = feature.name();
+        } else {
+            disableFeatures += "," + feature.name();
         }
-        disableFeatures.add(feature.name());
     }
 
     @Override
@@ -382,7 +387,9 @@ public class JacksonDataFormat extends ServiceSupport 
implements DataFormat, Cam
         }
 
         if (enableFeatures != null) {
-            for (String enable : enableFeatures) {
+            Iterator<Object> it = ObjectHelper.createIterator(enableFeatures);
+            while (it.hasNext()) {
+                String enable = it.next().toString();
                 // it can be different kind
                 SerializationFeature sf = 
getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, 
enable);
                 if (sf != null) {
@@ -403,7 +410,9 @@ public class JacksonDataFormat extends ServiceSupport 
implements DataFormat, Cam
             }
         }
         if (disableFeatures != null) {
-            for (String disable : disableFeatures) {
+            Iterator<Object> it = ObjectHelper.createIterator(enableFeatures);
+            while (it.hasNext()) {
+                String disable = it.next().toString();
                 // it can be different kind
                 SerializationFeature sf = 
getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, 
disable);
                 if (sf != null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/654e0390/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.java
 
b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.java
new file mode 100644
index 0000000..dd4f997
--- /dev/null
+++ 
b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.java
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jackson;
+
+import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version 
+ */
+public class SpringJacksonEnableFeatureTest extends CamelSpringTestSupport {
+
+    @Test
+    public void testMarshal() throws Exception {
+        TestPojoView in = new TestPojoView();
+
+        Object marshalled = template.requestBody("direct:in", in);
+        String marshalledAsString = 
context.getTypeConverter().convertTo(String.class, marshalled);
+        // we enable the wrap root type feature so we should have TestPojoView
+        
assertEquals("{\"TestPojoView\":{\"age\":30,\"height\":190,\"weight\":70}}", 
marshalledAsString);
+    }
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new 
ClassPathXmlApplicationContext("org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.xml");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/654e0390/components/camel-jackson/src/test/resources/org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.xml
----------------------------------------------------------------------
diff --git 
a/components/camel-jackson/src/test/resources/org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.xml
 
b/components/camel-jackson/src/test/resources/org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.xml
new file mode 100644
index 0000000..dc64e41
--- /dev/null
+++ 
b/components/camel-jackson/src/test/resources/org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring";>
+
+    <dataFormats>
+      <json id="jack" library="Jackson" 
unmarshalTypeName="org.apache.camel.component.jackson.TestPojo"
+            enableFeatures="WRAP_ROOT_VALUE"/>
+    </dataFormats>
+
+    <route>
+      <from uri="direct:in"/>
+      <marshal ref="jack"/>
+    </route>
+
+  </camelContext>
+
+</beans>

Reply via email to