Optimized stringifyJson a bit.

This commit is contained in:
FrozenCow 2011-03-04 22:17:50 +01:00
parent d1f280eb0f
commit 41bf56fccb

View File

@ -8,56 +8,55 @@ import java.util.List;
public class Json { public class Json {
public static String stringifyJson(Object o) { public static String stringifyJson(Object o) {
StringBuilder sb = new StringBuilder();
appendJson(o, sb);
return sb.toString();
}
public static void appendJson(Object o, StringBuilder s) {
if (o == null) { if (o == null) {
return "null"; s.append("null");
} else if (o instanceof Boolean) { } else if (o instanceof Boolean) {
return ((Boolean) o) ? "true" : "false"; s.append(((Boolean) o) ? "true" : "false");
} else if (o instanceof String) { } else if (o instanceof String) {
return "\"" + ((String)o).replace("\"", "\\\"") + "\""; s.append("\"" + ((String)o).replace("\"", "\\\"") + "\"");
} else if (o instanceof Integer || o instanceof Long || o instanceof Float || o instanceof Double) { } else if (o instanceof Integer || o instanceof Long || o instanceof Float || o instanceof Double) {
return o.toString(); s.append(o.toString());
} else if (o instanceof LinkedHashMap<?, ?>) { } else if (o instanceof LinkedHashMap<?, ?>) {
LinkedHashMap<?, ?> m = (LinkedHashMap<?, ?>) o; LinkedHashMap<?, ?> m = (LinkedHashMap<?, ?>) o;
StringBuilder sb = new StringBuilder(); s.append("{");
sb.append("{");
boolean first = true; boolean first = true;
for (Object key : m.keySet()) { for (Object key : m.keySet()) {
if (first) if (first)
first = false; first = false;
else else
sb.append(","); s.append(",");
sb.append(stringifyJson(key)); appendJson(key, s);
sb.append(": "); s.append(": ");
sb.append(stringifyJson(m.get(key))); appendJson(m.get(key), s);
} }
sb.append("}"); s.append("}");
return sb.toString();
} else if (o instanceof List<?>) { } else if (o instanceof List<?>) {
List<?> l = (List<?>) o; List<?> l = (List<?>) o;
StringBuilder sb = new StringBuilder(); s.append("[");
sb.append("[");
int count = 0; int count = 0;
for (int i = 0; i < l.size(); i++) { for (int i = 0; i < l.size(); i++) {
if (count++ > 0) sb.append(","); if (count++ > 0) s.append(",");
sb.append(stringifyJson(l.get(i))); appendJson(l.get(i), s);
} }
sb.append("]"); s.append("]");
return sb.toString();
} else if (o.getClass().isArray()) { } else if (o.getClass().isArray()) {
int length = Array.getLength(o); int length = Array.getLength(o);
StringBuilder sb = new StringBuilder(); s.append("[");
sb.append("[");
int count = 0; int count = 0;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
if (count++ > 0) sb.append(","); if (count++ > 0) s.append(",");
sb.append(stringifyJson(Array.get(o, i))); appendJson(Array.get(o, i), s);
} }
sb.append("]"); s.append("]");
return sb.toString();
} else if (o instanceof Object) /* TODO: Always true, maybe interface? */ { } else if (o instanceof Object) /* TODO: Always true, maybe interface? */ {
StringBuilder sb = new StringBuilder(); s.append("{");
sb.append("{");
boolean first = true; boolean first = true;
Class<?> c = o.getClass(); Class<?> c = o.getClass();
@ -77,15 +76,14 @@ public class Json {
if (first) if (first)
first = false; first = false;
else else
sb.append(","); s.append(",");
sb.append(stringifyJson(fieldName)); appendJson(fieldName, s);
sb.append(": "); s.append(": ");
sb.append(stringifyJson(fieldValue)); appendJson(fieldValue, s);
} }
sb.append("}"); s.append("}");
return sb.toString();
} else { } else {
return "undefined"; s.append("undefined");
} }
} }
} }