Package org.restlet.ext.json
Class JsonRepresentation
- java.lang.Object
-
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 Summary
Fields Modifier and Type Field Description private booleanindentingIndicates if JSON objects and arrays should be indented.private intindentingSizeNumber of spaces to use for indentation.private RepresentationjsonRepresentationThe wrapped JSON representation.private java.lang.ObjectjsonValueThe wrapped JSON value.-
Fields inherited from class org.restlet.representation.Representation
UNKNOWN_SIZE
-
-
Constructor Summary
Constructors Constructor Description JsonRepresentation(java.lang.Object bean)Constructor from a bean using reflection to generate JSON names.JsonRepresentation(java.lang.String jsonString)Constructor from a JSON string.JsonRepresentation(java.util.Map<java.lang.String,java.lang.Object> map)Constructor from a map object.JsonRepresentation(org.json.JSONArray jsonArray)Constructor from a JSON array.JsonRepresentation(org.json.JSONObject jsonObject)Constructor from a JSON object.JsonRepresentation(org.json.JSONStringer jsonStringer)Constructor from a JSON stringer.JsonRepresentation(org.json.JSONTokener jsonTokener)Constructor from a JSON tokener.JsonRepresentation(Representation jsonRepresentation)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetIndentingSize()Returns the number of spaces to use for indentation.org.json.JSONArraygetJsonArray()Gets the wrapped JSON array or converts the wrapped representation if needed.org.json.JSONObjectgetJsonObject()Gets the wrapped JSON object or converts the wrapped representation if needed.private java.lang.StringgetJsonText()Returns the JSON text for the wrapped JSON object or representation.org.json.JSONTokenergetJsonTokener()Gets the wrapped JSON tokener or converts the wrapped representation if needed.longgetSize()Returns the total size in bytes if known, UNKNOWN_SIZE (-1) otherwise.private voidinit(java.lang.Object jsonObject)booleanisIndenting()Indicates if JSON objects and arrays should be indented.voidsetIndenting(boolean indenting)Indicates if JSON objects and arrays should be indented.voidsetIndentingSize(int indentFactor)Sets the number of spaces to use for indentation.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, 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
-
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.JSONExceptionGets 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.JSONExceptionGets 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.JSONExceptionReturns 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.JSONExceptionGets 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:RepresentationReturns 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 theRepresentation.getAvailableSize()method.
Note that when used with HTTP connectors, this property maps to the "Content-Length" header.- Overrides:
getSizein classRepresentation- 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.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
-
-