This commit is contained in:
Rossen Stoyanchev
2014-05-21 18:11:49 -04:00
parent 811330f5c8
commit bc3ca2dea1
5 changed files with 60 additions and 59 deletions

View File

@@ -51,13 +51,12 @@ final class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer<byte[]>
@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));
}

View File

@@ -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<Gson>, BeanClassLoaderAware,
private final Log logger = LogFactory.getLog(getClass());
private Gson gson;
private GsonBuilder gsonBuilder;
@@ -61,15 +61,15 @@ public class GsonFactoryBean implements FactoryBean<Gson>, 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<Gson>, 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:
*
* <pre class="code">
* new GsonBuilder().setPrettyPrinting().create();
@@ -89,8 +89,9 @@ public class GsonFactoryBean implements FactoryBean<Gson>, 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:
*
* <pre class="code">
* new GsonBuilder().serializeNulls().create();
@@ -101,9 +102,9 @@ public class GsonFactoryBean implements FactoryBean<Gson>, 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:
*
* <pre class="code">
* new GsonBuilder().disableHtmlEscaping().create();
@@ -146,7 +147,8 @@ public class GsonFactoryBean implements FactoryBean<Gson>, BeanClassLoaderAware,
* writing JSON.
*
* <p>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<Gson>, 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<Gson>, 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 {

View File

@@ -76,18 +76,19 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
}
/**
* Set the {@code Gson} for this view.
* If not set, a default {@link Gson#Gson() Gson} is used.
* Set the {@code Gson} instance to use.
* If not set, a default {@link Gson#Gson() Gson} instance is used.
*
* <p>Setting 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 AbstractHttpMessageConverter<Objec
* Indicate whether the JSON output by this view should be prefixed with "{} &&".
* Default is {@code false}.
*
* <p>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.
* <p>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 AbstractHttpMessageConverter<Objec
/**
* Return the Gson {@link TypeToken} for the specified type.
* <p>The default implementation returns {@code TypeToken.get(type)}, but this can be
* overridden in subclasses to allow for custom generic collection handling.
* For instance:
*
* <p>The default implementation returns {@code TypeToken.get(type)}, but
* this can be overridden in subclasses to allow for custom generic
* collection handling. For instance:
* <pre class="code">
* protected TypeToken<?> getTypeToken(Type type) {
* if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) {
@@ -200,12 +203,11 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
Charset charset = getCharset(outputMessage.getHeaders());
OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset);
try {
if (this.jsonPrefix != null) {
writer.append(this.jsonPrefix);
}
gson.toJson(o, writer);
this.gson.toJson(o, writer);
writer.close();
}
catch(JsonIOException ex) {