Class GsonRepresentation<T>


  • 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
    • 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.IOException
        Description copied from class: Representation
        Writes 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 given Writer after writing to it as this will be handled by the Restlet connectors automatically.
        Specified by:
        write in class Representation
        Parameters:
        writer - The characters writer.
        Throws:
        java.io.IOException