From bc3ca2dea1719d34a16e1b2989e770801eacadcb Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 21 May 2014 18:11:49 -0400 Subject: [PATCH] Polish --- .../GsonBase64ByteArrayJsonTypeAdapter.java | 7 ++-- .../http/converter/json/GsonFactoryBean.java | 36 +++++++++-------- .../json/GsonHttpMessageConverter.java | 28 +++++++------ .../converter/json/GsonFactoryBeanTests.java | 8 ++-- .../json/GsonHttpMessageConverterTests.java | 40 +++++++++---------- 5 files changed, 60 insertions(+), 59 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/GsonBase64ByteArrayJsonTypeAdapter.java b/spring-web/src/main/java/org/springframework/http/converter/json/GsonBase64ByteArrayJsonTypeAdapter.java index a232af6dcc..54212f480a 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/GsonBase64ByteArrayJsonTypeAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/GsonBase64ByteArrayJsonTypeAdapter.java @@ -51,13 +51,12 @@ final class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer @Override public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(new String(this.base64.encode(src), DEFAULT_CHARSET)); + String encoded = new String(this.base64.encode(src), DEFAULT_CHARSET); + return new JsonPrimitive(encoded); } @Override - public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - + public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext cxt) throws JsonParseException { return this.base64.decode(json.getAsString().getBytes(DEFAULT_CHARSET)); } diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/GsonFactoryBean.java b/spring-web/src/main/java/org/springframework/http/converter/json/GsonFactoryBean.java index 44968e99a3..8355e03704 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/GsonFactoryBean.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/GsonFactoryBean.java @@ -25,13 +25,12 @@ import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.ClassUtils; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; /** - * A {@link FactoryBean} for creating a Google Gson 2.x {@link Gson} + * A {@link FactoryBean} for creating a Google Gson 2.x {@link Gson} instance. * * @author Roy Clarkson * @since 4.1 @@ -43,6 +42,7 @@ public class GsonFactoryBean implements FactoryBean, BeanClassLoaderAware, private final Log logger = LogFactory.getLog(getClass()); + private Gson gson; private GsonBuilder gsonBuilder; @@ -61,15 +61,15 @@ public class GsonFactoryBean implements FactoryBean, BeanClassLoaderAware, /** - * Set the GsonBuilder instance to use. If not set, the GsonBuilder will be created - * using its default constructor. + * Set the GsonBuilder instance to use. If not set, the GsonBuilder will be + * created using its default constructor. */ public void setGsonBuilder(GsonBuilder gsonBuilder) { this.gsonBuilder = gsonBuilder; } /** - * Return the GsonBuilder instance being used. + * Return the configured GsonBuilder instance to use, if any. * @return the GsonBuilder instance */ public GsonBuilder getGsonBuilder() { @@ -77,8 +77,8 @@ public class GsonFactoryBean implements FactoryBean, BeanClassLoaderAware, } /** - * Whether to use the {@link GsonBuilder#setPrettyPrinting()} when writing JSON. This - * is a shortcut for setting up a {@code Gson} as follows: + * Whether to use the {@link GsonBuilder#setPrettyPrinting()} when writing + * JSON. This is a shortcut for setting up a {@code Gson} as follows: * *
 	 * new GsonBuilder().setPrettyPrinting().create();
@@ -89,8 +89,9 @@ public class GsonFactoryBean implements FactoryBean, BeanClassLoaderAware,
 	}
 
 	/**
-	 * Whether to use the {@link GsonBuilder#serializeNulls()} option when writing JSON.
-	 * This is a shortcut for setting up a {@code Gson} as follows:
+	 * Whether to use the {@link GsonBuilder#serializeNulls()} option when
+	 * writing JSON. This is a shortcut for setting up a {@code Gson} as
+	 * follows:
 	 *
 	 * 
 	 * new GsonBuilder().serializeNulls().create();
@@ -101,9 +102,9 @@ public class GsonFactoryBean implements FactoryBean, BeanClassLoaderAware,
 	}
 
 	/**
-	 * Whether to use the {@link GsonBuilder#disableHtmlEscaping()} when writing JSON. Set
-	 * to {@code true} to disable HTML escaping in JSON. This is a shortcut for setting up
-	 * a {@code Gson} as follows:
+	 * Whether to use the {@link GsonBuilder#disableHtmlEscaping()} when writing
+	 * JSON. Set to {@code true} to disable HTML escaping in JSON. This is a
+	 * shortcut for setting up a {@code Gson} as follows:
 	 *
 	 * 
 	 * new GsonBuilder().disableHtmlEscaping().create();
@@ -146,7 +147,8 @@ public class GsonFactoryBean implements FactoryBean, BeanClassLoaderAware,
 	 * writing JSON.
 	 *
 	 * 

When set to {@code true} a custom {@link com.google.gson.TypeAdapter} - * is registered via {@link GsonBuilder#registerTypeHierarchyAdapter(Class, Object)} + * is registered via + * {@link GsonBuilder#registerTypeHierarchyAdapter(Class, Object)} * that serializes a {@code byte[]} property to and from a Base64 encoded * string instead of a JSON array. * @@ -167,7 +169,7 @@ public class GsonFactoryBean implements FactoryBean, BeanClassLoaderAware, @Override public void afterPropertiesSet() throws Exception { - if (gsonBuilder == null) { + if (this.gsonBuilder == null) { this.gsonBuilder = new GsonBuilder(); } if (this.prettyPrint != null && this.prettyPrint) { @@ -188,14 +190,14 @@ public class GsonFactoryBean implements FactoryBean, BeanClassLoaderAware, } } else if (logger.isDebugEnabled()) { - logger.debug("org.apache.commons.codec.binary.Base64 is not available on the class path. Gson Base64 encoding is disabled."); + logger.debug("org.apache.commons.codec.binary.Base64 is not " + + "available on the class path. Gson Base64 encoding is disabled."); } this.gson = this.gsonBuilder.create(); } - /** - * Return the singleton Gson. + * Return the created Gson instance. */ @Override public Gson getObject() throws Exception { diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java index 0adcf4b0fe..27677f009e 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java @@ -76,18 +76,19 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverterSetting a custom-configured {@code Gson} is one way to take further * control of the JSON serialization process. */ public void setGson(Gson gson) { - Assert.notNull(gson, "Gson must not be null"); + Assert.notNull(gson, "'gson' is required"); this.gson = gson; } /** - * Return the underlying {@code GsonBuilder} for this converter. + * Return the configured {@code Gson} instance for this converter. */ public Gson getGson() { return this.gson; @@ -106,10 +107,11 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverterPrefixing the JSON string in this manner is used to help prevent JSON Hijacking. - * The prefix renders the string syntactically invalid as a script so that it cannot - * be hijacked. This prefix does not affect the evaluation of JSON, but if JSON - * validation is performed on the string, the prefix would need to be ignored. + *

Prefixing the JSON string in this manner is used to help prevent JSON + * Hijacking. The prefix renders the string syntactically invalid as a script + * so that it cannot be hijacked. This prefix does not affect the evaluation + * of JSON, but if JSON validation is performed on the string, the prefix + * would need to be ignored. * * @see #setJsonPrefix */ @@ -157,9 +159,10 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverterThe default implementation returns {@code TypeToken.get(type)}, but this can be - * overridden in subclasses to allow for custom generic collection handling. - * For instance: + * + *

The default implementation returns {@code TypeToken.get(type)}, but + * this can be overridden in subclasses to allow for custom generic + * collection handling. For instance: *

 	 * protected TypeToken getTypeToken(Type type) {
 	 *   if (type instanceof Class && List.class.isAssignableFrom((Class) type)) {
@@ -200,12 +203,11 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter result = (HashMap) converter.read(HashMap.class, inputMessage);
+		HashMap result = (HashMap) this.converter.read(HashMap.class, inputMessage);
 		assertEquals("Foo", result.get("string"));
 		Number n = (Number) result.get("number");
 		assertEquals(42, n.longValue());
@@ -116,7 +117,7 @@ public class GsonHttpMessageConverterTests {
 		body.setArray(new String[]{"Foo", "Bar"});
 		body.setBool(true);
 		body.setBytes(new byte[]{0x1, 0x2});
-		converter.write(body, null, outputMessage);
+		this.converter.write(body, null, outputMessage);
 		Charset utf8 = Charset.forName("UTF-8");
 		String result = outputMessage.getBodyAsString(utf8);
 		assertTrue(result.contains("\"string\":\"Foo\""));
@@ -135,7 +136,7 @@ public class GsonHttpMessageConverterTests {
 		MediaType contentType = new MediaType("application", "json", utf16);
 		MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
 		String body = "H\u00e9llo W\u00f6rld";
-		converter.write(body, contentType, outputMessage);
+		this.converter.write(body, contentType, outputMessage);
 		assertEquals("Invalid result", "\"" + body + "\"", outputMessage.getBodyAsString(utf16));
 		assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType());
 	}
@@ -145,14 +146,13 @@ public class GsonHttpMessageConverterTests {
 		String body = "FooBar";
 		MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
 		inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
-		converter.read(MyBean.class, inputMessage);
+		this.converter.read(MyBean.class, inputMessage);
 	}
 
 	@Test
 	@SuppressWarnings("unchecked")
 	public void readGenerics() throws IOException {
 		GsonHttpMessageConverter converter = new GsonHttpMessageConverter() {
-
 			@Override
 			protected TypeToken getTypeToken(Type type) {
 				if (type instanceof Class && List.class.isAssignableFrom((Class) type)) {
@@ -164,7 +164,8 @@ public class GsonHttpMessageConverterTests {
 				}
 			}
 		};
-		String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
+		String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
+				"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
 		MockHttpInputMessage inputMessage = new MockHttpInputMessage(
 				body.getBytes(UTF8));
 		inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
@@ -186,7 +187,8 @@ public class GsonHttpMessageConverterTests {
 		ParameterizedTypeReference> beansList = new ParameterizedTypeReference>() {
 		};
 
-		String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
+		String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
+				"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
 		MockHttpInputMessage inputMessage = new MockHttpInputMessage(
 				body.getBytes(UTF8));
 		inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
@@ -208,7 +210,6 @@ public class GsonHttpMessageConverterTests {
 		MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
 		this.converter.setPrefixJson(true);
 		this.converter.writeInternal("foo", outputMessage);
-
 		assertEquals("{} && \"foo\"", outputMessage.getBodyAsString(UTF8));
 	}
 
@@ -217,7 +218,6 @@ public class GsonHttpMessageConverterTests {
 		MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
 		this.converter.setJsonPrefix(")]}',");
 		this.converter.writeInternal("foo", outputMessage);
-
 		assertEquals(")]}',\"foo\"", outputMessage.getBodyAsString(UTF8));
 	}