Class JsonRepresentation


  • public class JsonRepresentation
    extends WriterRepresentation
    Representation based on a JSON document. JSON stands for JavaScript Object Notation and is a lightweight data-interchange format.
    See Also:
    JSON home
    • Field Detail

      • indenting

        private boolean indenting
        Indicates if JSON objects and arrays should be indented.
      • indentingSize

        private int indentingSize
        Number of spaces to use for indentation.
      • jsonRepresentation

        private Representation jsonRepresentation
        The wrapped JSON representation.
      • jsonValue

        private java.lang.Object jsonValue
        The wrapped JSON value.
    • Constructor Detail

      • JsonRepresentation

        public JsonRepresentation​(org.json.JSONArray jsonArray)
        Constructor from a JSON array.
        Parameters:
        jsonArray - The JSON array.
      • JsonRepresentation

        public JsonRepresentation​(org.json.JSONObject jsonObject)
        Constructor from a JSON object.
        Parameters:
        jsonObject - The JSON object.
      • JsonRepresentation

        public JsonRepresentation​(org.json.JSONStringer jsonStringer)
        Constructor from a JSON stringer.
        Parameters:
        jsonStringer - The JSON stringer.
      • JsonRepresentation

        public JsonRepresentation​(org.json.JSONTokener jsonTokener)
        Constructor from a JSON tokener.
        Parameters:
        jsonTokener - The JSON tokener.
      • JsonRepresentation

        public JsonRepresentation​(java.util.Map<java.lang.String,​java.lang.Object> map)
        Constructor from a map object.
        Parameters:
        map - The map to convert to JSON.
        See Also:
        JSONObject(Map)
      • JsonRepresentation

        public JsonRepresentation​(java.lang.Object bean)
        Constructor from a bean using reflection to generate JSON names.
        Parameters:
        bean - The bean to convert to JSON.
        See Also:
        JSONObject(Object)
      • JsonRepresentation

        public JsonRepresentation​(Representation jsonRepresentation)
                           throws java.io.IOException
        Constructor.
        Parameters:
        jsonRepresentation - A source JSON representation to parse.
        Throws:
        java.io.IOException
      • JsonRepresentation

        public JsonRepresentation​(java.lang.String jsonString)
        Constructor from a JSON string.
        Parameters:
        jsonString - The JSON string.
    • Method Detail

      • getIndentingSize

        public int getIndentingSize()
        Returns the number of spaces to use for indentation.
        Returns:
        The number of spaces to use for indentation.
      • getJsonArray

        public org.json.JSONArray getJsonArray()
                                        throws org.json.JSONException
        Gets the wrapped JSON array or converts the wrapped representation if needed.
        Returns:
        The converted JSON array.
        Throws:
        org.json.JSONException
      • getJsonObject

        public org.json.JSONObject getJsonObject()
                                          throws org.json.JSONException
        Gets the wrapped JSON object or converts the wrapped representation if needed.
        Returns:
        The converted JSON object.
        Throws:
        org.json.JSONException
      • getJsonText

        private java.lang.String getJsonText()
                                      throws org.json.JSONException
        Returns the JSON text for the wrapped JSON object or representation.
        Returns:
        The JSON text.
        Throws:
        org.json.JSONException
      • getJsonTokener

        public org.json.JSONTokener getJsonTokener()
                                            throws org.json.JSONException
        Gets the wrapped JSON tokener or converts the wrapped representation if needed.
        Returns:
        The converted JSON tokener.
        Throws:
        org.json.JSONException
      • getSize

        public long getSize()
        Description copied from class: Representation
        Returns the total size in bytes if known, UNKNOWN_SIZE (-1) otherwise. When ranges are used, this might not be the actual size available. For this purpose, you can use the Representation.getAvailableSize() method.

        Note that when used with HTTP connectors, this property maps to the "Content-Length" header.
        Overrides:
        getSize in class Representation
        Returns:
        The size in bytes if known, UNKNOWN_SIZE (-1) otherwise.
        See Also:
        Representation.isEmpty()
      • init

        private void init​(java.lang.Object jsonObject)
        Parameters:
        jsonObject -
      • isIndenting

        public boolean isIndenting()
        Indicates if JSON objects and arrays should be indented.
        Returns:
        True if JSON objects and arrays should be indented.
      • setIndenting

        public void setIndenting​(boolean indenting)
        Indicates if JSON objects and arrays should be indented.
        Parameters:
        indenting - True if JSON objects and arrays should be indented.
      • setIndentingSize

        public void setIndentingSize​(int indentFactor)
        Sets the number of spaces to use for indentation.
        Parameters:
        indentFactor - The number of spaces to use for indentation.
      • 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