JSON转换工具类
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import jakarta.validation.constraints.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class Json {
private static final Logger logger = LoggerFactory.getLogger ( Json.class) ;
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ( ) ;
static {
// 如果为空则不输出
OBJECT_MAPPER.setSerializationInclusion ( JsonInclude.Include.NON_EMPTY) ;
// 对于空的对象转json的时候不抛出错误
OBJECT_MAPPER.disable ( SerializationFeature.FAIL_ON_EMPTY_BEANS) ;
// 禁用序列化日期为timestamps
OBJECT_MAPPER.disable ( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) ;
// 禁用遇到未知属性抛出异常
OBJECT_MAPPER.disable ( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) ;
}
public static String toJsonString(Object object) {
try {
return OBJECT_MAPPER.writeValueAsString ( object) ;
} catch (JsonProcessingException e) {
logger.error ( "toJsonString() error: {}" , e.getMessage ( ) ) ;
}
return "" ;
}
public static <T> T parseObject(String json, Class<T> clazz) {
if (json == null) {
return null;
}
T result = null;
try {
result = OBJECT_MAPPER.readValue ( json, clazz) ;
} catch (Exception e) {
logger.error ( "parseObject() error: {}" , e.getMessage ( ) ) ;
}
return result;
}
public static <T> T parseObject(byte[] src, Class<T> clazz) {
T result = null;
try {
result = OBJECT_MAPPER.readValue ( src, clazz) ;
} catch (Exception e) {
logger.error ( "parseObject() error: {}" , e.getMessage ( ) ) ;
}
return result;
}
@NotNull
@SuppressWarnings ( "unused" )
public static ObjectMapper getObjectMapper ( ) {
return OBJECT_MAPPER;
}
@SuppressWarnings ( "unused" )
public static <T> List<T> parseArray ( String json, Class<T[]> clazz) {
if (json == null) {
return null;
}
T[] result = getTs ( json, clazz) ;
if (result == null) {
return Collections.emptyList ( ) ;
}
return Arrays.asList ( result) ;
}
@Nullable
private static <T> T[] getTs ( String json, Class<T[]> clazz) {
T[] result = null;
try {
result = OBJECT_MAPPER.readValue ( json, clazz) ;
} catch (Exception e) {
logger.error ( "parseArray() error: {}" , e.getMessage ( ) ) ;
}
return result;
}
@SuppressWarnings ( "unused" )
public static <T> List<T> parseArray ( byte[] src, Class<T[]> clazz) {
T[] result = null;
try {
result = OBJECT_MAPPER.readValue ( src, clazz) ;
} catch (Exception e) {
logger.error ( "parseArray() error: {}" , e.getMessage ( ) ) ;
}
if (result == null) {
return Collections.emptyList ( ) ;
}
return Arrays.asList ( result) ;
}
@SuppressWarnings ( "unused" )
public static JsonNode parseJson ( String jsonStr) {
if (jsonStr == null) {
return null;
}
JsonNode jsonNode = null;
try {
jsonNode = OBJECT_MAPPER.readTree ( jsonStr) ;
} catch (Exception e) {
logger.error ( "parseJson() error: {}" , e.getMessage ( ) ) ;
}
return jsonNode;
}
@SuppressWarnings ( "unused" )
public static String[] PRODUCT_TIME_COLUMN = new String[]{ "createTime" , "updateTime" , "checkTime" } ;
@SuppressWarnings ( "unused" )
public static String[] ORDER_TIME_COLUMN = new String[]{ "createTime" , "updateTime" , "payTime" , "deliveryTime" , "finallyTime" , "shopCartTime" , "cancelTime" , "inquiryCheckTime" } ;
@SuppressWarnings ( "unused" )
public static String[] SHARE_TIME_COLUMN = new String[]{ "createTime" , "updateTime" , "payTime" , "orderCompleteTime" , "confirmTime" , "applyTime" , "submitTime" , "callBackTime" } ;
@SuppressWarnings ( "unused" )
public static JSONObject formatterDateToLong ( @NotNull JSONObject json, String... keys) {
if (keys != null) {
for (String key : keys) {
if (json.containsKey(key) && json.getDate(key) != null) {
json.put ( key, json.getDate ( key) .getTime ( ) ) ;
}
}
}
return json;
}
@SuppressWarnings ( "unused" )
public static JSONObject formatterLongToDate ( @NotNull JSONObject json, String... keys) {
if (keys != null) {
for (String key : keys) {
if (json.containsKey(key) && json.getLong(key) != null) {
json.put ( key, new Date ( json.getLong ( key) ) ) ;
}
}
}
return json;
}
@SuppressWarnings ( "unused" )
public static JSONObject formatterLongToLocalDateTime ( JSONObject json, String... keys) {
if (keys != null) {
for (String key : keys) {
if (json.containsKey(key) && json.getLong(key) != null) {
json.put ( key, TimeUtil.timestamp2LocalDateTime ( json.getLong ( key) ) ) ;
}
}
}
return json;
}
}