Pretty Json

Given a string A representating json object. Return an array of string denoting json object with proper indentaion.

Rules for proper indentaion:

Note:


  1. [] and {} are only acceptable braces in this case.


  2. Assume for this problem that space characters can be done away with.



Input Format

The only argument given is the integer array A.

Output Format

Return a list of strings, where each entry corresponds to a single line. The strings should not have "\n" character in them.

For Example

Input 1:
    A = "{A:"B",C:{D:"E",F:{G:"H",I:"J"}}}"
Output 1:
    { 
        A:"B",
        C: 
        { 
            D:"E",
            F: 
            { 
                G:"H",
                I:"J"
            } 
        } 
    }

Input 2:
    A = ["foo", {"bar":["baz",null,1.0,2]}]
Output 2:
   [
        "foo", 
        {
            "bar":
            [
                "baz", 
                null, 
                1.0, 
                2
            ]
        }
    ]
Method:

The important thing to notice is that when do we add a new line. we do it when current char is ',' or a bracket, or when next char is a bracket. 
Note there is a special case which is '},' and '],' in which we put comma and bracket on the same line

Solution:

public class Solution {
    public ArrayList<String> prettyJSON(String A) {
        ArrayList<String> result = new ArrayList<>();
        if (A == null || A.length() == 0) return result;
        StringBuilder sb = new StringBuilder();
        int brackets = 0;
        char lastChar = '\0';
        // when next line:
        //  1. curr = ',' or bracket
        //  2. next char = bracket
        for (int i = 0; i < A.length(); i ++) {
           char c = A.charAt(i);
           if (c == ' ') continue;
           sb.append(c);
           if (c == ',' || c == '{' || c == '[' || c == '}' || c == ']') {
                if (c == '{' || c == '[') {
                    addTab(sb, brackets);
                    brackets ++;
                } else if (c == '}' || c == ']') {
                    brackets --;
                    addTab(sb, brackets);
                    if (i + 1 < A.length()) {
                        char n = A.charAt(i + 1);
                        if (n == ',') {
                            sb.append(n);
                            i ++;
                        }
                    }
                } else {
                    addTab(sb, brackets);
                }
                result.add(sb.toString());
                sb.setLength(0);
            } else if (i + 1 < A.length()) {
                char n = A.charAt(i + 1);
                if (n == '{' || n == '[' || n == '}' || n == ']') {
                    addTab(sb, brackets);
                    result.add(sb.toString());
                    sb.setLength(0);
               }
            }
            lastChar = A.charAt(i);
        }
        return result;
    }
    
    private void addTab(StringBuilder sb, int j) {
        for (int i = 0; i < j; i ++) {
            sb.insert(0, '\t');
        }
    }
}