package com.moral.api.utils;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
import java.io.IOException;
|
import java.util.Collection;
|
import java.util.List;
|
import java.util.Map;
|
/**
|
* @ClassName JsonUtils
|
* @Description TODO
|
* @Author @lizijie
|
* @Date 2023-09-20 13:43
|
* @Version 1.0
|
*/
|
public class JsonUtils {
|
private static ObjectMapper objectMapper = new ObjectMapper();
|
|
/**
|
* 对象转json字符串
|
*
|
* @param object
|
* @return
|
*/
|
public static String objectToJson(Object object) {
|
return objectToJson(object, null);
|
}
|
|
/**
|
* 对象转json字符串
|
*
|
* @param object 需要转换json的对象
|
* @param defaultStr 如果抛异常的默认字符串
|
* @return
|
*/
|
public static String objectToJson(Object object, String defaultStr) {
|
try {
|
return objectMapper.writeValueAsString(object);
|
} catch (JsonProcessingException e) {
|
e.printStackTrace();
|
}
|
return defaultStr;
|
}
|
|
/**
|
* json字符串转对象实体
|
*
|
* @param jsonStr json字符串
|
* @param object 转换异常的默认对象
|
* @param <T> 泛型
|
* @return
|
*/
|
public static <T> T jsonToObject(String jsonStr, T object) {
|
try {
|
return objectMapper.readValue(jsonStr, new TypeReference<T>() {
|
});
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return object;
|
|
}
|
|
/**
|
* json字符串转对象实体
|
*
|
* @param jsonStr json字符串
|
* @param t 实体类的类型
|
* @param <T> 泛型
|
* @return
|
*/
|
public static <T> T jsonToObject(String jsonStr, Class<T> t) {
|
try {
|
return objectMapper.readValue(jsonStr, t);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
|
}
|
|
/**
|
* json字符串转对象实体
|
*
|
* @param jsonStr json字符串
|
* @param typeReference
|
* @param <T>
|
* @return
|
*/
|
public static <T> T jsonToObject(String jsonStr, TypeReference<T> typeReference) {
|
try {
|
return objectMapper.readValue(jsonStr, typeReference);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
|
}
|
|
/**
|
* json字符串转Map
|
*
|
* @param jsonStr
|
* @param t
|
* @param v
|
* @param <K>
|
* @param <V>
|
* @return
|
*/
|
public static <K, V> Map<K, V> jsonToMap(String jsonStr, Class<K> t, Class<V> v) {
|
try {
|
return objectMapper.readValue(jsonStr, new TypeReference<Map<K, V>>() {
|
});
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
|
}
|
public static <K, V> Map<K, V> objectToMap(Object object, Class<K> t, Class<V> v) {
|
try {
|
return objectMapper.readValue(objectToJson(object), new TypeReference<Map<K, V>>() {
|
});
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
|
}
|
|
/**
|
* json字符串转集合
|
*
|
* @param jsonStr json字符串
|
* @param objectClass 类信息
|
* @param <T>
|
* @return
|
*/
|
public static <T> List<T> jsonToList(String jsonStr, Class<T> objectClass) {
|
try {
|
return objectMapper.readValue(jsonStr, new TypeReference<List<T>>() {
|
});
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
public static <T> List<T> convert(Collection<?> collection, Class<T> clazz) {
|
return jsonToList(objectToJson(collection), clazz);
|
}
|
}
|