Skip to content

Commit

Permalink
CAMEL-8389: camel-jackson - Allow to configure jackson object mapper …
Browse files Browse the repository at this point in the history
…from XML DSL
  • Loading branch information
davsclaus committed Feb 22, 2015
1 parent 2777d9b commit 654e039
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 32 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
*/ */
package org.apache.camel.model.dataformat; package org.apache.camel.model.dataformat;


import java.util.Set;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.XmlTransient;


Expand Down Expand Up @@ -66,10 +64,10 @@ public class JsonDataFormat extends DataFormatDefinition {
private String moduleClassNames; private String moduleClassNames;
@XmlAttribute @XmlAttribute
private String moduleRefs; private String moduleRefs;
@XmlElement @XmlAttribute
private Set<String> enableFeatures; private String enableFeatures;
@XmlElement @XmlAttribute
private Set<String> disableFeatures; private String disableFeatures;


public JsonDataFormat() { public JsonDataFormat() {
super("json"); super("json");
Expand Down Expand Up @@ -223,7 +221,7 @@ public void setModuleRefs(String moduleRefs) {
this.moduleRefs = moduleRefs; this.moduleRefs = moduleRefs;
} }


public Set<String> getEnableFeatures() { public String getEnableFeatures() {
return enableFeatures; return enableFeatures;
} }


Expand All @@ -232,12 +230,14 @@ public Set<String> getEnableFeatures() {
* <p/> * <p/>
* The features should be a name that matches a enum from <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, * The features should be a name that matches a enum from <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
* <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or <tt>com.fasterxml.jackson.databind.MapperFeature</tt> * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or <tt>com.fasterxml.jackson.databind.MapperFeature</tt>
* <p/>
* Multiple features can be separated by comma
*/ */
public void setEnableFeatures(Set<String> enableFeatures) { public void setEnableFeatures(String enableFeatures) {
this.enableFeatures = enableFeatures; this.enableFeatures = enableFeatures;
} }


public Set<String> getDisableFeatures() { public String getDisableFeatures() {
return disableFeatures; return disableFeatures;
} }


Expand All @@ -246,8 +246,10 @@ public Set<String> getDisableFeatures() {
* <p/> * <p/>
* The features should be a name that matches a enum from <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, * The features should be a name that matches a enum from <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
* <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or <tt>com.fasterxml.jackson.databind.MapperFeature</tt> * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or <tt>com.fasterxml.jackson.databind.MapperFeature</tt>
* <p/>
* Multiple features can be separated by comma
*/ */
public void setDisableFeatures(Set<String> disableFeatures) { public void setDisableFeatures(String disableFeatures) {
this.disableFeatures = disableFeatures; this.disableFeatures = disableFeatures;
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;


import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
Expand Down Expand Up @@ -65,8 +64,8 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Cam
private boolean allowJmsType; private boolean allowJmsType;
private boolean useList; private boolean useList;
private boolean enableJaxbAnnotationModule; private boolean enableJaxbAnnotationModule;
private Set<String> enableFeatures; private String enableFeatures;
private Set<String> disableFeatures; private String disableFeatures;


/** /**
* Use the default Jackson {@link ObjectMapper} and {@link Map} * Use the default Jackson {@link ObjectMapper} and {@link Map}
Expand Down Expand Up @@ -294,70 +293,76 @@ public void setAllowJmsType(boolean allowJmsType) {
this.allowJmsType = allowJmsType; this.allowJmsType = allowJmsType;
} }


public Set<String> getEnableFeatures() { public String getEnableFeatures() {
return enableFeatures; return enableFeatures;
} }


/** /**
* Set of features to enable on the Jackson {@link ObjectMapper}. * Set of features to enable on the Jackson {@link ObjectMapper}.
* The features should be a name that matches a enum from {@link SerializationFeature}, {@link DeserializationFeature}, or {@link MapperFeature}. * The features should be a name that matches a enum from {@link SerializationFeature}, {@link DeserializationFeature}, or {@link MapperFeature}.
*/ */
public void setEnableFeatures(Set<String> enableFeatures) { public void setEnableFeatures(String enableFeatures) {
this.enableFeatures = enableFeatures; this.enableFeatures = enableFeatures;
} }


public Set<String> getDisableFeatures() { public String getDisableFeatures() {
return disableFeatures; return disableFeatures;
} }


/** /**
* Set of features to disable on the Jackson {@link ObjectMapper}. * Set of features to disable on the Jackson {@link ObjectMapper}.
* The features should be a name that matches a enum from {@link SerializationFeature}, {@link DeserializationFeature}, or {@link MapperFeature}. * The features should be a name that matches a enum from {@link SerializationFeature}, {@link DeserializationFeature}, or {@link MapperFeature}.
*/ */
public void setDisableFeatures(Set<String> disableFeatures) { public void setDisableFeatures(String disableFeatures) {
this.disableFeatures = disableFeatures; this.disableFeatures = disableFeatures;
} }


public void enableFeature(SerializationFeature feature) { public void enableFeature(SerializationFeature feature) {
if (enableFeatures == null) { if (enableFeatures == null) {
enableFeatures = new HashSet<String>(); enableFeatures = feature.name();
} else {
enableFeatures += "," + feature.name();
} }
enableFeatures.add(feature.name());
} }


public void enableFeature(DeserializationFeature feature) { public void enableFeature(DeserializationFeature feature) {
if (enableFeatures == null) { if (enableFeatures == null) {
enableFeatures = new HashSet<String>(); enableFeatures = feature.name();
} else {
enableFeatures += "," + feature.name();
} }
enableFeatures.add(feature.name());
} }


public void enableFeature(MapperFeature feature) { public void enableFeature(MapperFeature feature) {
if (enableFeatures == null) { if (enableFeatures == null) {
enableFeatures = new HashSet<String>(); enableFeatures = feature.name();
} else {
enableFeatures += "," + feature.name();
} }
enableFeatures.add(feature.name());
} }


public void disableFeature(SerializationFeature feature) { public void disableFeature(SerializationFeature feature) {
if (disableFeatures == null) { if (disableFeatures == null) {
disableFeatures = new HashSet<String>(); disableFeatures = feature.name();
} else {
disableFeatures += "," + feature.name();
} }
disableFeatures.add(feature.name());
} }


public void disableFeature(DeserializationFeature feature) { public void disableFeature(DeserializationFeature feature) {
if (disableFeatures == null) { if (disableFeatures == null) {
disableFeatures = new HashSet<String>(); disableFeatures = feature.name();
} else {
disableFeatures += "," + feature.name();
} }
disableFeatures.add(feature.name());
} }


public void disableFeature(MapperFeature feature) { public void disableFeature(MapperFeature feature) {
if (disableFeatures == null) { if (disableFeatures == null) {
disableFeatures = new HashSet<String>(); disableFeatures = feature.name();
} else {
disableFeatures += "," + feature.name();
} }
disableFeatures.add(feature.name());
} }


@Override @Override
Expand All @@ -382,7 +387,9 @@ protected void doStart() throws Exception {
} }


if (enableFeatures != null) { if (enableFeatures != null) {
for (String enable : enableFeatures) { Iterator<Object> it = ObjectHelper.createIterator(enableFeatures);
while (it.hasNext()) {
String enable = it.next().toString();
// it can be different kind // it can be different kind
SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, enable); SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, enable);
if (sf != null) { if (sf != null) {
Expand All @@ -403,7 +410,9 @@ protected void doStart() throws Exception {
} }
} }
if (disableFeatures != null) { if (disableFeatures != null) {
for (String disable : disableFeatures) { Iterator<Object> it = ObjectHelper.createIterator(enableFeatures);
while (it.hasNext()) {
String disable = it.next().toString();
// it can be different kind // it can be different kind
SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, disable); SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, disable);
if (sf != null) { if (sf != null) {
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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.test.spring.CamelSpringTestSupport;
import org.junit.Test;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @version
*/
public class SpringJacksonEnableFeatureTest extends CamelSpringTestSupport {

@Test
public void testMarshal() throws Exception {
TestPojoView in = new TestPojoView();

Object marshalled = template.requestBody("direct:in", in);
String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled);
// we enable the wrap root type feature so we should have TestPojoView
assertEquals("{\"TestPojoView\":{\"age\":30,\"height\":190,\"weight\":70}}", marshalledAsString);
}

@Override
protected AbstractXmlApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("org/apache/camel/component/jackson/SpringJacksonEnableFeatureTest.xml");
}

}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
">

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

<dataFormats>
<json id="jack" library="Jackson" unmarshalTypeName="org.apache.camel.component.jackson.TestPojo"
enableFeatures="WRAP_ROOT_VALUE"/>
</dataFormats>

<route>
<from uri="direct:in"/>
<marshal ref="jack"/>
</route>

</camelContext>

</beans>

0 comments on commit 654e039

Please sign in to comment.