This is how I ended up solving this. Not a great solution, but not sure how it could've been done better (given the problematic input restrictions). It's working well however, and attempts to pretty-print json the best it can. It's building on the solution seen here for PHP: https://gist.github.com/GloryFish/1045396
public String prettyPrintJSONAsString(String jsonString) {
int tabCount = 0;
StringBuffer prettyPrintJson = new StringBuffer();
String lineSeparator = "\r\n";
String tab = " ";
boolean ignoreNext = false;
boolean inQuote = false;
char character;
/* Loop through each character to style the output */
for (int i = 0; i < jsonString.length(); i++) {
character = jsonString.charAt(i);
if (inQuote) {
if (ignoreNext) {
ignoreNext = false;
} else if (character == '"') {
inQuote = !inQuote;
}
prettyPrintJson.append(character);
} else {
if (ignoreNext ? ignoreNext = !ignoreNext : ignoreNext);
switch (character) {
case '[':
++tabCount;
prettyPrintJson.append(character);
prettyPrintJson.append(lineSeparator);
printIndent(tabCount, prettyPrintJson, tab);
break;
case ']':
--tabCount;
prettyPrintJson.append(lineSeparator);
printIndent(tabCount, prettyPrintJson, tab);
prettyPrintJson.append(character);
break;
case '{':
++tabCount;
prettyPrintJson.append(character);
prettyPrintJson.append(lineSeparator);
printIndent(tabCount, prettyPrintJson, tab);
break;
case '}':
--tabCount;
prettyPrintJson.append(lineSeparator);
printIndent(tabCount, prettyPrintJson, tab);
prettyPrintJson.append(character);
break;
case '"':
inQuote = !inQuote;
prettyPrintJson.append(character);
break;
case ',':
prettyPrintJson.append(character);
prettyPrintJson.append(lineSeparator);
printIndent(tabCount, prettyPrintJson, tab);
break;
case ':':
prettyPrintJson.append(character + " ");
break;
case '\\':
prettyPrintJson.append(character);
ignoreNext = true;
break;
default:
prettyPrintJson.append(character);
break;
}
}
}
return prettyPrintJson.toString();
}
private void printIndent(int count, StringBuffer stringBuffer, String indent) {
for (int i = 0; i < count; i++) {
stringBuffer.append(indent);
}
}