org.alfresco.json
Class JSONUtil

java.lang.Object
  extended by org.alfresco.json.JSONUtil

public class JSONUtil
extends Object

Utilities for JSON-parsing.

Author:
Frederik Heremans, Michael Suzuki

Field Summary
static String FORM_PROCESSOR_JSON_PERSISTED_OBJECT
           
static String HEADER_ACCEPT
           
static String HEADER_CONTENT_TYPE
           
static String JSON_DATA
           
static String JSON_ID
           
static String JSON_ITEMS
           
static String JSON_NAME
           
static String MIME_TYPE_JSON
           
 
Constructor Summary
JSONUtil()
           
 
Method Summary
static org.json.simple.JSONArray getArray(org.json.simple.JSONObject json, String key)
           
static org.json.simple.JSONArray getDataArrayFromResponse(org.apache.http.HttpEntity method)
          Extract the "data" JSON-array from the method's response.
static org.json.simple.JSONObject getDataFromResponse(org.apache.http.HttpEntity method)
          Extract the "data" JSON-object from the method's response.
static org.json.simple.JSONObject getObject(org.json.simple.JSONObject json, String key)
           
static String getString(org.json.simple.JSONObject json, String key, String defaultValue)
          Gets a string-value from the given JSON-object for the given key.
static void populateRequestBody(org.apache.http.client.methods.HttpPost method, org.json.simple.JSONObject object)
          Adds the JSON as request-body the the method and sets the correct content-type.
static org.json.simple.JSONObject readStream(org.apache.http.HttpEntity entity)
          Parses http response stream into a JSONObject.
static void setJSONExpected(org.apache.http.client.methods.HttpPost method)
          Sets a header to notify server we expect a JSON-repsponse for this method.
static org.apache.http.entity.StringEntity setMessageBody(org.json.simple.JSONObject json)
          Populate HTTP message call with given content.
static org.apache.http.entity.StringEntity setMessageBody(String content)
          Populate HTTP message call with given content.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JSON_DATA

public static final String JSON_DATA
See Also:
Constant Field Values

JSON_ID

public static final String JSON_ID
See Also:
Constant Field Values

JSON_ITEMS

public static final String JSON_ITEMS
See Also:
Constant Field Values

JSON_NAME

public static final String JSON_NAME
See Also:
Constant Field Values

FORM_PROCESSOR_JSON_PERSISTED_OBJECT

public static final String FORM_PROCESSOR_JSON_PERSISTED_OBJECT
See Also:
Constant Field Values

MIME_TYPE_JSON

public static final String MIME_TYPE_JSON
See Also:
Constant Field Values

HEADER_ACCEPT

public static final String HEADER_ACCEPT
See Also:
Constant Field Values

HEADER_CONTENT_TYPE

public static final String HEADER_CONTENT_TYPE
See Also:
Constant Field Values
Constructor Detail

JSONUtil

public JSONUtil()
Method Detail

setJSONExpected

public static void setJSONExpected(org.apache.http.client.methods.HttpPost method)
Sets a header to notify server we expect a JSON-repsponse for this method.

Parameters:
method -

populateRequestBody

public static void populateRequestBody(org.apache.http.client.methods.HttpPost method,
                                       org.json.simple.JSONObject object)
Adds the JSON as request-body the the method and sets the correct content-type.

Parameters:
method -
object -

getDataFromResponse

public static org.json.simple.JSONObject getDataFromResponse(org.apache.http.HttpEntity method)
Extract the "data" JSON-object from the method's response.

Parameters:
method - the method containing the response
Returns:
the "data" object. Returns null if response is not JSON or no data-object is present.

getDataArrayFromResponse

public static org.json.simple.JSONArray getDataArrayFromResponse(org.apache.http.HttpEntity method)
Extract the "data" JSON-array from the method's response.

Parameters:
method - the method containing the response
Returns:
the "data" object. Returns null if response is not JSON or no data-object is present.

getString

public static String getString(org.json.simple.JSONObject json,
                               String key,
                               String defaultValue)
Gets a string-value from the given JSON-object for the given key.

Parameters:
json - the json object
key - key pointing to the value
defaultValue - if value is not set or if value is not of type "String", this value is returned

getArray

public static org.json.simple.JSONArray getArray(org.json.simple.JSONObject json,
                                                 String key)
Parameters:
json - JSON to extract array from
key - key under which array is present on JSON
Returns:
the JSONArray. Returns null, if the value is null or not an array.

getObject

public static org.json.simple.JSONObject getObject(org.json.simple.JSONObject json,
                                                   String key)
Parameters:
json - JSON to extract object from
key - key under which object is present on JSON
Returns:
the JSONObject. Returns null, if the value is null or not an object.

setMessageBody

public static org.apache.http.entity.StringEntity setMessageBody(String content)
                                                          throws UnsupportedEncodingException
Populate HTTP message call with given content.

Parameters:
content - String content
Returns:
StringEntity content.
Throws:
UnsupportedEncodingException - if unsupported

setMessageBody

public static org.apache.http.entity.StringEntity setMessageBody(org.json.simple.JSONObject json)
                                                          throws UnsupportedEncodingException
Populate HTTP message call with given content.

Parameters:
json - JSONObject content
Returns:
StringEntity content.
Throws:
UnsupportedEncodingException - if unsupported

readStream

public static org.json.simple.JSONObject readStream(org.apache.http.HttpEntity entity)
Parses http response stream into a JSONObject.

Parameters:
stream - Http response entity
Returns:
JSONObject response


Copyright © 2012. All Rights Reserved.