diff --git a/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/AbstractJacksonDataFormat.java b/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/AbstractJacksonDataFormat.java index b09f60487ef44..c490245eccc96 100644 --- a/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/AbstractJacksonDataFormat.java +++ b/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/AbstractJacksonDataFormat.java @@ -55,6 +55,10 @@ import tools.jackson.databind.PropertyNamingStrategies; import tools.jackson.databind.PropertyNamingStrategy; import tools.jackson.databind.SerializationFeature; +import tools.jackson.databind.cfg.DatatypeFeature; +import tools.jackson.databind.cfg.DateTimeFeature; +import tools.jackson.databind.cfg.EnumFeature; +import tools.jackson.databind.cfg.JsonNodeFeature; import tools.jackson.databind.type.CollectionType; /** @@ -551,6 +555,14 @@ public void enableFeature(MapperFeature feature) { } } + public void enableFeature(Enum feature) { + if (enableFeatures == null) { + enableFeatures = feature.name(); + } else { + enableFeatures += "," + feature.name(); + } + } + public void disableFeature(SerializationFeature feature) { if (disableFeatures == null) { disableFeatures = feature.name(); @@ -575,6 +587,14 @@ public void disableFeature(MapperFeature feature) { } } + public void disableFeature(Enum feature) { + if (disableFeatures == null) { + disableFeatures = feature.name(); + } else { + disableFeatures += "," + feature.name(); + } + } + @Override protected void doInit() throws Exception { if (unmarshalTypeName != null && (unmarshalType == null || unmarshalType == Object.class)) { @@ -702,9 +722,27 @@ private void doEnableFeaures() { setObjectMapper(om); continue; } + DateTimeFeature dtf = getCamelContext().getTypeConverter().tryConvertTo(DateTimeFeature.class, enable); + if (dtf != null) { + ObjectMapper om = objectMapper.rebuild().enable(dtf).build(); + setObjectMapper(om); + continue; + } + EnumFeature ef = getCamelContext().getTypeConverter().tryConvertTo(EnumFeature.class, enable); + if (ef != null) { + ObjectMapper om = objectMapper.rebuild().enable(ef).build(); + setObjectMapper(om); + continue; + } + JsonNodeFeature jnf = getCamelContext().getTypeConverter().tryConvertTo(JsonNodeFeature.class, enable); + if (jnf != null) { + ObjectMapper om = objectMapper.rebuild().enable(jnf).build(); + setObjectMapper(om); + continue; + } throw new IllegalArgumentException( "Enable feature: " + enable - + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]"); + + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature,DateTimeFeature,EnumFeature,JsonNodeFeature]"); } } @@ -803,9 +841,27 @@ private void doDisableFeatures() { setObjectMapper(om); continue; } + DateTimeFeature dtf = getCamelContext().getTypeConverter().tryConvertTo(DateTimeFeature.class, disable); + if (dtf != null) { + ObjectMapper om = objectMapper.rebuild().disable(dtf).build(); + setObjectMapper(om); + continue; + } + EnumFeature ef = getCamelContext().getTypeConverter().tryConvertTo(EnumFeature.class, disable); + if (ef != null) { + ObjectMapper om = objectMapper.rebuild().disable(ef).build(); + setObjectMapper(om); + continue; + } + JsonNodeFeature jnf = getCamelContext().getTypeConverter().tryConvertTo(JsonNodeFeature.class, disable); + if (jnf != null) { + ObjectMapper om = objectMapper.rebuild().disable(jnf).build(); + setObjectMapper(om); + continue; + } throw new IllegalArgumentException( "Disable feature: " + disable - + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]"); + + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature,DateTimeFeature,EnumFeature,JsonNodeFeature]"); } } diff --git a/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/JacksonFeaturesTest.java b/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/JacksonFeaturesTest.java index 8207ebf1c07a5..69a43ef6a09c6 100644 --- a/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/JacksonFeaturesTest.java +++ b/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/JacksonFeaturesTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.jackson3; +import java.util.Date; + import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit6.CamelTestSupport; @@ -23,6 +25,7 @@ import tools.jackson.databind.DeserializationFeature; import tools.jackson.databind.MapperFeature; import tools.jackson.databind.SerializationFeature; +import tools.jackson.databind.cfg.DateTimeFeature; public class JacksonFeaturesTest extends CamelTestSupport { @@ -49,6 +52,17 @@ public void testEnableMapperFeature() throws Exception { mock.assertIsSatisfied(); } + @Test + public void testEnableDatatypeMapperFeature() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.message(0).body().isEqualTo("123"); + + template.send("direct:unformat", exchange -> exchange.getIn().setBody(new Date(123))); + + mock.expectedMessageCount(1); + mock.assertIsSatisfied(); + } + @Override protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { @@ -61,8 +75,10 @@ public void configure() { format.disableFeature(SerializationFeature.INDENT_OUTPUT); format.disableFeature(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); format.disableFeature(MapperFeature.APPLY_DEFAULT_VALUES); + format.enableFeature(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS); from("direct:format").unmarshal(format).to("mock:result"); + from("direct:unformat").marshal(format).to("mock:result"); } }; }