格式化json类型字符串

/**
 * 这个类是从json-path.jar中 com.jayway.jsonpath.internal.JsonFormatter复制过来的,
 * 
 * @author xixi
 * @date 2018-09-28 14:24:01
 */

public class JsonFormatter {
    private static final String INDENT = "   ";

    private static final String NEW_LINE = System.getProperty("line.separator");

    private static final int MODE_SINGLE = 100;
    private static final int MODE_DOUBLE = 101;
    private static final int MODE_ESCAPE_SINGLE = 102;
    private static final int MODE_ESCAPE_DOUBLE = 103;
    private static final int MODE_BETWEEN = 104;

    private static void appendIndent(StringBuilder sb, int count) {
        for (; count > 0; --count)
            sb.append(INDENT);
    }

    public static String prettyPrint(String input) {

        input = input.replaceAll("[\\r\\n]", "");

        StringBuilder output = new StringBuilder(input.length() * 2);
        int mode = MODE_BETWEEN;
        int depth = 0;

        for (int i = 0; i < input.length(); ++i) {
            char ch = input.charAt(i);

            switch (mode) {
            case MODE_BETWEEN:
                switch (ch) {
                case '{':
                case '[':
                    output.append(ch);
                    output.append(NEW_LINE);
                    appendIndent(output, ++depth);
                    break;
                case '}':
                case ']':
                    output.append(NEW_LINE);
                    appendIndent(output, --depth);
                    output.append(ch);
                    break;
                case ',':
                    output.append(ch);
                    output.append(NEW_LINE);
                    appendIndent(output, depth);
                    break;
                case ':':
                    output.append(" : ");
                    break;
                case '\'':
                    output.append(ch);
                    mode = MODE_SINGLE;
                    break;
                case '"':
                    output.append(ch);
                    mode = MODE_DOUBLE;
                    break;
                case ' ':
                    break;
                default:
                    output.append(ch);
                    break;
                }
                break;
            case MODE_ESCAPE_SINGLE:
                output.append(ch);
                mode = MODE_SINGLE;
                break;
            case MODE_ESCAPE_DOUBLE:
                output.append(ch);
                mode = MODE_DOUBLE;
                break;
            case MODE_SINGLE:
                output.append(ch);
                switch (ch) {
                case '\'':
                    mode = MODE_BETWEEN;
                    break;
                case '\\':
                    mode = MODE_ESCAPE_SINGLE;
                    break;
                }
                break;
            case MODE_DOUBLE:
                output.append(ch);
                switch (ch) {
                case '"':
                    mode = MODE_BETWEEN;
                    break;
                case '\\':
                    mode = MODE_ESCAPE_DOUBLE;
                    break;
                }
                break;
            }
        }
        return output.toString();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值