package org.treeleafj.xdoc.utils;

/**
 * @author leaf
 * @date 2017-03-07 17:01
 */
public class JsonFormatUtils {

    /**
     * 格式化json字符串
     *
     * @param jsonStr
     * @return
     */
    public static String formatJson(String jsonStr) {
        if (null == jsonStr || "".equals(jsonStr)) return "";
        StringBuilder sb = new StringBuilder();
        char last;
        char current = '\0';
        int indent = 0;
        for (int i = 0; i < jsonStr.length(); i++) {
            last = current;
            current = jsonStr.charAt(i);
            switch (current) {
                case '{':
                case '[':
                    sb.append(current);
                    sb.append('\n');
                    indent++;
                    addIndentBlank(sb, indent);
                    break;
                case '}':
                case ']':
                    sb.append('\n');
                    indent--;
                    addIndentBlank(sb, indent);
                    sb.append(current);
                    break;
                case ',':
                    sb.append(current);
                    if (last != '\\') {
                        sb.append('\n');
                        addIndentBlank(sb, indent);
                    }
                    break;
                default:
                    sb.append(current);
            }
        }

        return sb.toString();
    }

    /**
     * 添加space
     *
     * @param sb
     * @param indent
     */
    private static void addIndentBlank(StringBuilder sb, int indent) {
        for (int i = 0; i < indent; i++) {
            sb.append('\t');
        }
    }

}

@@ -28,16 +28,62 @@
 utils;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0A
 /**%0A * @
@@ -210,16 +210,28 @@
  jsonStr
+ %E6%9C%AA%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%89%8D%E7%9A%84JSON%E7%AA%9C
 %0A     * 
@@ -237,16 +237,27 @@
  @return
+ %E6%A0%BC%E5%BC%8F%E5%8C%96%E5%A5%BD%E7%9A%84JSON%E7%AA%9C
 %0A     */
@@ -327,57 +327,87 @@
 if (
-null == jsonStr %7C%7C %22%22.equals(jsonStr)) return %22%22;
+StringUtils.isBlank(jsonStr)) %7B%0A            return StringUtils.EMPTY;%0A        %7D
 %0A   
@@ -1565,16 +1565,30 @@
 param sb
+     %E8%A6%81%E8%BF%BD%E5%8A%A0%E7%A9%BA%E6%A0%BC%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2
 %0A     * 
@@ -1600,16 +1600,23 @@
 m indent
+ %E8%BF%BD%E5%8A%A0%E7%9A%84%E7%A9%BA%E6%A0%BC%E6%95%B0
 %0A     */

优化360扫描代码提示