This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 3f6df8e7e8e CAMEL-19269: camel-jackson - Converter for jackson numeric 
wrappers
3f6df8e7e8e is described below

commit 3f6df8e7e8e742d3f252a06471d8d033c15644e3
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Apr 16 16:48:40 2023 +0200

    CAMEL-19269: camel-jackson - Converter for jackson numeric wrappers
---
 .../converter/JacksonTypeConvertersLoader.java     | 10 ++++
 .../apache/camel/component/jackson/jackson.json    |  3 +-
 .../jackson/converter/JacksonTypeConverters.java   | 60 ++++++++++++++++++++++
 .../converter/JacksonJSonNodeConverterTest.java    | 46 +++++++++++++++++
 4 files changed, 118 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-jackson/src/generated/java/org/apache/camel/component/jackson/converter/JacksonTypeConvertersLoader.java
 
b/components/camel-jackson/src/generated/java/org/apache/camel/component/jackson/converter/JacksonTypeConvertersLoader.java
index a527933abc1..3e0df6fae52 100644
--- 
a/components/camel-jackson/src/generated/java/org/apache/camel/component/jackson/converter/JacksonTypeConvertersLoader.java
+++ 
b/components/camel-jackson/src/generated/java/org/apache/camel/component/jackson/converter/JacksonTypeConvertersLoader.java
@@ -60,6 +60,16 @@ public final class JacksonTypeConvertersLoader implements 
TypeConverterLoader, C
             (type, exchange, value) -> 
getJacksonTypeConverters().toInputStream((com.fasterxml.jackson.databind.JsonNode)
 value, exchange));
         addTypeConverter(registry, java.io.Reader.class, 
com.fasterxml.jackson.databind.JsonNode.class, false,
             (type, exchange, value) -> 
getJacksonTypeConverters().toReader((com.fasterxml.jackson.databind.JsonNode) 
value, exchange));
+        addTypeConverter(registry, java.lang.Boolean.class, 
com.fasterxml.jackson.databind.JsonNode.class, false,
+            (type, exchange, value) -> 
getJacksonTypeConverters().toBoolean((com.fasterxml.jackson.databind.JsonNode) 
value, exchange));
+        addTypeConverter(registry, java.lang.Double.class, 
com.fasterxml.jackson.databind.JsonNode.class, false,
+            (type, exchange, value) -> 
getJacksonTypeConverters().toDouble((com.fasterxml.jackson.databind.JsonNode) 
value, exchange));
+        addTypeConverter(registry, java.lang.Float.class, 
com.fasterxml.jackson.databind.JsonNode.class, false,
+            (type, exchange, value) -> 
getJacksonTypeConverters().toFloat((com.fasterxml.jackson.databind.JsonNode) 
value, exchange));
+        addTypeConverter(registry, java.lang.Integer.class, 
com.fasterxml.jackson.databind.JsonNode.class, false,
+            (type, exchange, value) -> 
getJacksonTypeConverters().toInteger((com.fasterxml.jackson.databind.JsonNode) 
value, exchange));
+        addTypeConverter(registry, java.lang.Long.class, 
com.fasterxml.jackson.databind.JsonNode.class, false,
+            (type, exchange, value) -> 
getJacksonTypeConverters().toLong((com.fasterxml.jackson.databind.JsonNode) 
value, exchange));
         addTypeConverter(registry, java.lang.String.class, 
com.fasterxml.jackson.databind.JsonNode.class, false,
             (type, exchange, value) -> 
getJacksonTypeConverters().toString((com.fasterxml.jackson.databind.JsonNode) 
value, exchange));
         addTypeConverter(registry, java.util.Map.class, 
com.fasterxml.jackson.databind.JsonNode.class, false,
diff --git 
a/components/camel-jackson/src/generated/resources/org/apache/camel/component/jackson/jackson.json
 
b/components/camel-jackson/src/generated/resources/org/apache/camel/component/jackson/jackson.json
index a4deb7f0ee8..640221f4a72 100644
--- 
a/components/camel-jackson/src/generated/resources/org/apache/camel/component/jackson/jackson.json
+++ 
b/components/camel-jackson/src/generated/resources/org/apache/camel/component/jackson/jackson.json
@@ -31,7 +31,8 @@
     "enableFeatures": { "kind": "attribute", "displayName": "Enable Features", 
"label": "advanced", "required": false, "type": "string", "javaType": 
"java.lang.String", "deprecated": false, "autowired": false, "secret": false, 
"description": "Set of features to enable on the Jackson 
com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that 
matches a enum from com.fasterxml.jackson.databind.SerializationFeature, 
com.fasterxml.jackson.databind.DeserializationFeature,  [...]
     "disableFeatures": { "kind": "attribute", "displayName": "Disable 
Features", "label": "advanced", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": false, "description": "Set of features to disable on the Jackson 
com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that 
matches a enum from com.fasterxml.jackson.databind.SerializationFeature, 
com.fasterxml.jackson.databind.DeserializationFeatur [...]
     "allowUnmarshallType": { "kind": "attribute", "displayName": "Allow 
Unmarshall Type", "required": false, "type": "boolean", "javaType": 
"java.lang.Boolean", "deprecated": false, "autowired": false, "secret": false, 
"defaultValue": false, "description": "If enabled then Jackson is allowed to 
attempt to use the CamelJacksonUnmarshalType header during the unmarshalling. 
This should only be enabled when desired to be used." },
-    "timezone": { "kind": "attribute", "displayName": "Timezone", "label": 
"advanced", "required": false, "type": "string", "javaType": 
"java.lang.String", "deprecated": false, "autowired": false, "secret": false, 
"description": "If set then Jackson will use the Timezone when 
marshalling\/unmarshalling. This option will have no effect on the others Json 
DataFormat, like gson and fastjson." },
+    "timezone": { "kind": "attribute", "displayName": "Timezone", "label": 
"advanced", "required": false, "type": "string", "javaType": 
"java.lang.String", "deprecated": false, "autowired": false, "secret": false, 
"description": "If set then Jackson will use the Timezone when 
marshalling\/unmarshalling. This option will have no effect on the others Json 
DataFormat, like gson, fastjson and xstream." },
+    "dropRootNode": { "kind": "attribute", "displayName": "Drop Root Node", 
"label": "advanced", "required": false, "type": "boolean", "javaType": 
"java.lang.Boolean", "deprecated": false, "autowired": false, "secret": false, 
"defaultValue": false, "description": "Whether XStream will drop the root node 
in the generated JSon. You may want to enable this when using POJOs; as then 
the written object will include the class name as root node, which is often not 
intended to be written in the  [...]
     "schemaResolver": { "kind": "attribute", "displayName": "Schema Resolver", 
"label": "advanced", "required": false, "type": "string", "javaType": 
"java.lang.String", "deprecated": false, "autowired": false, "secret": false, 
"description": "Optional schema resolver used to lookup schemas for the data in 
transit." },
     "autoDiscoverSchemaResolver": { "kind": "attribute", "displayName": "Auto 
Discover Schema Resolver", "label": "advanced", "required": false, "type": 
"boolean", "javaType": "java.lang.Boolean", "deprecated": false, "autowired": 
false, "secret": false, "defaultValue": true, "description": "When not 
disabled, the SchemaResolver will be looked up into the registry" },
     "namingStrategy": { "kind": "attribute", "displayName": "Naming Strategy", 
"required": false, "type": "string", "javaType": "java.lang.String", 
"deprecated": false, "autowired": false, "secret": false, "description": "If 
set then Jackson will use the the defined Property Naming Strategy.Possible 
values are: LOWER_CAMEL_CASE, LOWER_DOT_CASE, LOWER_CASE, KEBAB_CASE, 
SNAKE_CASE and UPPER_CAMEL_CASE" },
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 ec0c60c06ec..e93b78cf873 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
@@ -30,6 +30,8 @@ import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.Module;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.BooleanNode;
+import com.fasterxml.jackson.databind.node.NumericNode;
 import com.fasterxml.jackson.databind.node.TextNode;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Converter;
@@ -144,6 +146,64 @@ public final class JacksonTypeConverters {
         return new InputStreamReader(is);
     }
 
+    @Converter
+    public Integer toInteger(JsonNode node, Exchange exchange) throws 
Exception {
+        if (node instanceof NumericNode) {
+            NumericNode nn = (NumericNode) node;
+            if (nn.canConvertToInt()) {
+                return nn.asInt();
+            }
+        }
+        String text = node.asText();
+        return Integer.valueOf(text);
+    }
+
+    @Converter
+    public Long toLong(JsonNode node, Exchange exchange) throws Exception {
+        if (node instanceof NumericNode) {
+            NumericNode nn = (NumericNode) node;
+            if (nn.canConvertToLong()) {
+                return nn.asLong();
+            }
+        }
+        String text = node.asText();
+        return Long.valueOf(text);
+    }
+
+    @Converter
+    public Boolean toBoolean(JsonNode node, Exchange exchange) throws 
Exception {
+        if (node instanceof BooleanNode) {
+            BooleanNode bn = (BooleanNode) node;
+            return bn.asBoolean();
+        }
+        String text = node.asText();
+        return org.apache.camel.util.ObjectHelper.toBoolean(text);
+    }
+
+    @Converter
+    public Double toDouble(JsonNode node, Exchange exchange) throws Exception {
+        if (node instanceof NumericNode) {
+            NumericNode nn = (NumericNode) node;
+            if (nn.isFloatingPointNumber()) {
+                return nn.asDouble();
+            }
+        }
+        String text = node.asText();
+        return Double.valueOf(text);
+    }
+
+    @Converter
+    public Float toFloat(JsonNode node, Exchange exchange) throws Exception {
+        if (node instanceof NumericNode) {
+            NumericNode nn = (NumericNode) node;
+            if (nn.isFloat()) {
+                return nn.floatValue();
+            }
+        }
+        String text = node.asText();
+        return Float.valueOf(text);
+    }
+
     @Converter(fallback = true)
     public <T> T convertTo(Class<T> type, Exchange exchange, Object value, 
TypeConverterRegistry registry) throws Exception {
 
diff --git 
a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/converter/JacksonJSonNodeConverterTest.java
 
b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/converter/JacksonJSonNodeConverterTest.java
index f4669c0eab0..03c74ef4a22 100644
--- 
a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/converter/JacksonJSonNodeConverterTest.java
+++ 
b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/converter/JacksonJSonNodeConverterTest.java
@@ -22,6 +22,10 @@ import java.nio.charset.StandardCharsets;
 import java.util.Map;
 
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.BooleanNode;
+import com.fasterxml.jackson.databind.node.DoubleNode;
+import com.fasterxml.jackson.databind.node.IntNode;
+import com.fasterxml.jackson.databind.node.LongNode;
 import com.fasterxml.jackson.databind.node.TextNode;
 import org.apache.camel.Exchange;
 import org.apache.camel.support.DefaultExchange;
@@ -148,4 +152,46 @@ public class JacksonJSonNodeConverterTest extends 
CamelTestSupport {
         Assertions.assertEquals(44, out.get("age"));
     }
 
+    @Test
+    public void convertToInt() {
+        int value = context.getTypeConverter().convertTo(Integer.class, new 
IntNode(42));
+        Assertions.assertEquals(42, value);
+
+        value = context.getTypeConverter().convertTo(int.class, new 
IntNode(43));
+        Assertions.assertEquals(43, value);
+    }
+
+    @Test
+    public void convertToLong() {
+        long value = context.getTypeConverter().convertTo(Long.class, new 
LongNode(1234567890));
+        Assertions.assertEquals(1234567890L, value);
+
+        value = context.getTypeConverter().convertTo(long.class, new 
LongNode(44448888));
+        Assertions.assertEquals(44448888L, value);
+    }
+
+    @Test
+    public void convertToDouble() {
+        double value = context.getTypeConverter().convertTo(Double.class, new 
DoubleNode(1.23d));
+        Assertions.assertEquals(1.23d, value);
+
+        value = context.getTypeConverter().convertTo(double.class, new 
DoubleNode(1.99d));
+        Assertions.assertEquals(1.99d, value);
+    }
+
+    @Test
+    public void convertToFloat() {
+        float value = context.getTypeConverter().convertTo(Float.class, new 
DoubleNode(2.23f));
+        Assertions.assertEquals(2.23f, value);
+
+        value = context.getTypeConverter().convertTo(float.class, new 
DoubleNode(2.99f));
+        Assertions.assertEquals(2.99f, value);
+    }
+
+    @Test
+    public void convertToBoolean() {
+        boolean value = context.getTypeConverter().convertTo(Boolean.class, 
BooleanNode.TRUE);
+        Assertions.assertTrue(value);
+    }
+
 }

Reply via email to