lodsve.core.json.JsonFormatUtils Maven / Gradle / Ivy
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package lodsve.core.json;
/**
* json字符串格式化工具类.
*
* @author sunhao([email protected])
* @date 2018-07-09 10:31
*/
public final class JsonFormatUtils {
/**
* 返回格式化JSON字符串。
*
* @param json 未格式化的JSON字符串。
* @return 格式化的JSON字符串。
*/
public static String formatJson(String json) {
StringBuilder result = new StringBuilder();
int length = json.length();
int number = 0;
char key;
//遍历输入字符串。
for (int i = 0; i < length; i++) {
//1、获取当前字符。
key = json.charAt(i);
//2、如果当前字符是前方括号、前花括号做如下处理:
if ((key == '[') || (key == '{')) {
//(1)如果前面还有字符,并且字符为“:”,打印:换行和缩进字符字符串。
if ((i - 1 > 0) && (json.charAt(i - 1) == ':')) {
result.append('\n');
result.append(indent(number));
}
//(2)打印:当前字符。
result.append(key);
//(3)前方括号、前花括号,的后面必须换行。打印:换行。
result.append('\n');
//(4)每出现一次前方括号、前花括号;缩进次数增加一次。打印:新行缩进。
number++;
result.append(indent(number));
//(5)进行下一次循环。
continue;
}
//3、如果当前字符是后方括号、后花括号做如下处理:
if ((key == ']') || (key == '}')) {
//(1)后方括号、后花括号,的前面必须换行。打印:换行。
result.append('\n');
//(2)每出现一次后方括号、后花括号;缩进次数减少一次。打印:缩进。
number--;
result.append(indent(number));
//(3)打印:当前字符。
result.append(key);
//(4)如果当前字符后面还有字符,并且字符不为“,”,打印:换行。
if (((i + 1) < length) && (json.charAt(i + 1) != ',')) {
result.append('\n');
}
//(5)继续下一次循环。
continue;
}
//4、如果当前字符是逗号。逗号后面换行,并缩进,不改变缩进次数。
if ((key == ',')) {
result.append(key);
result.append('\n');
result.append(indent(number));
continue;
}
//5、打印:当前字符。
result.append(key);
}
return result.toString();
}
/**
* 返回指定次数的缩进字符串。每一次缩进三个空格,即SPACE。
*
* @param number 缩进次数。
* @return 指定缩进次数的字符串。
*/
private static String indent(int number) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < number; i++) {
result.append(" ");
}
return result.toString();
}
}