Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -551,6 +555,14 @@ public void enableFeature(MapperFeature feature) {
}
}

public void enableFeature(Enum<? extends DatatypeFeature> feature) {
if (enableFeatures == null) {
enableFeatures = feature.name();
} else {
enableFeatures += "," + feature.name();
}
}

public void disableFeature(SerializationFeature feature) {
if (disableFeatures == null) {
disableFeatures = feature.name();
Expand All @@ -575,6 +587,14 @@ public void disableFeature(MapperFeature feature) {
}
}

public void disableFeature(Enum<? extends DatatypeFeature> 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)) {
Expand Down Expand Up @@ -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]");
}
}

Expand Down Expand Up @@ -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]");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
*/
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;
import org.junit.jupiter.api.Test;
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 {

Expand All @@ -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() {
Expand All @@ -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");
}
};
}
Expand Down