Author: davsclaus
Date: Sun Mar 31 08:28:06 2013
New Revision: 1462899
URL: http://svn.apache.org/r1462899
Log:
CAMEL-6227: gson data format can configure more options now.
Added:
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonFieldNamePolicyTest.java
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/PersonPojo.java
- copied, changed from r1462883,
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/TestPojo.java
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.java
camel/trunk/components/camel-gson/src/test/resources/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.xml
- copied, changed from r1462883,
camel/trunk/components/camel-gson/src/test/resources/org/apache/camel/component/gson/SpringGsonJsonDataFormatTest.xml
Modified:
camel/trunk/components/camel-gson/src/main/java/org/apache/camel/component/gson/GsonDataFormat.java
Modified:
camel/trunk/components/camel-gson/src/main/java/org/apache/camel/component/gson/GsonDataFormat.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-gson/src/main/java/org/apache/camel/component/gson/GsonDataFormat.java?rev=1462899&r1=1462898&r2=1462899&view=diff
==============================================================================
---
camel/trunk/components/camel-gson/src/main/java/org/apache/camel/component/gson/GsonDataFormat.java
(original)
+++
camel/trunk/components/camel-gson/src/main/java/org/apache/camel/component/gson/GsonDataFormat.java
Sun Mar 31 08:28:06 2013
@@ -22,23 +22,36 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
+import java.util.Arrays;
+import java.util.List;
import java.util.Map;
import com.google.gson.ExclusionStrategy;
+import com.google.gson.FieldNamingPolicy;
+import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import com.google.gson.LongSerializationPolicy;
import org.apache.camel.Exchange;
import org.apache.camel.spi.DataFormat;
+import org.apache.camel.support.ServiceSupport;
import org.apache.camel.util.IOHelper;
/**
* A <a href="http://camel.apache.org/data-format.html">data format</a>
({@link DataFormat})
* using <a href="http://code.google.com/p/google-gson/">Gson</a> to marshal
to and from JSON.
*/
-public class GsonDataFormat implements DataFormat {
+public class GsonDataFormat extends ServiceSupport implements DataFormat {
- private final Gson gson;
+ private Gson gson;
private Class<?> unmarshalType;
+ private List<ExclusionStrategy> exclusionStrategies;
+ private LongSerializationPolicy longSerializationPolicy;
+ private FieldNamingPolicy fieldNamingPolicy;
+ private FieldNamingStrategy fieldNamingStrategy;
+ private Boolean serializeNulls;
+ private Boolean prettyPrinting;
+ private String dateFormatPattern;
public GsonDataFormat() {
this(Map.class);
@@ -51,7 +64,7 @@ public class GsonDataFormat implements D
* @param unmarshalType the custom unmarshal type
*/
public GsonDataFormat(Class<?> unmarshalType) {
- this(new Gson(), unmarshalType);
+ this(null, unmarshalType);
}
/**
@@ -60,9 +73,12 @@ public class GsonDataFormat implements D
*
* @param unmarshalType the custom unmarshal type
* @param exclusionStrategies one or more custom ExclusionStrategy
implementations
+ * @deprecated use the setter instead
*/
+ @Deprecated
public GsonDataFormat(Class<?> unmarshalType, ExclusionStrategy...
exclusionStrategies) {
- this(createGsonWithExclusionStrategy(exclusionStrategies),
unmarshalType);
+ this(null, unmarshalType);
+ setExclusionStrategies(Arrays.asList(exclusionStrategies));
}
/**
@@ -76,10 +92,6 @@ public class GsonDataFormat implements D
this.unmarshalType = unmarshalType;
}
- private static Gson createGsonWithExclusionStrategy(ExclusionStrategy...
exclusionStrategies) {
- return exclusionStrategies != null ? new
GsonBuilder().setExclusionStrategies(exclusionStrategies).create() : new Gson();
- }
-
@Override
public void marshal(Exchange exchange, Object graph, OutputStream stream)
throws Exception {
BufferedWriter writer = IOHelper.buffered(new
OutputStreamWriter(stream));
@@ -95,6 +107,39 @@ public class GsonDataFormat implements D
return result;
}
+ @Override
+ protected void doStart() throws Exception {
+ GsonBuilder builder = new GsonBuilder();
+ if (exclusionStrategies != null && !exclusionStrategies.isEmpty()) {
+ ExclusionStrategy[] strategies = exclusionStrategies.toArray(new
ExclusionStrategy[exclusionStrategies.size()]);
+ builder.setExclusionStrategies(strategies);
+ }
+ if (longSerializationPolicy != null) {
+ builder.setLongSerializationPolicy(longSerializationPolicy);
+ }
+ if (fieldNamingPolicy != null) {
+ builder.setFieldNamingPolicy(fieldNamingPolicy);
+ }
+ if (fieldNamingStrategy != null) {
+ builder.setFieldNamingStrategy(fieldNamingStrategy);
+ }
+ if (serializeNulls != null && serializeNulls) {
+ builder.serializeNulls();
+ }
+ if (prettyPrinting != null && prettyPrinting) {
+ builder.setPrettyPrinting();
+ }
+ if (dateFormatPattern != null) {
+ builder.setDateFormat(dateFormatPattern);
+ }
+ gson = builder.create();
+ }
+
+ @Override
+ protected void doStop() throws Exception {
+ // noop
+ }
+
// Properties
//
-------------------------------------------------------------------------
@@ -106,6 +151,62 @@ public class GsonDataFormat implements D
this.unmarshalType = unmarshalType;
}
+ public List<ExclusionStrategy> getExclusionStrategies() {
+ return exclusionStrategies;
+ }
+
+ public void setExclusionStrategies(List<ExclusionStrategy>
exclusionStrategies) {
+ this.exclusionStrategies = exclusionStrategies;
+ }
+
+ public LongSerializationPolicy getLongSerializationPolicy() {
+ return longSerializationPolicy;
+ }
+
+ public void setLongSerializationPolicy(LongSerializationPolicy
longSerializationPolicy) {
+ this.longSerializationPolicy = longSerializationPolicy;
+ }
+
+ public FieldNamingPolicy getFieldNamingPolicy() {
+ return fieldNamingPolicy;
+ }
+
+ public void setFieldNamingPolicy(FieldNamingPolicy fieldNamingPolicy) {
+ this.fieldNamingPolicy = fieldNamingPolicy;
+ }
+
+ public FieldNamingStrategy getFieldNamingStrategy() {
+ return fieldNamingStrategy;
+ }
+
+ public void setFieldNamingStrategy(FieldNamingStrategy
fieldNamingStrategy) {
+ this.fieldNamingStrategy = fieldNamingStrategy;
+ }
+
+ public Boolean getSerializeNulls() {
+ return serializeNulls;
+ }
+
+ public void setSerializeNulls(Boolean serializeNulls) {
+ this.serializeNulls = serializeNulls;
+ }
+
+ public Boolean getPrettyPrinting() {
+ return prettyPrinting;
+ }
+
+ public void setPrettyPrinting(Boolean prettyPrinting) {
+ this.prettyPrinting = prettyPrinting;
+ }
+
+ public String getDateFormatPattern() {
+ return dateFormatPattern;
+ }
+
+ public void setDateFormatPattern(String dateFormatPattern) {
+ this.dateFormatPattern = dateFormatPattern;
+ }
+
public Gson getGson() {
return this.gson;
}
Added:
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonFieldNamePolicyTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonFieldNamePolicyTest.java?rev=1462899&view=auto
==============================================================================
---
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonFieldNamePolicyTest.java
(added)
+++
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonFieldNamePolicyTest.java
Sun Mar 31 08:28:06 2013
@@ -0,0 +1,64 @@
+/**
+ * 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.gson;
+
+import com.google.gson.FieldNamingPolicy;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class GsonFieldNamePolicyTest extends CamelTestSupport {
+
+ @Test
+ public void testUnmarshalPojo() throws Exception {
+ String json =
"{\"id\":\"123\",\"first_name\":\"Donald\",\"last_name\":\"Duck\"}";
+ PersonPojo pojo = template.requestBody("direct:backPojo", json,
PersonPojo.class);
+ assertNotNull(pojo);
+
+ assertEquals(123, pojo.getId());
+ assertEquals("Donald", pojo.getFirstName());
+ assertEquals("Duck", pojo.getLastName());
+ }
+
+ @Test
+ public void testMarshalPojo() throws Exception {
+ PersonPojo pojo = new PersonPojo();
+ pojo.setId(123);
+ pojo.setFirstName("Donald");
+ pojo.setLastName("Duck");
+
+ String expected =
"{\"id\":123,\"first_name\":\"Donald\",\"last_name\":\"Duck\"}";
+ String json = template.requestBody("direct:inPojo", pojo,
String.class);
+ assertEquals(expected, json);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ GsonDataFormat formatPojo = new GsonDataFormat();
+ formatPojo.setUnmarshalType(PersonPojo.class);
+
formatPojo.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
+
+ from("direct:inPojo").marshal(formatPojo);
+ from("direct:backPojo").unmarshal(formatPojo);
+ }
+ };
+ }
+
+}
Copied:
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/PersonPojo.java
(from r1462883,
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/TestPojo.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/PersonPojo.java?p2=camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/PersonPojo.java&p1=camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/TestPojo.java&r1=1462883&r2=1462899&rev=1462899&view=diff
==============================================================================
---
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/TestPojo.java
(original)
+++
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/PersonPojo.java
Sun Mar 31 08:28:06 2013
@@ -16,30 +16,33 @@
*/
package org.apache.camel.component.gson;
-public class TestPojo {
+public class PersonPojo {
- private String name;
+ private int id;
+ private String firstName;
+ private String lastName;
- public String getName() {
- return this.name;
+ public int getId() {
+ return id;
}
- public void setName(String name) {
- this.name = name;
+ public void setId(int id) {
+ this.id = id;
}
- @Override
- public boolean equals(Object obj) {
- return this.name.equals(((TestPojo) obj).getName());
+ public String getFirstName() {
+ return firstName;
}
- @Override
- public int hashCode() {
- return name != null ? name.hashCode() : 0;
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
}
- @Override
- public String toString() {
- return "TestPojo[" + name + "]";
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
}
}
\ No newline at end of file
Added:
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.java?rev=1462899&view=auto
==============================================================================
---
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.java
(added)
+++
camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.java
Sun Mar 31 08:28:06 2013
@@ -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.gson;
+
+import org.apache.camel.test.junit4.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class SpringGsonFieldNamePolicyTest extends CamelSpringTestSupport {
+
+ @Override
+ protected AbstractApplicationContext createApplicationContext() {
+ return new
ClassPathXmlApplicationContext("org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.xml");
+ }
+
+ @Test
+ public void testUnmarshalPojo() throws Exception {
+ String json =
"{\"id\":\"123\",\"first_name\":\"Donald\",\"last_name\":\"Duck\"}";
+ PersonPojo pojo = template.requestBody("direct:backPojo", json,
PersonPojo.class);
+ assertNotNull(pojo);
+
+ assertEquals(123, pojo.getId());
+ assertEquals("Donald", pojo.getFirstName());
+ assertEquals("Duck", pojo.getLastName());
+ }
+
+ @Test
+ public void testMarshalPojo() throws Exception {
+ PersonPojo pojo = new PersonPojo();
+ pojo.setId(123);
+ pojo.setFirstName("Donald");
+ pojo.setLastName("Duck");
+
+ String expected =
"{\"id\":123,\"first_name\":\"Donald\",\"last_name\":\"Duck\"}";
+ String json = template.requestBody("direct:inPojo", pojo,
String.class);
+ assertEquals(expected, json);
+ }
+}
Copied:
camel/trunk/components/camel-gson/src/test/resources/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.xml
(from r1462883,
camel/trunk/components/camel-gson/src/test/resources/org/apache/camel/component/gson/SpringGsonJsonDataFormatTest.xml)
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-gson/src/test/resources/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.xml?p2=camel/trunk/components/camel-gson/src/test/resources/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.xml&p1=camel/trunk/components/camel-gson/src/test/resources/org/apache/camel/component/gson/SpringGsonJsonDataFormatTest.xml&r1=1462883&r2=1462899&rev=1462899&view=diff
==============================================================================
---
camel/trunk/components/camel-gson/src/test/resources/org/apache/camel/component/gson/SpringGsonJsonDataFormatTest.xml
(original)
+++
camel/trunk/components/camel-gson/src/test/resources/org/apache/camel/component/gson/SpringGsonFieldNamePolicyTest.xml
Sun Mar 31 08:28:06 2013
@@ -23,13 +23,16 @@
">
<!-- START SNIPPET: e1 -->
- <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+ <!-- define the gson data format, where we configure the data format using
the properties -->
+ <bean id="gson" class="org.apache.camel.component.gson.GsonDataFormat">
+ <!-- we want to unmarshal to person pojo -->
+ <property name="unmarshalType"
value="org.apache.camel.component.gson.PersonPojo"/>
+ <!-- we want to map fields to use lower case and underscores -->
+ <property name="fieldNamingPolicy"
value="LOWER_CASE_WITH_UNDERSCORES"/>
+ </bean>
- <!-- we define the json jackson data formats to be used -->
- <dataFormats>
- <!-- this one uses our own TestPojo class as unmarshal type -->
- <json id="gson" library="Gson"
unmarshalTypeName="org.apache.camel.component.gson.TestPojo"/>
- </dataFormats>
+ <!-- Camel application where we use the gson data format in two routes -->
+ <camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:inPojo"/>
@@ -39,7 +42,6 @@
<route>
<from uri="direct:backPojo"/>
<unmarshal ref="gson"/>
- <to uri="mock:reversePojo"/>
</route>
</camelContext>