Search in sources :

Example 1 with JSONAware

use of org.json.simple.JSONAware in project alfresco-repository by Alfresco.

the class JSONConversionComponent method propertyToJSON.

/**
 * Handles the work of converting values to JSON.
 *
 * @param nodeRef NodeRef
 * @param propertyName QName
 * @param key String
 * @param value Serializable
 * @return the JSON value
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object propertyToJSON(final NodeRef nodeRef, final QName propertyName, final String key, final Serializable value) {
    if (value != null) {
        // Has a decorator has been registered for this property?
        if (propertyDecorators.containsKey(propertyName)) {
            JSONAware jsonAware = propertyDecorators.get(propertyName).decorate(propertyName, nodeRef, value);
            if (jsonAware != null) {
                return jsonAware;
            }
        } else {
            // Built-in data type processing
            if (value instanceof Date) {
                JSONObject dateObj = new JSONObject();
                dateObj.put("value", JSONObject.escape(value.toString()));
                dateObj.put("iso8601", JSONObject.escape(ISO8601DateFormat.format((Date) value)));
                return dateObj;
            } else if (value instanceof List) {
                // Convert the List to a JSON list by recursively calling propertyToJSON
                List<Object> jsonList = new ArrayList<Object>(((List<Serializable>) value).size());
                for (Serializable listItem : (List<Serializable>) value) {
                    jsonList.add(propertyToJSON(nodeRef, propertyName, key, listItem));
                }
                return jsonList;
            } else if (value instanceof Double) {
                return (Double.isInfinite((Double) value) || Double.isNaN((Double) value) ? null : value.toString());
            } else if (value instanceof Float) {
                return (Float.isInfinite((Float) value) || Float.isNaN((Float) value) ? null : value.toString());
            } else {
                return value.toString();
            }
        }
    }
    return null;
}
Also used : Serializable(java.io.Serializable) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.simple.JSONObject) JSONAware(org.json.simple.JSONAware) Date(java.util.Date)

Aggregations

Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 JSONAware (org.json.simple.JSONAware)1 JSONObject (org.json.simple.JSONObject)1