Search in sources :

Example 1 with HasJSONProperties

use of com.openmeap.json.HasJSONProperties in project OpenMEAP by OpenMEAP.

the class JSONObjectBuilder method toJSON.

public JSONObject toJSON(Object obj) throws JSONException {
    if (obj == null) {
        return null;
    }
    if (!HasJSONProperties.class.isAssignableFrom(obj.getClass())) {
        throw new RuntimeException("The rootObject being converted to JSON must implement the HasJSONProperties interface.");
    }
    JSONProperty[] properties = ((HasJSONProperties) obj).getJSONProperties();
    JSONObject jsonObj = new JSONObject();
    // iterate over each JSONProperty annotated method
    for (int jsonPropertyIdx = 0; jsonPropertyIdx < properties.length; jsonPropertyIdx++) {
        JSONProperty property = properties[jsonPropertyIdx];
        // determine the method return type
        Class returnType = property.getReturnType();
        Object value = property.getGetterSetter().getValue(obj);
        if (value == null) {
            continue;
        }
        if (returnType == null) {
            throw new JSONException(obj.getClass().getName() + "." + property.getPropertyName() + " is annotated with JSONProperty, but has no return type." + "  I can't work with this.");
        }
        // strip "get" off the front
        String propertyName = property.getPropertyName();
        try {
            if (Enum.class.isAssignableFrom(returnType)) {
                Enum ret = (Enum) value;
                jsonObj.put(propertyName, ret.value());
            } else if (isSimpleType(returnType)) {
                jsonObj.put(propertyName, handleSimpleType(returnType, property.getGetterSetter().getValue(obj)));
            } else {
                if (returnType.isArray()) {
                    Object[] returnValues = (Object[]) value;
                    JSONArray jsonArray = new JSONArray();
                    for (int returnValueIdx = 0; returnValueIdx < returnValues.length; returnValueIdx++) {
                        Object thisValue = returnValues[returnValueIdx];
                        jsonArray.put(toJSON(thisValue));
                    }
                    jsonObj.put(propertyName, jsonArray);
                } else if (Hashtable.class.isAssignableFrom(returnType)) {
                    Hashtable map = (Hashtable) value;
                    JSONObject jsonMap = new JSONObject();
                    Enumeration enumer = map.keys();
                    while (enumer.hasMoreElements()) {
                        Object key = (String) enumer.nextElement();
                        Object thisValue = (Object) map.get(key);
                        if (isSimpleType(thisValue.getClass())) {
                            jsonMap.put(key.toString(), handleSimpleType(returnType, thisValue));
                        } else {
                            jsonMap.put(key.toString(), toJSON(thisValue));
                        }
                    }
                    jsonObj.put(propertyName, jsonMap);
                } else if (Vector.class.isAssignableFrom(returnType)) {
                    Vector returnValues = (Vector) property.getGetterSetter().getValue(obj);
                    JSONArray jsonArray = new JSONArray();
                    int size = returnValues.size();
                    for (int returnValueIdx = 0; returnValueIdx < size; returnValueIdx++) {
                        Object thisValue = returnValues.elementAt(returnValueIdx);
                        if (isSimpleType(property.getContainedType())) {
                            jsonArray.put(thisValue);
                        } else {
                            jsonArray.put(toJSON(thisValue));
                        }
                    }
                    jsonObj.put(propertyName, jsonArray);
                } else {
                    jsonObj.put(propertyName, toJSON(value));
                }
            }
        } catch (Exception ite) {
            throw new JSONException(ite);
        }
    }
    return jsonObj;
}
Also used : Enum(com.openmeap.json.Enum) Enumeration(java.util.Enumeration) HasJSONProperties(com.openmeap.json.HasJSONProperties) Hashtable(java.util.Hashtable) JSONArray(com.openmeap.thirdparty.org.json.me.JSONArray) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Vector(java.util.Vector)

Example 2 with HasJSONProperties

use of com.openmeap.json.HasJSONProperties in project OpenMEAP by OpenMEAP.

the class JSONObjectBuilder method fromJSON.

public Object fromJSON(JSONObject jsonObj, Object rootObject) throws JSONException {
    if (jsonObj == null) {
        return null;
    }
    if (!HasJSONProperties.class.isAssignableFrom(rootObject.getClass())) {
        throw new RuntimeException("The rootObject being converted to JSON must implement the HashJSONProperties interface.");
    }
    JSONProperty[] properties = ((HasJSONProperties) rootObject).getJSONProperties();
    for (int jsonPropertyIdx = 0; jsonPropertyIdx < properties.length; jsonPropertyIdx++) {
        JSONProperty property = properties[jsonPropertyIdx];
        Class returnType = property.getReturnType();
        String propertyName = property.getPropertyName();
        try {
            // get the unparsed value from the JSONObject
            Object value = null;
            try {
                value = jsonObj.get(propertyName);
            } catch (JSONException e) {
                continue;
            }
            if (value == JSONObject.NULL) {
                continue;
            }
            if (Enum.class.isAssignableFrom(returnType)) {
                property.getGetterSetter().setValue(rootObject, value);
            } else if (value instanceof JSONArray) {
                JSONArray array = (JSONArray) value;
                Vector list = new Vector();
                for (int i = 0; i < array.length(); i++) {
                    Object obj = array.get(i);
                    if (obj instanceof JSONObject) {
                        Object newObj = (Object) returnType.newInstance();
                        list.addElement(fromJSON((JSONObject) obj, newObj));
                    } else {
                        list.addElement(obj);
                    }
                }
                if (property.getContainedType() != null) {
                    property.getGetterSetter().setValue(rootObject, list);
                } else {
                    property.getGetterSetter().setValue(rootObject, toTypedArray(list));
                }
            } else if (value instanceof JSONObject) {
                Object obj = (Object) returnType.newInstance();
                if (Hashtable.class.isAssignableFrom(returnType)) {
                    Hashtable table = (Hashtable) obj;
                    JSONObject jsonMap = (JSONObject) value;
                    Enumeration keysEnum = jsonMap.keys();
                    while (keysEnum.hasMoreElements()) {
                        String key = (String) keysEnum.nextElement();
                        Object thisValue = jsonMap.get(key);
                        if (thisValue instanceof JSONObject) {
                            Object newObj = (Object) returnType.newInstance();
                            table.put(key, fromJSON((JSONObject) thisValue, newObj));
                        } else {
                            table.put(key, thisValue);
                        }
                    }
                    property.getGetterSetter().setValue(rootObject, table);
                } else {
                    // of the type correct for the
                    property.getGetterSetter().setValue(rootObject, fromJSON((JSONObject) value, obj));
                }
            } else if (isSimpleType(returnType)) {
                property.getGetterSetter().setValue(rootObject, correctCasting(returnType, value));
            }
        } catch (Exception e) {
            throw new JSONException(e);
        }
    }
    return rootObject;
}
Also used : Enumeration(java.util.Enumeration) HasJSONProperties(com.openmeap.json.HasJSONProperties) Hashtable(java.util.Hashtable) JSONArray(com.openmeap.thirdparty.org.json.me.JSONArray) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Vector(java.util.Vector)

Aggregations

HasJSONProperties (com.openmeap.json.HasJSONProperties)2 JSONArray (com.openmeap.thirdparty.org.json.me.JSONArray)2 JSONException (com.openmeap.thirdparty.org.json.me.JSONException)2 JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)2 Enumeration (java.util.Enumeration)2 Hashtable (java.util.Hashtable)2 Vector (java.util.Vector)2 Enum (com.openmeap.json.Enum)1