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(); } } }