package com.example.base.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @Author Other
*/
public class JacksonUtil {
private static final Logger logger = LoggerFactory.getLogger(JacksonUtil.class);
private final static ObjectMapper MAPPER;
static {
MAPPER = new ObjectMapper();
// 忽略Json中在对象不存在对应属性
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 忽略空Bean转Json错误
MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// 设置属性为 空("") 或者为 NULL 都不序列化,默认 JsonInclude.Include.ALWAYS
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
// MAPPER.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
}
/**
* Object TO Json String 字符串输出(推荐)
*/
public static String toJson(Object object) {
try {
return MAPPER.writeValueAsString(object);
} catch (Exception e) {
logger.error("method=toJSON() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* Object TO Json String 字符串输出(输出空字符)
*/
public static String toJsonEmpty(Object object) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object param, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
//设置返回null转为 空字符串""
jsonGenerator.writeString("");
}
});
return objectMapper.writeValueAsString(object);
} catch (Exception e) {
logger.error("method=toJsonEmpty() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* Json 转为 Jave Bean
*/
public static <T> T toBean(String text, Class<T> clazz) {
try {
return MAPPER.readValue(text, clazz);
} catch (Exception e) {
logger.error("method=toBean() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* Json 转为 Map
*/
public static <K, V> Map<K, V> toMap(String text) {
try {
return toObject(text, new TypeReference<Map<K, V>>() {
});
} catch (Exception e) {
logger.error("method=toMap() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* Json 转 List, Class 集合中泛型的类型,非集合本身
*/
public static <T> List<T> toList(String text) {
try {
return toObject(text, new TypeReference<List<T>>() {
});
} catch (Exception e) {
logger.error("method=toList() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* Json 转 Object
*/
public static <T> T toObject(String text, TypeReference<T> typeReference) {
try {
if (!typeReference.getType().equals(String.class)) {
throw new ClassCastException("转换失败,Json对象类型不匹配");
}
return verifyParams(text) ? MAPPER.readValue(text, typeReference) : null;
} catch (Exception e) {
logger.error("method=toObject() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* 参数校验
*/
private static boolean verifyParams(Object object) {
if (null != object) {
if (object instanceof String) {
return !"".equals(object);
}
}
return false;
}
}
基于Jackson的工具类
最新推荐文章于 2024-04-28 15:15:34 发布