51 lines
1.6 KiB
Java
51 lines
1.6 KiB
Java
package com.tiesheng.util;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
|
import com.tiesheng.util.config.DesensitizeValueFilter;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
|
|
public class CommonUtil {
|
|
|
|
|
|
/**
|
|
* FastJson配置
|
|
*
|
|
* @return
|
|
*/
|
|
public static FastJsonConfig fastJsonConfig() {
|
|
FastJsonConfig config = new FastJsonConfig();
|
|
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
|
|
SerializerFeature.WriteNullStringAsEmpty,
|
|
SerializerFeature.WriteEnumUsingName,
|
|
SerializerFeature.DisableCircularReferenceDetect
|
|
);
|
|
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
config.setSerializeFilters(new DesensitizeValueFilter());
|
|
return config;
|
|
}
|
|
|
|
|
|
/**
|
|
* 格式化数据
|
|
*
|
|
* @param value
|
|
* @return
|
|
* @throws IOException
|
|
*/
|
|
public static String writeJsonString(Object value) throws IOException {
|
|
FastJsonConfig fastJsonConfig = fastJsonConfig();
|
|
ByteArrayOutputStream outnew = new ByteArrayOutputStream();
|
|
JSON.writeJSONStringWithFastJsonConfig(outnew, fastJsonConfig.getCharset(),
|
|
value, fastJsonConfig.getSerializeConfig(),
|
|
fastJsonConfig.getSerializeFilters(),
|
|
fastJsonConfig.getDateFormat(), JSON.DEFAULT_GENERATE_FEATURE,
|
|
fastJsonConfig.getSerializerFeatures());
|
|
return outnew.toString();
|
|
}
|
|
|
|
}
|