|
Spring for Android | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
org.springframework.http.converter.json.GsonHttpMessageConverter
public class GsonHttpMessageConverter
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.
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 |
|---|
public static final java.nio.charset.Charset DEFAULT_CHARSET
| Constructor Detail |
|---|
public GsonHttpMessageConverter()
GsonHttpMessageConverter.
| Method Detail |
|---|
public void setGson(Gson gson)
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.
public Gson getGson()
Gson instance for this converter.
public void setJsonPrefix(java.lang.String jsonPrefix)
setPrefixJson(boolean)public void setPrefixJson(boolean prefixJson)
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.
setJsonPrefix(java.lang.String)
public boolean canRead(java.lang.Class<?> clazz,
MediaType mediaType)
AbstractHttpMessageConverter
canRead in interface HttpMessageConverter<java.lang.Object>canRead in class AbstractHttpMessageConverter<java.lang.Object>clazz - the class to test for readabilitymediaType - the media type to read, can be null if not specified.
Typically the value of a Content-Type header.
true if readable; false otherwise
public boolean canRead(java.lang.reflect.Type type,
java.lang.Class<?> contextClass,
MediaType mediaType)
GenericHttpMessageConverter
canRead in interface GenericHttpMessageConverter<java.lang.Object>type - the type to test for readabilitycontextClass - a context class for the target type, for example a class
in which the target type appears in a method signature, can be nullmediaType - the media type to read, can be null if not specified.
Typically the value of a Content-Type header.
true if readable; false otherwise
public boolean canWrite(java.lang.Class<?> clazz,
MediaType mediaType)
AbstractHttpMessageConverter
canWrite in interface HttpMessageConverter<java.lang.Object>canWrite in class AbstractHttpMessageConverter<java.lang.Object>clazz - the class to test for writabilitymediaType - the media type to write, can be null if not specified.
Typically the value of an Accept header.
true if writable; false otherwiseprotected boolean supports(java.lang.Class<?> clazz)
AbstractHttpMessageConverter
supports in class AbstractHttpMessageConverter<java.lang.Object>clazz - the class to test for support
true if supported; false otherwise
protected java.lang.Object readInternal(java.lang.Class<?> clazz,
HttpInputMessage inputMessage)
throws java.io.IOException,
HttpMessageNotReadableException
AbstractHttpMessageConverterAbstractHttpMessageConverter.read(java.lang.Class extends T>, org.springframework.http.HttpInputMessage).
readInternal in class AbstractHttpMessageConverter<java.lang.Object>clazz - the type of object to returninputMessage - the HTTP input message to read from
java.io.IOException - in case of I/O errors
HttpMessageNotReadableException - in case of conversion errors
public java.lang.Object read(java.lang.reflect.Type type,
java.lang.Class<?> contextClass,
HttpInputMessage inputMessage)
throws java.io.IOException,
HttpMessageNotReadableException
GenericHttpMessageConverter
read in interface GenericHttpMessageConverter<java.lang.Object>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 nullinputMessage - the HTTP input message to read from
java.io.IOException - in case of I/O errors
HttpMessageNotReadableException - in case of conversion errorsprotected TypeToken<?> getTypeToken(java.lang.reflect.Type type)
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);
}
}
type - the type for which to return the TypeToken
protected void writeInternal(java.lang.Object o,
HttpOutputMessage outputMessage)
throws java.io.IOException,
HttpMessageNotWritableException
AbstractHttpMessageConverterAbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).
writeInternal in class AbstractHttpMessageConverter<java.lang.Object>o - the object to write to the output messageoutputMessage - the HTTP output message to write to
java.io.IOException - in case of I/O errors
HttpMessageNotWritableException - in case of conversion errors
|
Spring for Android | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||