trying out a toJSON() method

This commit is contained in:
Ben Fry
2016-08-03 18:30:25 -04:00
parent ce007f41be
commit c1e130925f
2 changed files with 19 additions and 0 deletions

View File

@@ -660,8 +660,22 @@ public class IntDict {
}
/**
* Return this dictionary as a String in JSON format.
*/
public String toJSON() {
StringList items = new StringList();
for (int i = 0; i < size(); i++) {
items.append("\"" + keys[i] + "\": " + values[i]);
}
return "{ " + items.join(", ") + " }";
}
@Override
public String toString() {
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
/*
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName() + " size=" + size() + " { ");
for (int i = 0; i < size(); i++) {
@@ -672,5 +686,6 @@ public class IntDict {
}
sb.append(" }");
return sb.toString();
*/
}
}