Skip to content
Merged
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 @@ -507,8 +511,10 @@ public String getEnableFeatures() {

/**
* Set of features to enable on the Jackson {@link tools.jackson.databind.ObjectMapper}. The features should be a
* name that matches a enum from {@link tools.jackson.databind.SerializationFeature},
* {@link tools.jackson.databind.DeserializationFeature}, or {@link tools.jackson.databind.MapperFeature}.
* name that matches an enum from {@link tools.jackson.databind.SerializationFeature},
* {@link tools.jackson.databind.DeserializationFeature}, {@link tools.jackson.databind.MapperFeature},
* {@link tools.jackson.databind.cfg.DateTimeFeature}, {@link tools.jackson.databind.cfg.EnumFeature} or
* {@link tools.jackson.databind.cfg.JsonNodeFeature}.
*/
public void setEnableFeatures(String enableFeatures) {
this.enableFeatures = enableFeatures;
Expand All @@ -520,8 +526,10 @@ public String getDisableFeatures() {

/**
* Set of features to disable on the Jackson {@link tools.jackson.databind.ObjectMapper}. The features should be a
* name that matches a enum from {@link tools.jackson.databind.SerializationFeature},
* {@link tools.jackson.databind.DeserializationFeature}, or {@link tools.jackson.databind.MapperFeature}.
* name that matches an enum from {@link tools.jackson.databind.SerializationFeature},
* {@link tools.jackson.databind.DeserializationFeature}, {@link tools.jackson.databind.MapperFeature},
* {@link tools.jackson.databind.cfg.DateTimeFeature}, {@link tools.jackson.databind.cfg.EnumFeature} or
* {@link tools.jackson.databind.cfg.JsonNodeFeature}.
*/
public void setDisableFeatures(String disableFeatures) {
this.disableFeatures = disableFeatures;
Expand Down Expand Up @@ -551,6 +559,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 +591,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 +726,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 +845,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,36 +16,60 @@
*/
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 {

@Test
public void testEnableDeserializationFeature() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.message(0).body().isNull();

template.send("direct:format", exchange -> exchange.getIn().setBody("[]"));

mock.expectedMessageCount(1);

mock.assertIsSatisfied();
}

@Test
public void testEnableMapperFeature() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.message(0).body().isInstanceOf(TestPojo.class);

template.send("direct:format", exchange -> exchange.getIn().setBody("{\"nAmE\": \"test\"}"));

mock.assertIsSatisfied();
}

@Test
public void testEnableDatatypeFeature() throws Exception {
Comment thread
graben marked this conversation as resolved.
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.message(0).body(String.class).isEqualTo("123");

template.send("direct:unformat", exchange -> exchange.getIn().setBody(new Date(123)));

mock.assertIsSatisfied();
}

@Test
public void testEnableDatatypeFeatureViaModelString() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.message(0).body(String.class).isEqualTo("123");

template.send("direct:unformat-model", exchange -> exchange.getIn().setBody(new Date(123)));

mock.assertIsSatisfied();
}

Expand All @@ -61,8 +85,15 @@ 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");

JacksonDataFormat formatModel = new JacksonDataFormat();
formatModel.setEnableFeatures("WRITE_DATES_AS_TIMESTAMPS");

from("direct:unformat-model").marshal(formatModel).to("mock:result");
}
};
}
Expand Down