use of com.openmeap.thirdparty.org.json.me.JSONArray 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;
}
use of com.openmeap.thirdparty.org.json.me.JSONArray 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);
}
}
use of com.openmeap.thirdparty.org.json.me.JSONArray 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 + ">";
}
}
Aggregations