Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String format(String input) {
- int tabs = 0;
- boolean quoted = false;
- StringBuilder builder = new StringBuilder(input);
- for (int index = 0; index < builder.length(); index++) {
- char c = builder.charAt(index);
- if (quoted && c != '"')
- continue;
- switch (c) {
- case '"':
- quoted = !(quoted && builder.charAt(index - 1) != '\\');
- continue;
- case ':':
- builder.insert(++index, ' ');
- continue;
- case '{':
- case '[':
- tabs++;
- case ',':
- builder.insert(index + 1, '\n');
- continue;
- case '}':
- case ']':
- tabs--;
- builder.insert(index++, '\n');
- for (int t = 0; t < tabs; t++)
- builder.insert(index++, '\t');
- continue;
- case '\n':
- for (int t = 0; t < tabs; t++)
- builder.insert(++index, '\t');
- continue;
- }
- }
- return builder.toString();
- }
Add Comment
Please, Sign In to add comment