Package org.restlet.ext.gson
Class GsonRepresentation<T>
- java.lang.Object
-
public class GsonRepresentation<T> extends WriterRepresentation
Representation based on a JSON document. JSON stands for JavaScript Object Notation and is a lightweight data-interchange format.- See Also:
- Gson project
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classGsonRepresentation.ISODateDeserializerCustom deserializer forDateinstances.private classGsonRepresentation.ISODateSerializerCustom serializer forDateinstances.
-
Field Summary
Fields Modifier and Type Field Description private com.google.gson.GsonBuilderbuilderThe modifiable Gson builder.private RepresentationjsonRepresentationThe JSON representation to parse.private TobjectThe (parsed) object to format.private java.lang.Class<T>objectClassThe object class to instantiate.-
Fields inherited from class org.restlet.representation.Representation
UNKNOWN_SIZE
-
-
Constructor Summary
Constructors Constructor Description GsonRepresentation(Representation representation, java.lang.Class<T> objectClass)Constructor.GsonRepresentation(T object)Constructor for the JSON media type.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected com.google.gson.GsonBuildercreateBuilder()Returns a new instance of the builder for Gson instances.com.google.gson.GsonBuildergetBuilder()Returns the builder for Gson instances.TgetObject()Returns the wrapped object, deserializing the representation with Gson if necessary.java.lang.Class<T>getObjectClass()Returns the object class to instantiate.voidsetBuilder(com.google.gson.GsonBuilder builder)Sets the Gson builder.voidsetObject(T object)Sets the object to format.voidsetObjectClass(java.lang.Class<T> objectClass)Sets the object class to instantiate.voidwrite(java.io.Writer writer)Writes the representation to a characters writer.-
Methods inherited from class org.restlet.representation.WriterRepresentation
getReader
-
Methods inherited from class org.restlet.representation.CharacterRepresentation
getChannel, getStream, write, write
-
Methods inherited from class org.restlet.representation.Representation
append, exhaust, getAvailableSize, getDigest, getDisposition, getExpirationDate, getRange, getRegistration, getSize, getText, hasKnownSize, isAvailable, isEmpty, isSelectable, isTransient, release, setAvailable, setDigest, setDisposition, setExpirationDate, setListener, setRange, setSize, setTransient
-
Methods inherited from class org.restlet.representation.RepresentationInfo
getModificationDate, getTag, setModificationDate, setTag
-
Methods inherited from class org.restlet.representation.Variant
createClientInfo, equals, getCharacterSet, getEncodings, getLanguages, getLocationRef, getMediaType, hashCode, includes, isCompatible, setCharacterSet, setEncodings, setLanguages, setLocationRef, setLocationRef, setMediaType, toString
-
-
-
-
Field Detail
-
builder
private com.google.gson.GsonBuilder builder
The modifiable Gson builder.
-
jsonRepresentation
private Representation jsonRepresentation
The JSON representation to parse.
-
object
private T object
The (parsed) object to format.
-
objectClass
private java.lang.Class<T> objectClass
The object class to instantiate.
-
-
Constructor Detail
-
GsonRepresentation
public GsonRepresentation(Representation representation, java.lang.Class<T> objectClass)
Constructor.- Parameters:
representation- The representation to parse.objectClass- The object class to instantiate.
-
GsonRepresentation
public GsonRepresentation(T object)
Constructor for the JSON media type.- Parameters:
object- The object to format.
-
-
Method Detail
-
createBuilder
protected com.google.gson.GsonBuilder createBuilder()
Returns a new instance of the builder for Gson instances.- Returns:
- a new instance of builder for Gson instances.
-
getBuilder
public com.google.gson.GsonBuilder getBuilder()
Returns the builder for Gson instances.- Returns:
- The builder for Gson instances.
-
getObject
public T getObject() throws java.io.IOException
Returns the wrapped object, deserializing the representation with Gson if necessary.- Returns:
- The wrapped object.
- Throws:
java.io.IOException
-
getObjectClass
public java.lang.Class<T> getObjectClass()
Returns the object class to instantiate.- Returns:
- The object class to instantiate.
-
setBuilder
public void setBuilder(com.google.gson.GsonBuilder builder)
Sets the Gson builder.- Parameters:
builder- The Gson builder.
-
setObject
public void setObject(T object)
Sets the object to format.- Parameters:
object- The object to format.
-
setObjectClass
public void setObjectClass(java.lang.Class<T> objectClass)
Sets the object class to instantiate.- Parameters:
objectClass- The object class to instantiate.
-
write
public void write(java.io.Writer writer) throws java.io.IOExceptionDescription copied from class:RepresentationWrites the representation to a characters writer. This method is ensured to write the full content for each invocation unless it is a transient representation, in which case an exception is thrown.
Note that the class implementing this method shouldn't flush or close the givenWriterafter writing to it as this will be handled by the Restlet connectors automatically.- Specified by:
writein classRepresentation- Parameters:
writer- The characters writer.- Throws:
java.io.IOException
-
-