use of com.openmeap.thirdparty.org.json.me.JSONObject in project OpenMEAP by OpenMEAP.
the class JSONObject method toString.
/**
* Make a JSON text of this JSONObject. For compactness, no whitespace
* is added. If this would not result in a syntactically correct JSON text,
* then null will be returned instead.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return a printable, displayable, portable, transmittable
* representation of the object, beginning
* with <code>{</code> <small>(left brace)</small> and ending
* with <code>}</code> <small>(right brace)</small>.
*/
public String toString() {
try {
Enumeration keys = keys();
StringBuffer sb = new StringBuffer("{");
while (keys.hasMoreElements()) {
if (sb.length() > 1) {
sb.append(',');
}
Object o = keys.nextElement();
sb.append(quote(o.toString()));
sb.append(':');
sb.append(valueToString(this.myHashMap.get(o)));
}
sb.append('}');
return sb.toString();
} catch (Exception e) {
return null;
}
}
use of com.openmeap.thirdparty.org.json.me.JSONObject in project OpenMEAP by OpenMEAP.
the class XML method parse.
/**
* Scan the content following the named tag, attaching it to the context.
* @param x The XMLTokener containing the source string.
* @param context The JSONObject that will include the new material.
* @param name The tag name.
* @return true if the close tag is processed.
* @throws JSONException
*/
private static boolean parse(XMLTokener x, JSONObject context, String name) throws JSONException {
char c;
int i;
String n;
JSONObject o = null;
String s;
Object t;
// Test for and skip past these forms:
// <!-- ... -->
// <! ... >
// <![ ... ]]>
// <? ... ?>
// Report errors for these forms:
// <>
// <=
// <<
t = x.nextToken();
if (t == BANG) {
c = x.next();
if (c == '-') {
if (x.next() == '-') {
x.skipPast("-->");
return false;
}
x.back();
} else if (c == '[') {
t = x.nextToken();
if (t.equals("CDATA")) {
if (x.next() == '[') {
s = x.nextCDATA();
if (s.length() > 0) {
context.accumulate("content", s);
}
return false;
}
}
throw x.syntaxError("Expected 'CDATA['");
}
i = 1;
do {
t = x.nextMeta();
if (t == null) {
throw x.syntaxError("Missing '>' after '<!'.");
} else if (t == LT) {
i += 1;
} else if (t == GT) {
i -= 1;
}
} while (i > 0);
return false;
} else if (t == QUEST) {
// <?
x.skipPast("?>");
return false;
} else if (t == SLASH) {
if (name == null || !x.nextToken().equals(name)) {
throw x.syntaxError("Mismatched close tag");
}
if (x.nextToken() != GT) {
throw x.syntaxError("Misshaped close tag");
}
return true;
} else if (t instanceof Character) {
throw x.syntaxError("Misshaped tag");
// Open tag <
} else {
n = (String) t;
t = null;
o = new JSONObject();
for (; ; ) {
if (t == null) {
t = x.nextToken();
}
if (t instanceof String) {
s = (String) t;
t = x.nextToken();
if (t == EQ) {
t = x.nextToken();
if (!(t instanceof String)) {
throw x.syntaxError("Missing value");
}
o.accumulate(s, t);
t = null;
} else {
o.accumulate(s, "");
}
// Empty tag <.../>
} else if (t == SLASH) {
if (x.nextToken() != GT) {
throw x.syntaxError("Misshaped tag");
}
context.accumulate(n, o);
return false;
// Content, between <...> and </...>
} else if (t == GT) {
for (; ; ) {
t = x.nextContent();
if (t == null) {
if (name != null) {
throw x.syntaxError("Unclosed tag " + name);
}
return false;
} else if (t instanceof String) {
s = (String) t;
if (s.length() > 0) {
o.accumulate("content", s);
}
// Nested element
} else if (t == LT) {
if (parse(x, o, n)) {
if (o.length() == 0) {
context.accumulate(n, "");
} else if (o.length() == 1 && o.opt("content") != null) {
context.accumulate(n, o.opt("content"));
} else {
context.accumulate(n, o);
}
return false;
}
}
}
} else {
throw x.syntaxError("Misshaped tag");
}
}
}
}
use of com.openmeap.thirdparty.org.json.me.JSONObject in project OpenMEAP by OpenMEAP.
the class JSONObjectBuilderTest method testToJSON.
public void testToJSON() throws Exception {
RootClass root = new RootClass();
root.setDoubleValue(Double.valueOf("3.14"));
root.setLongValue(new Long(Long.parseLong("1000")));
root.setIntegerValue(new Integer(Integer.parseInt("2000")));
root.setStringValue("value");
root.setStringArrayValue(new String[] { "value1", "value2" });
root.setChild(new BranchClass());
root.getChild().setTypeOne(Types.TWO);
root.getChild().setTypeTwo(Types.ONE);
root.getChild().setString("child_string");
Hashtable table = new Hashtable();
table.put("key1", "value1");
table.put("key2", new Long(1000));
table.put("key3", new Integer(1000));
root.setHashTable(table);
Vector vector = new Vector();
vector.add(Long.valueOf(1));
vector.add(Long.valueOf(2));
vector.add(Long.valueOf(3));
root.setVector(vector);
JSONObjectBuilder builder = new JSONObjectBuilder();
JSONObject jsonObj = builder.toJSON(root);
System.out.println(jsonObj.toString());
Assert.assertEquals("{\"stringValue\":\"value\",\"vector\":[1,2,3],\"integerValue\":2000," + "\"stringArrayValue\":[\"value1\",\"value2\"]," + "\"hashTable\":{\"key3\":1000,\"key2\":1000,\"key1\":\"value1\"}," + "\"doubleValue\":\"3.14\",\"longValue\":1000," + "\"child\":{\"string\":\"child_string\",\"typeTwo\":\"ONE\",\"typeOne\":\"TWO\"}}", jsonObj.toString());
RootClass afterRoundTrip = (RootClass) builder.fromJSON(jsonObj, new RootClass());
Assert.assertEquals(builder.toJSON(afterRoundTrip).toString(), jsonObj.toString());
}
use of com.openmeap.thirdparty.org.json.me.JSONObject in project OpenMEAP by OpenMEAP.
the class JsApiCoreImpl method performUpdate.
/**
*
* @param header JSON of the update header
* @param statusCallBack a status change callback
*/
public void performUpdate(final String header, final String statusCallBack) {
// needs to immediately return control to the calling javascript,
// and pass back download status information via the callback function.
JsUpdateHeader jsUpdateHeader = null;
try {
jsUpdateHeader = new JsUpdateHeader(header);
} catch (JSONException e) {
throw new GenericRuntimeException(e);
}
UpdateHeader reloadedHeader = jsUpdateHeader.getWrappedObject();
if (reloadedHeader != null) {
updateHandler.handleUpdate(reloadedHeader, new UpdateHandler.StatusChangeHandler() {
public void onStatusChange(UpdateStatus update) {
try {
JSONObject js = new JSONObject("{update:" + header + "}");
UpdateException error = update.getError();
js.put("bytesDownloaded", update.getBytesDownloaded());
js.put("complete", update.getComplete());
js.put("error", error != null ? new JsError(error.getUpdateResult().toString(), error.getMessage()).toJSONObject() : null);
webView.executeJavascriptFunction(statusCallBack, new String[] { js.toString() });
} catch (JSONException e) {
throw new GenericRuntimeException(e);
}
}
});
}
}
use of com.openmeap.thirdparty.org.json.me.JSONObject 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;
}
Aggregations