mirror of
https://github.com/webbukkit/dynmap.git
synced 2024-12-28 19:47:45 +01:00
Added reflection-support for stringifyJson.
This commit is contained in:
parent
d651d58d63
commit
a937d13086
@ -1,5 +1,7 @@
|
||||
package org.dynmap.web;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
@ -40,6 +42,35 @@ public class Json {
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
} else if (o instanceof Object) /* TODO: Always true, maybe interface? */ {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
boolean first = true;
|
||||
|
||||
Class<?> c = o.getClass();
|
||||
for(Field field : c.getFields()) {
|
||||
if (!Modifier.isPublic(field.getModifiers()))
|
||||
continue;
|
||||
String fieldName = field.getName();
|
||||
Object fieldValue;
|
||||
try {
|
||||
fieldValue = field.get(o);
|
||||
} catch (IllegalArgumentException e) {
|
||||
continue;
|
||||
} catch (IllegalAccessException e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
sb.append(",");
|
||||
sb.append(stringifyJson(fieldName));
|
||||
sb.append(": ");
|
||||
sb.append(stringifyJson(fieldValue));
|
||||
}
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
} else {
|
||||
return "undefined";
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user