Repository: camel Updated Branches: refs/heads/camel-2.13.x 3039e87fe -> c23789af5 refs/heads/master 122419876 -> d5394d3b4
CAMEL-7624: camel-jackson allow to configure include option to make it easy to skip null fields. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d5394d3b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d5394d3b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d5394d3b Branch: refs/heads/master Commit: d5394d3b47988e38c27ad54a9db5628b9772d81f Parents: 1224198 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Jul 22 12:34:55 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Jul 22 12:34:55 2014 +0200 ---------------------------------------------------------------------- .../apache/camel/builder/DataFormatClause.java | 18 ++++++- .../camel/model/dataformat/JsonDataFormat.java | 14 ++++- .../component/jackson/JacksonDataFormat.java | 28 ++++++++-- .../jackson/JacksonIncludeDefaultTest.java | 53 +++++++++++++++++++ .../jackson/JacksonIncludeNotNulllTest.java | 54 ++++++++++++++++++++ .../camel/component/jackson/TestOtherPojo.java | 40 +++++++++++++++ 6 files changed, 201 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d5394d3b/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java b/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java index b28d062..508b9fb 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java +++ b/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java @@ -359,9 +359,8 @@ public class DataFormatClause<T extends ProcessorDefinition<?>> { } /** - * Uses the JSON data format + * Uses the Jackson JSON data format * - * @param type the json type to use * @param unmarshalType unmarshal type for json jackson type * @param jsonView the view type for json jackson type */ @@ -373,6 +372,21 @@ public class DataFormatClause<T extends ProcessorDefinition<?>> { } /** + * Uses the Jackson JSON data format + * + * @param unmarshalType unmarshal type for json jackson type + * @param jsonView the view type for json jackson type + * @param include include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc. + */ + public T json(Class<?> unmarshalType, Class<?> jsonView, String include) { + JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson); + json.setUnmarshalType(unmarshalType); + json.setJsonView(jsonView); + json.setInclude(include); + return dataFormat(json); + } + + /** * Uses the protobuf data format */ public T protobuf() { http://git-wip-us.apache.org/repos/asf/camel/blob/d5394d3b/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java index 2cf7cf7..d72492a 100644 --- a/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java +++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java @@ -46,6 +46,8 @@ public class JsonDataFormat extends DataFormatDefinition { private Class<?> unmarshalType; @XmlAttribute private Class<?> jsonView; + @XmlAttribute + private String include; public JsonDataFormat() { } @@ -94,6 +96,14 @@ public class JsonDataFormat extends DataFormatDefinition { this.jsonView = jsonView; } + public String getInclude() { + return include; + } + + public void setInclude(String include) { + this.include = include; + } + @Override protected DataFormat createDataFormat(RouteContext routeContext) { if (library == JsonLibrary.XStream) { @@ -123,10 +133,12 @@ public class JsonDataFormat extends DataFormatDefinition { if (prettyPrint != null) { setProperty(camelContext, dataFormat, "prettyPrint", unmarshalType); } - if (jsonView != null) { setProperty(camelContext, dataFormat, "jsonView", jsonView); } + if (include != null) { + setProperty(camelContext, dataFormat, "include", include); + } } } http://git-wip-us.apache.org/repos/asf/camel/blob/d5394d3b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java index 231adf7..a537e33 100644 --- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java +++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java @@ -21,22 +21,23 @@ import java.io.OutputStream; import java.util.HashMap; import java.util.Map; +import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; - import org.apache.camel.Exchange; import org.apache.camel.spi.DataFormat; - +import org.apache.camel.support.ServiceSupport; /** * A <a href="http://camel.apache.org/data-format.html">data format</a> ({@link DataFormat}) * using <a href="http://jackson.codehaus.org/">Jackson</a> to marshal to and from JSON. */ -public class JacksonDataFormat implements DataFormat { +public class JacksonDataFormat extends ServiceSupport implements DataFormat { private final ObjectMapper objectMapper; private Class<?> unmarshalType; private Class<?> jsonView; + private String include; /** * Use the default Jackson {@link ObjectMapper} and {@link Map} @@ -128,4 +129,25 @@ public class JacksonDataFormat implements DataFormat { return this.objectMapper; } + public String getInclude() { + return include; + } + + public void setInclude(String include) { + this.include = include; + } + + @Override + protected void doStart() throws Exception { + if (include != null) { + JsonInclude.Include inc = JsonInclude.Include.valueOf(include); + objectMapper.setSerializationInclusion(inc); + } + } + + @Override + protected void doStop() throws Exception { + // noop + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/d5394d3b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonIncludeDefaultTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonIncludeDefaultTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonIncludeDefaultTest.java new file mode 100644 index 0000000..4baca3f --- /dev/null +++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonIncludeDefaultTest.java @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.jackson; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class JacksonIncludeDefaultTest extends CamelTestSupport { + + @Test + public void testMmarshalPojo() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:marshal"); + mock.expectedMessageCount(1); + mock.message(0).body(String.class).isEqualTo("{\"name\":\"Camel\",\"country\":null}"); + + TestOtherPojo pojo = new TestOtherPojo(); + pojo.setName("Camel"); + + template.sendBody("direct:marshal", pojo); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + JacksonDataFormat format = new JacksonDataFormat(); + + from("direct:marshal").marshal(format).to("mock:marshal"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/d5394d3b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonIncludeNotNulllTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonIncludeNotNulllTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonIncludeNotNulllTest.java new file mode 100644 index 0000000..7811402 --- /dev/null +++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonIncludeNotNulllTest.java @@ -0,0 +1,54 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.jackson; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class JacksonIncludeNotNulllTest extends CamelTestSupport { + + @Test + public void testMmarshalPojo() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:marshal"); + mock.expectedMessageCount(1); + mock.message(0).body(String.class).isEqualTo("{\"name\":\"Camel\"}"); + + TestOtherPojo pojo = new TestOtherPojo(); + pojo.setName("Camel"); + + template.sendBody("direct:marshal", pojo); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + JacksonDataFormat format = new JacksonDataFormat(); + format.setInclude("NON_NULL"); + + from("direct:marshal").marshal(format).to("mock:marshal"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/d5394d3b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestOtherPojo.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestOtherPojo.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestOtherPojo.java new file mode 100644 index 0000000..cfdde52 --- /dev/null +++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestOtherPojo.java @@ -0,0 +1,40 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.jackson; + +public class TestOtherPojo { + + private String name; + private String country; + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + +} \ No newline at end of file