Helius
2021-08-05 fdb91cc72f7cbe8c095a1ce6442c9259ff01ff06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.xzx.gc.common.utils;
 
import cn.hutool.json.JSON;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONUtil;
import lombok.experimental.UtilityClass;
 
import java.io.StringWriter;
 
@UtilityClass
public class JSONS{
 
    /**
     * 格式化输出JSON字符串
     * @param indentFactor 每层缩进空格数
     * @param indent       本级别缩进量
     * @return JSON字符串
     * @throws JSONException 包含非法数抛出此异常
     */
    public  String format(Object jsonObj,int indentFactor,int indent ) throws JSONException {
        if(indentFactor==-1)indentFactor=4;
        if(indent==-1)indent=16;
        if(jsonObj==null)return "{}";
        JSON json=JSONUtil.parse(jsonObj);
        final StringWriter sw = new StringWriter();
        synchronized (sw.getBuffer()) {
            return json.write(sw, indentFactor, indent).toString();
        }
    }
}