package com.mes.utils;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
/**
|
* @Author : zhoush
|
* @Date: 2025/9/8 15:23
|
* @Description:
|
*/
|
public class JsonUtils {
|
private static final ObjectMapper OBJECT_MAPPER = createObjectMapper();
|
|
private static ObjectMapper createObjectMapper() {
|
ObjectMapper mapper = new ObjectMapper();
|
|
// 重要配置:忽略未知属性,避免解析失败
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
// 其他有用配置
|
mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
|
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
|
// 支持Java 8日期时间
|
mapper.registerModule(new JavaTimeModule());
|
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
|
return mapper;
|
}
|
|
|
/**
|
* JSON字符串转对象
|
*
|
* @param json
|
* @param clazz
|
* @param <T>
|
* @return
|
*/
|
public static <T> T fromJson(String json, Class<T> clazz) {
|
try {
|
return OBJECT_MAPPER.readValue(json, clazz);
|
} catch (Exception e) {
|
throw new RuntimeException("JSON转换失败", e);
|
}
|
}
|
|
/**
|
* JSON字符串转泛型对象(如List、Map等)
|
*
|
* @param json
|
* @param typeReference
|
* @param <T>
|
* @return
|
*/
|
public static <T> T fromJson(String json, TypeReference<T> typeReference) {
|
try {
|
return OBJECT_MAPPER.readValue(json, typeReference);
|
} catch (Exception e) {
|
throw new RuntimeException("JSON转换失败", e);
|
}
|
}
|
|
/**
|
* 对象转JSON字符串
|
*
|
* @param obj
|
* @return
|
*/
|
public static String toJson(Object obj) {
|
try {
|
return OBJECT_MAPPER.writeValueAsString(obj);
|
} catch (Exception e) {
|
throw new RuntimeException("对象转JSON失败", e);
|
}
|
}
|
}
|