camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/2] camel git commit: CAMEL-9583: camel-jackson - Allow the FallbackTypeConverter to convert to/from string types
Date Wed, 10 Feb 2016 08:50:28 GMT
Repository: camel
Updated Branches:
  refs/heads/master 2c78e2f30 -> 2b4b9df67


CAMEL-9583: camel-jackson - Allow the FallbackTypeConverter to convert to/from string types


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

Branch: refs/heads/master
Commit: 7009d64091876c3b1f9e36e074f8cc7484cfb434
Parents: 2c78e2f
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Feb 10 08:16:35 2016 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Feb 10 08:16:35 2016 +0100

----------------------------------------------------------------------
 .../component/jackson/JacksonConstants.java     |  1 +
 .../converter/JacksonTypeConverters.java        | 36 ++++++++++++++++----
 2 files changed, 30 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7009d640/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java
b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java
index 1429dc0..d4f5c26 100644
--- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java
+++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonConstants.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.jackson;
 public final class JacksonConstants {
 
     public static final String ENABLE_TYPE_CONVERTER = "CamelJacksonEnableTypeConverter";
+    public static final String TYPE_CONVERTER_POJO_ONLY = "CamelJacksonTypeConverterPojoOnly";
 
     public static final String UNMARSHAL_TYPE = "CamelJacksonUnmarshalType";
 

http://git-wip-us.apache.org/repos/asf/camel/blob/7009d640/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
index 5cb2451..110b6ce 100644
--- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
+++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
@@ -16,10 +16,10 @@
  */
 package org.apache.camel.component.jackson.converter;
 
-import java.util.Map;
 import java.util.Set;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
 import org.apache.camel.Exchange;
 import org.apache.camel.FallbackConverter;
 import org.apache.camel.component.jackson.JacksonConstants;
@@ -28,21 +28,33 @@ import org.apache.camel.spi.TypeConverterRegistry;
 
 public final class JacksonTypeConverters {
 
-    private final ObjectMapper defaultMapper = new ObjectMapper();
+    private final ObjectMapper defaultMapper;
     private boolean init;
     private Boolean enabled;
+    private boolean pojoOnly = true;
 
     public JacksonTypeConverters() {
+        defaultMapper = new ObjectMapper();
+        // Enables JAXB processing
+        JaxbAnnotationModule module = new JaxbAnnotationModule();
+        defaultMapper.registerModule(module);
     }
 
     @FallbackConverter
-    public <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry
registry) {
+    public <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry
registry) throws Exception {
 
         // only do this if enabled
         if (!init && exchange != null) {
             // init to see if this is enabled
             String text = exchange.getContext().getProperties().get(JacksonConstants.ENABLE_TYPE_CONVERTER);
             enabled = "true".equalsIgnoreCase(text);
+
+            // pojo only by default
+            text = exchange.getContext().getProperties().get(JacksonConstants.TYPE_CONVERTER_POJO_ONLY);
+            if (text != null) {
+                pojoOnly = "true".equalsIgnoreCase(text);
+            }
+
             init = true;
         }
 
@@ -50,14 +62,24 @@ public final class JacksonTypeConverters {
             return null;
         }
 
-
-        if (isNotPojoType(type)) {
+        if (pojoOnly && isNotPojoType(type)) {
             return null;
         }
 
-        if (exchange != null && value instanceof Map) {
+        if (exchange != null) {
             ObjectMapper mapper = resolveObjectMapper(exchange.getContext().getRegistry());
-            if (mapper.canSerialize(type)) {
+            if (String.class.isAssignableFrom(type)) {
+                String out = mapper.writeValueAsString(value);
+                return type.cast(out);
+            } else if (byte[].class.isAssignableFrom(type)) {
+                byte[] out = mapper.writeValueAsBytes(value);
+                return type.cast(out);
+            } else if (mapper.canSerialize(type)) {
+                // TODO: favor using mapper readValue
+                // if the input is string or input stream
+                if (String.class.isAssignableFrom(value.getClass())) {
+                    return mapper.readValue((String) value, type);
+                }
                 return mapper.convertValue(value, type);
             }
         }


Mime
View raw message