Search in sources :

Example 1 with JSONArray

use of com.openmeap.thirdparty.org.json.me.JSONArray in project OpenMEAP by OpenMEAP.

the class JSONObjectBuilder method handleSimpleType.

private Object handleSimpleType(Class returnType, Object value) {
    if (returnType.isArray()) {
        Object[] returnValues = (Object[]) value;
        JSONArray jsonArray = new JSONArray();
        for (int returnValuesIdx = 0; returnValuesIdx < returnValues.length; returnValuesIdx++) {
            Object thisValue = returnValues[returnValuesIdx];
            jsonArray.put(thisValue);
        }
        return jsonArray;
    } else {
        return value;
    }
}
Also used : JSONArray(com.openmeap.thirdparty.org.json.me.JSONArray) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject)

Example 2 with JSONArray

use of com.openmeap.thirdparty.org.json.me.JSONArray 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 3 with JSONArray

use of com.openmeap.thirdparty.org.json.me.JSONArray 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)

Example 4 with JSONArray

use of com.openmeap.thirdparty.org.json.me.JSONArray in project OpenMEAP by OpenMEAP.

the class JSONArray method write.

/**
     * Write the contents of the JSONArray as JSON text to a writer.
     * For compactness, no whitespace is added.
     * <p>
     * Warning: This method assumes that the data structure is acyclical.
     *
     * @return The writer.
     * @throws JSONException
     */
public Writer write(Writer writer) throws JSONException {
    try {
        boolean b = false;
        int len = length();
        writer.write('[');
        for (int i = 0; i < len; i += 1) {
            if (b) {
                writer.write(',');
            }
            Object v = this.myArrayList.elementAt(i);
            if (v instanceof JSONObject) {
                ((JSONObject) v).write(writer);
            } else if (v instanceof JSONArray) {
                ((JSONArray) v).write(writer);
            } else {
                writer.write(JSONObject.valueToString(v));
            }
            b = true;
        }
        writer.write(']');
        return writer;
    } catch (IOException e) {
        throw new JSONException(e);
    }
}
Also used : JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JSONArray(com.openmeap.thirdparty.org.json.me.JSONArray) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) IOException(java.io.IOException)

Example 5 with JSONArray

use of com.openmeap.thirdparty.org.json.me.JSONArray in project OpenMEAP by OpenMEAP.

the class JSONObject method names.

/**
     * Produce a JSONArray containing the names of the elements of this
     * JSONObject.
     * @return A JSONArray containing the key strings, or null if the JSONObject
     * is empty.
     */
public JSONArray names() {
    JSONArray ja = new JSONArray();
    Enumeration keys = keys();
    while (keys.hasMoreElements()) {
        ja.put(keys.nextElement());
    }
    return ja.length() == 0 ? null : ja;
}
Also used : Enumeration(java.util.Enumeration) JSONArray(com.openmeap.thirdparty.org.json.me.JSONArray)

Aggregations

JSONArray (com.openmeap.thirdparty.org.json.me.JSONArray)8 JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)7 Enumeration (java.util.Enumeration)5 JSONException (com.openmeap.thirdparty.org.json.me.JSONException)4 HasJSONProperties (com.openmeap.json.HasJSONProperties)2 IOException (java.io.IOException)2 Hashtable (java.util.Hashtable)2 Vector (java.util.Vector)2 Enum (com.openmeap.json.Enum)1