Package ej.rest.web
Class JSONResource
- java.lang.Object
-
- ej.rest.web.Resty
-
- ej.rest.web.AbstractResource
-
- ej.rest.web.JSONResource
-
public class JSONResource extends AbstractResource
A resource presentation in JSON format. You can ask Resty to parse the JSON into a JSONArray or a JSONObject. The JSONObject is similar to org.json.JSONObject and allows full access to the JSON. You can also access the JSON with a JSONPathQuery to extract only the parts you are interested in.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class ej.rest.web.Resty
Resty.Option, Resty.Timeout
-
-
Field Summary
-
Fields inherited from class ej.rest.web.AbstractResource
inputStream, urlConnection
-
Fields inherited from class ej.rest.web.Resty
DEFAULT_USER_AGENT, MOZILLA, userAgent
-
-
Constructor Summary
Constructors Constructor Description JSONResource(Resty.Option... options)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JSONArrayarray()Parse and return JSON array.Objectget(JSONPathQuery aQuery)Gets the partial JSON object or attribute as specified in the path expression.Objectget(String path)Gets the partial JSON object or attribute as specified in the path expression.StringgetAcceptedTypes()JSONResourcejson(JSONPathQuery path)Execute the given path query on the json GET the returned URI expecting JSONJSONResourcejson(JSONPathQuery path, Content content)Execute the given path query on the json and POST to the returned URI expecting JSONJSONObjectobject()Parse and return JSON object.TextResourcetext(JSONPathQuery path)Execute the given path query on the json and use the returned string as an URI expecting text/*JSONObjecttoObject()Added for compatibility with Scala.protected Objectunmarshal()Transforming the JSON-
Methods inherited from class ej.rest.web.AbstractResource
getUrlConnection, http, location, printResponseHeaders, status, stream
-
Methods inherited from class ej.rest.web.Resty
addAdditionalHeaders, addStandardHeaders, bytes, bytes, bytes, bytes, chunked, chunked, content, content, content, content, createBinaryResource, createJSONResource, createTextResource, data, data, delete, doGET, dontSend, doPOSTOrPUT, fillResourceFromURL, form, form, getAdditionalHeaders, identifyAsMozilla, identifyAsResty, json, json, json, json, openConnection, path, put, setOptions, text, text, text, text, withHeader
-
-
-
-
Constructor Detail
-
JSONResource
public JSONResource(Resty.Option... options)
-
-
Method Detail
-
array
public JSONArray array() throws IOException, JSONException
Parse and return JSON array. Parsing is done only once after which the inputStream is at EOF.- Throws:
IOExceptionJSONException
-
object
public JSONObject object() throws IOException, JSONException
Parse and return JSON object. Parsing is done only once after which the inputStrem is at EOF.- Returns:
- the JSON object
- Throws:
IOExceptionJSONException
-
toObject
public JSONObject toObject() throws IOException, JSONException
Added for compatibility with Scala. See Issue #2 at github.- Returns:
- the JSONObject presentation
- Throws:
IOExceptionJSONException- if data was no valid JSON
-
unmarshal
protected Object unmarshal() throws IOException, JSONException
Transforming the JSON- Throws:
IOExceptionJSONException
-
json
public JSONResource json(JSONPathQuery path) throws Exception
Execute the given path query on the json GET the returned URI expecting JSON- Parameters:
path- path to the URI to follow- Returns:
- a new resource, as a result of getting it from the server in JSON format
- Throws:
ExceptionJSONException
-
json
public JSONResource json(JSONPathQuery path, Content content) throws Exception
Execute the given path query on the json and POST to the returned URI expecting JSON- Parameters:
path- path to the URI to follow- Returns:
- a new resource, as a result of getting it from the server in JSON format
- Throws:
ExceptionJSONException
-
text
public TextResource text(JSONPathQuery path) throws Exception
Execute the given path query on the json and use the returned string as an URI expecting text/*- Parameters:
path- path to the URI to follow- Returns:
- a new resource, as a result of getting it from the server in text/plain format
- Throws:
ExceptionJSONException
-
get
public Object get(String path) throws Exception
Gets the partial JSON object or attribute as specified in the path expression.- Throws:
Exception
-
get
public Object get(JSONPathQuery aQuery) throws Exception
Gets the partial JSON object or attribute as specified in the path expression.- Throws:
Exception
-
getAcceptedTypes
public String getAcceptedTypes()
- Specified by:
getAcceptedTypesin classAbstractResource
-
-