clean up toString() and implement toJSON() consistently

This commit is contained in:
Ben Fry
2016-10-30 16:12:40 -04:00
parent a44c05c41f
commit 67e9d13148
7 changed files with 74 additions and 76 deletions

View File

@@ -475,17 +475,20 @@ public class StringDict {
}
/**
* Return this dictionary as a String in JSON format.
*/
public String toJSON() {
StringList items = new StringList();
for (int i = 0; i < count; i++) {
items.append(JSONObject.quote(keys[i])+ ": " + JSONObject.quote(values[i]));
}
return "{ " + items.join(", ") + " }";
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName() + " size=" + size() + " { ");
for (int i = 0; i < size(); i++) {
if (i != 0) {
sb.append(", ");
}
sb.append("\"" + keys[i] + "\": \"" + values[i] + "\"");
}
sb.append(" }");
return sb.toString();
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
}
}