添加jacksonUtil
This commit is contained in:
parent
c4a5d8df3b
commit
1385971adb
@ -19,15 +19,14 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* <h3>一个可以自定义日志级别的配置文件</h3>
|
||||
* 如果将A配置设置为debug,在将A清 除会还原为默认的info级别
|
||||
* 配置格式:
|
||||
* 如果将A配置设置为debug,在将A清除会还原为默认的info级别
|
||||
*<p/>
|
||||
* <b>配置格式</b>
|
||||
*<pre>
|
||||
*pokonyan:
|
||||
* logger: #dev环境skywalking不可用,暂时屏蔽日志,防止刷屏
|
||||
* loggerLevel: {org.apache.skywalking.apm.dependencies.io.grpc.internal.ManagedChannelImpl: ERROR}
|
||||
* </pre>
|
||||
<pre>
|
||||
pokonyan:
|
||||
logger: #dev环境skywalking不可用,暂时屏蔽日志,防止刷屏
|
||||
loggerLevel: {org.apache.skywalking.apm.dependencies.io.grpc.internal.ManagedChannelImpl: ERROR}
|
||||
</pre>
|
||||
* @author tanjie@axzo.cn
|
||||
* @date 2023/7/6 10:48
|
||||
*/
|
||||
|
||||
@ -31,9 +31,6 @@ import java.util.TimeZone;
|
||||
/**
|
||||
* JacksonConfig
|
||||
*
|
||||
* @author lengleng
|
||||
* @author L.cm
|
||||
* @author lishangbu
|
||||
* @date 2020-06-17
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
|
||||
124
src/main/java/cn/axzo/pokonyan/util/JacksonUtil.java
Normal file
124
src/main/java/cn/axzo/pokonyan/util/JacksonUtil.java
Normal file
@ -0,0 +1,124 @@
|
||||
package cn.axzo.pokonyan.util;
|
||||
|
||||
import cn.axzo.pokonyan.config.jsonserializer.JackSonTimeModule;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class JacksonUtil implements InitializingBean {
|
||||
|
||||
private static ObjectMapper OBJECT_MAPPER;
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
JacksonUtil.OBJECT_MAPPER = SpringUtil.getBean(ObjectMapper.class);
|
||||
}
|
||||
|
||||
private JacksonUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象转Json格式字符串
|
||||
*
|
||||
* @param obj 对象
|
||||
* @return Json格式字符串
|
||||
*/
|
||||
public static <T> String obj2String(T obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return obj instanceof String ? (String) obj : OBJECT_MAPPER.writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warn("Parse Object to String error : {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 对象转Json格式字符串(格式化的Json字符串)
|
||||
*
|
||||
* @param obj 对象
|
||||
* @return 美化的Json格式字符串
|
||||
*/
|
||||
public static <T> String obj2StringPretty(T obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return obj instanceof String ? (String) obj : OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warn("Parse Object to String error : {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转换为自定义对象
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param clazz 自定义对象的class对象
|
||||
* @return 自定义对象
|
||||
*/
|
||||
public static <T> T string2Obj(String str, Class<T> clazz) {
|
||||
if (StringUtils.isEmpty(str) || clazz == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return clazz.equals(String.class) ? (T) str : OBJECT_MAPPER.readValue(str, clazz);
|
||||
} catch (Exception e) {
|
||||
log.warn("Parse String to Object error : {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转换为自定义字段转为list
|
||||
* @param str
|
||||
* @param typeReference
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T string2Obj(String str, TypeReference<T> typeReference) {
|
||||
if (StringUtils.isEmpty(str) || typeReference == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return (T) (typeReference.getType().equals(String.class) ? str : OBJECT_MAPPER.readValue(str, typeReference));
|
||||
} catch (IOException e) {
|
||||
log.warn("Parse String to Object error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T string2Obj(String str, Class<?> collectionClazz, Class<?>... elementClazzes) {
|
||||
JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
|
||||
try {
|
||||
return OBJECT_MAPPER.readValue(str, javaType);
|
||||
} catch (IOException e) {
|
||||
log.warn("Parse String to Object error : {}" + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user