Spring for Android

org.springframework.http.converter.json
Class GsonHttpMessageConverter

java.lang.Object
  extended by org.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
      extended by org.springframework.http.converter.json.GsonHttpMessageConverter
All Implemented Interfaces:
GenericHttpMessageConverter<java.lang.Object>, HttpMessageConverter<java.lang.Object>

public class GsonHttpMessageConverter
extends AbstractHttpMessageConverter<java.lang.Object>
implements GenericHttpMessageConverter<java.lang.Object>

Implementation of HttpMessageConverter that can read and write JSON using the Google Gson library's Gson class.

This converter can be used to bind to typed beans or untyped HashMaps. By default, it supports application/json and application/*+json.

Tested against Gson 2.3; compatible with Gson 2.0 and higher.

Since:
1.0
Author:
Roy Clarkson
See Also:
setGson(com.google.gson.Gson), AbstractHttpMessageConverter.setSupportedMediaTypes(java.util.List)

Field Summary
static java.nio.charset.Charset DEFAULT_CHARSET
           
 
Constructor Summary
GsonHttpMessageConverter()
          Construct a new GsonHttpMessageConverter.
 
Method Summary
 boolean canRead(java.lang.Class<?> clazz, MediaType mediaType)
          This implementation checks if the given class is supported, and if the supported media types include the given media type.
 boolean canRead(java.lang.reflect.Type type, java.lang.Class<?> contextClass, MediaType mediaType)
          Indicates whether the given type can be read by this converter.
 boolean canWrite(java.lang.Class<?> clazz, MediaType mediaType)
          This implementation checks if the given class is supported, and if the supported media types include the given media type.
 Gson getGson()
          Return the configured Gson instance for this converter.
protected  TypeToken<?> getTypeToken(java.lang.reflect.Type type)
          Return the Gson TypeToken for the specified type.
 java.lang.Object read(java.lang.reflect.Type type, java.lang.Class<?> contextClass, HttpInputMessage inputMessage)
          Read an object of the given type form the given input message, and returns it.
protected  java.lang.Object readInternal(java.lang.Class<?> clazz, HttpInputMessage inputMessage)
          Abstract template method that reads the actual object.
 void setGson(Gson gson)
          Set the Gson instance to use.
 void setJsonPrefix(java.lang.String jsonPrefix)
          Specify a custom prefix to use for JSON output.
 void setPrefixJson(boolean prefixJson)
          Indicate whether the JSON output by this view should be prefixed with "{} &&".
protected  boolean supports(java.lang.Class<?> clazz)
          Indicates whether the given class is supported by this converter.
protected  void writeInternal(java.lang.Object o, HttpOutputMessage outputMessage)
          Abstract template method that writes the actual body.
 
Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
canRead, canWrite, getContentLength, getDefaultContentType, getSupportedMediaTypes, read, setSupportedMediaTypes, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.http.converter.HttpMessageConverter
getSupportedMediaTypes, read, write
 

Field Detail

DEFAULT_CHARSET

public static final java.nio.charset.Charset DEFAULT_CHARSET
Constructor Detail

GsonHttpMessageConverter

public GsonHttpMessageConverter()
Construct a new GsonHttpMessageConverter.

Method Detail

setGson

public void setGson(Gson gson)
Set the Gson instance to use. If not set, a default Gson instance is used.

Setting a custom-configured Gson is one way to take further control of the JSON serialization process.


getGson

public Gson getGson()
Return the configured Gson instance for this converter.


setJsonPrefix

public void setJsonPrefix(java.lang.String jsonPrefix)
Specify a custom prefix to use for JSON output. Default is none.

See Also:
setPrefixJson(boolean)

setPrefixJson

public void setPrefixJson(boolean prefixJson)
Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false.

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 Also:
setJsonPrefix(java.lang.String)

canRead

public boolean canRead(java.lang.Class<?> clazz,
                       MediaType mediaType)
Description copied from class: AbstractHttpMessageConverter
This implementation checks if the given class is supported, and if the supported media types include the given media type.

Specified by:
canRead in interface HttpMessageConverter<java.lang.Object>
Overrides:
canRead in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the class to test for readability
mediaType - the media type to read, can be null if not specified. Typically the value of a Content-Type header.
Returns:
true if readable; false otherwise

canRead

public boolean canRead(java.lang.reflect.Type type,
                       java.lang.Class<?> contextClass,
                       MediaType mediaType)
Description copied from interface: GenericHttpMessageConverter
Indicates whether the given type can be read by this converter.

Specified by:
canRead in interface GenericHttpMessageConverter<java.lang.Object>
Parameters:
type - the type to test for readability
contextClass - a context class for the target type, for example a class in which the target type appears in a method signature, can be null
mediaType - the media type to read, can be null if not specified. Typically the value of a Content-Type header.
Returns:
true if readable; false otherwise

canWrite

public boolean canWrite(java.lang.Class<?> clazz,
                        MediaType mediaType)
Description copied from class: AbstractHttpMessageConverter
This implementation checks if the given class is supported, and if the supported media types include the given media type.

Specified by:
canWrite in interface HttpMessageConverter<java.lang.Object>
Overrides:
canWrite in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the class to test for writability
mediaType - the media type to write, can be null if not specified. Typically the value of an Accept header.
Returns:
true if writable; false otherwise

supports

protected boolean supports(java.lang.Class<?> clazz)
Description copied from class: AbstractHttpMessageConverter
Indicates whether the given class is supported by this converter.

Specified by:
supports in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the class to test for support
Returns:
true if supported; false otherwise

readInternal

protected java.lang.Object readInternal(java.lang.Class<?> clazz,
                                        HttpInputMessage inputMessage)
                                 throws java.io.IOException,
                                        HttpMessageNotReadableException
Description copied from class: AbstractHttpMessageConverter
Abstract template method that reads the actual object. Invoked from AbstractHttpMessageConverter.read(java.lang.Class, org.springframework.http.HttpInputMessage).

Specified by:
readInternal in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the type of object to return
inputMessage - the HTTP input message to read from
Returns:
the converted object
Throws:
java.io.IOException - in case of I/O errors
HttpMessageNotReadableException - in case of conversion errors

read

public java.lang.Object read(java.lang.reflect.Type type,
                             java.lang.Class<?> contextClass,
                             HttpInputMessage inputMessage)
                      throws java.io.IOException,
                             HttpMessageNotReadableException
Description copied from interface: GenericHttpMessageConverter
Read an object of the given type form the given input message, and returns it.

Specified by:
read in interface GenericHttpMessageConverter<java.lang.Object>
Parameters:
type - the type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.
contextClass - a context class for the target type, for example a class in which the target type appears in a method signature, can be null
inputMessage - the HTTP input message to read from
Returns:
the converted object
Throws:
java.io.IOException - in case of I/O errors
HttpMessageNotReadableException - in case of conversion errors

getTypeToken

protected TypeToken<?> getTypeToken(java.lang.reflect.Type type)
Return the Gson TypeToken for the specified type.

The default implementation returns 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)) {
     return new TypeToken>() {};
   }
   else {
     return super.getTypeToken(type);
   }
 }
 

Parameters:
type - the type for which to return the TypeToken
Returns:
the type token

writeInternal

protected void writeInternal(java.lang.Object o,
                             HttpOutputMessage outputMessage)
                      throws java.io.IOException,
                             HttpMessageNotWritableException
Description copied from class: AbstractHttpMessageConverter
Abstract template method that writes the actual body. Invoked from AbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).

Specified by:
writeInternal in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
o - the object to write to the output message
outputMessage - the HTTP output message to write to
Throws:
java.io.IOException - in case of I/O errors
HttpMessageNotWritableException - in case of conversion errors

Spring for Android