Search in sources :

Example 16 with JSONObject

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

the class JSONObject method accumulate.

/**
     * Accumulate values under a key. It is similar to the put method except
     * that if there is already an object stored under the key then a
     * JSONArray is stored under the key to hold all of the accumulated values.
     * If there is already a JSONArray, then the new value is appended to it.
     * In contrast, the put method replaces the previous value.
     * @param key   A key string.
     * @param value An object to be accumulated under the key.
     * @return this.
     * @throws JSONException If the value is an invalid number
     *  or if the key is null.
     */
public JSONObject accumulate(String key, Object value) throws JSONException {
    testValidity(value);
    Object o = opt(key);
    if (o == null) {
        put(key, value);
    } else if (o instanceof JSONArray) {
        ((JSONArray) o).put(value);
    } else {
        put(key, new JSONArray().put(o).put(value));
    }
    return this;
}
Also used : JSONArray(com.openmeap.thirdparty.org.json.me.JSONArray) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject)

Example 17 with JSONObject

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

the class JSONObject method write.

/**
      * Write the contents of the JSONObject 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;
        Enumeration keys = keys();
        writer.write('{');
        while (keys.hasMoreElements()) {
            if (b) {
                writer.write(',');
            }
            Object k = keys.nextElement();
            writer.write(quote(k.toString()));
            writer.write(':');
            Object v = this.myHashMap.get(k);
            if (v instanceof JSONObject) {
                ((JSONObject) v).write(writer);
            } else if (v instanceof JSONArray) {
                ((JSONArray) v).write(writer);
            } else {
                writer.write(valueToString(v));
            }
            b = true;
        }
        writer.write('}');
        return writer;
    } catch (IOException e) {
        throw new JSONException(e);
    }
}
Also used : Enumeration(java.util.Enumeration) 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 18 with JSONObject

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

the class JSONObject method toString.

/**
     * Make a prettyprinted JSON text of this JSONObject.
     * <p>
     * Warning: This method assumes that the data structure is acyclical.
     * @param indentFactor The number of spaces to add to each level of
     *  indentation.
     * @param indent The indentation of the top level.
     * @return a printable, displayable, transmittable
     *  representation of the object, beginning
     *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending
     *  with <code>}</code>&nbsp;<small>(right brace)</small>.
     * @throws JSONException If the object contains an invalid number.
     */
String toString(int indentFactor, int indent) throws JSONException {
    int i;
    int n = length();
    if (n == 0) {
        return "{}";
    }
    Enumeration keys = keys();
    StringBuffer sb = new StringBuffer("{");
    int newindent = indent + indentFactor;
    Object o;
    if (n == 1) {
        o = keys.nextElement();
        sb.append(quote(o.toString()));
        sb.append(": ");
        sb.append(valueToString(this.myHashMap.get(o), indentFactor, indent));
    } else {
        while (keys.hasMoreElements()) {
            o = keys.nextElement();
            if (sb.length() > 1) {
                sb.append(",\n");
            } else {
                sb.append('\n');
            }
            for (i = 0; i < newindent; i += 1) {
                sb.append(' ');
            }
            sb.append(quote(o.toString()));
            sb.append(": ");
            sb.append(valueToString(this.myHashMap.get(o), indentFactor, newindent));
        }
        if (sb.length() > 1) {
            sb.append('\n');
            for (i = 0; i < indent; i += 1) {
                sb.append(' ');
            }
        }
    }
    sb.append('}');
    return sb.toString();
}
Also used : Enumeration(java.util.Enumeration) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject)

Example 19 with JSONObject

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

the class XML method toString.

/**
     * Convert a JSONObject into a well-formed, element-normal XML string.
     * @param o A JSONObject.
     * @param tagName The optional name of the enclosing tag.
     * @return A string.
     * @throws JSONException
     */
public static String toString(Object o, String tagName) throws JSONException {
    StringBuffer b = new StringBuffer();
    int i;
    JSONArray ja;
    JSONObject jo;
    String k;
    Enumeration keys;
    int len;
    String s;
    Object v;
    if (o instanceof JSONObject) {
        if (tagName != null) {
            b.append('<');
            b.append(tagName);
            b.append('>');
        }
        // Loop thru the keys.
        jo = (JSONObject) o;
        keys = jo.keys();
        while (keys.hasMoreElements()) {
            k = keys.nextElement().toString();
            v = jo.get(k);
            if (v instanceof String) {
                s = (String) v;
            } else {
                s = null;
            }
            if (k.equals("content")) {
                if (v instanceof JSONArray) {
                    ja = (JSONArray) v;
                    len = ja.length();
                    for (i = 0; i < len; i += 1) {
                        if (i > 0) {
                            b.append('\n');
                        }
                        b.append(escape(ja.get(i).toString()));
                    }
                } else {
                    b.append(escape(v.toString()));
                }
            // Emit an array of similar keys
            } else if (v instanceof JSONArray) {
                ja = (JSONArray) v;
                len = ja.length();
                for (i = 0; i < len; i += 1) {
                    b.append(toString(ja.get(i), k));
                }
            } else if (v.equals("")) {
                b.append('<');
                b.append(k);
                b.append("/>");
            // Emit a new tag <k>
            } else {
                b.append(toString(v, k));
            }
        }
        if (tagName != null) {
            // Emit the </tagname> close tag
            b.append("</");
            b.append(tagName);
            b.append('>');
        }
        return b.toString();
    // XML does not have good support for arrays. If an array appears in a place
    // where XML is lacking, synthesize an <array> element.
    } else if (o instanceof JSONArray) {
        ja = (JSONArray) o;
        len = ja.length();
        for (i = 0; i < len; ++i) {
            b.append(toString(ja.opt(i), (tagName == null) ? "array" : tagName));
        }
        return b.toString();
    } else {
        s = (o == null) ? "null" : escape(o.toString());
        return (tagName == null) ? "\"" + s + "\"" : (s.length() == 0) ? "<" + tagName + "/>" : "<" + tagName + ">" + s + "</" + tagName + ">";
    }
}
Also used : Enumeration(java.util.Enumeration) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JSONArray(com.openmeap.thirdparty.org.json.me.JSONArray) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject)

Example 20 with JSONObject

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

the class XML method toJSONObject.

/**
     * Convert a well-formed (but not necessarily valid) XML string into a
     * JSONObject. Some information may be lost in this transformation
     * because JSON is a data format and XML is a document format. XML uses
     * elements, attributes, and content text, while JSON uses unordered
     * collections of name/value pairs and arrays of values. JSON does not
     * does not like to distinguish between elements and attributes.
     * Sequences of similar elements are represented as JSONArrays. Content
     * text may be placed in a "content" member. Comments, prologs, DTDs, and
     * <code>&lt;[ [ ]]></code> are ignored.
     * @param string The source string.
     * @return A JSONObject containing the structured data from the XML string.
     * @throws JSONException
     */
public static JSONObject toJSONObject(String string) throws JSONException {
    JSONObject o = new JSONObject();
    XMLTokener x = new XMLTokener(string);
    while (x.more()) {
        x.skipPast("<");
        parse(x, o, null);
    }
    return o;
}
Also used : JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject)

Aggregations

JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)20 JSONException (com.openmeap.thirdparty.org.json.me.JSONException)11 JSONObjectBuilder (com.openmeap.json.JSONObjectBuilder)7 JSONArray (com.openmeap.thirdparty.org.json.me.JSONArray)6 Enumeration (java.util.Enumeration)6 IOException (java.io.IOException)5 Hashtable (java.util.Hashtable)5 HttpResponse (com.openmeap.http.HttpResponse)3 Result (com.openmeap.protocol.dto.Result)3 Result (com.openmeap.services.dto.Result)3 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)3 Vector (java.util.Vector)3 ClusterNodeRequest (com.openmeap.cluster.dto.ClusterNodeRequest)2 HasJSONProperties (com.openmeap.json.HasJSONProperties)2 HttpRequestException (com.openmeap.http.HttpRequestException)1 Enum (com.openmeap.json.Enum)1 Application (com.openmeap.model.dto.Application)1 ClusterNode (com.openmeap.model.dto.ClusterNode)1 GlobalSettings (com.openmeap.model.dto.GlobalSettings)1 ModelServiceRefreshHandler (com.openmeap.model.event.handler.ModelServiceRefreshHandler)1