mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-21 18:45:57 +08:00
自定义json工具类
This commit is contained in:
parent
0b293092a3
commit
eb807fae94
@ -0,0 +1,128 @@
|
||||
package org.dromara.common.json.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class JacksonJsonUtil {
|
||||
private static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
static {
|
||||
//对象的所有字段全部列入
|
||||
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
|
||||
|
||||
//取消默认转换timesstamps(时间戳)形式
|
||||
OBJECT_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||
|
||||
//忽略空bean转json错误
|
||||
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||
|
||||
//忽略在json字符串中存在,在java类中不存在字段,防止错误
|
||||
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||
}
|
||||
|
||||
public static <T> String objToJson(T obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return obj instanceof String ? (String)obj : OBJECT_MAPPER.writeValueAsString(obj);
|
||||
} catch (Exception e) {
|
||||
log.error("obj To json is error" , e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回格式化好的json串
|
||||
* @param obj
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> String objToJsonPretty(T obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return obj instanceof String ? (String)obj : OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
||||
} catch (Exception e) {
|
||||
log.warn("obj To json pretty is error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T json2Object(String json, Class<T> clazz) {
|
||||
if (StringUtils.isEmpty(json) || clazz == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return clazz.equals(String.class) ? (T)json : OBJECT_MAPPER.readValue(json, clazz);
|
||||
} catch (Exception e) {
|
||||
log.warn("json To obj is error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 TypeReference 处理List<User>这类多泛型问题
|
||||
* @param json
|
||||
* @param typeReference
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T json2Object(String json, TypeReference typeReference) {
|
||||
if (StringUtils.isEmpty(json) || typeReference == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return (T)(typeReference.getType().equals(String.class) ? json : OBJECT_MAPPER.readValue(json, typeReference));
|
||||
} catch (Exception e) {
|
||||
log.warn("json To obj is error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过jackson 的javatype 来处理多泛型的转换
|
||||
* @param json
|
||||
* @param collectionClazz
|
||||
* @param elements
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T json2Object(String json, Class<?> collectionClazz, Class<?>...elements) {
|
||||
JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClazz, elements);
|
||||
|
||||
try {
|
||||
return OBJECT_MAPPER.readValue(json, javaType);
|
||||
} catch (Exception e) {
|
||||
log.warn("json To obj is error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Map toLinkedHashMap(String jsonStr) {
|
||||
|
||||
Map<String, String> data = null;
|
||||
try {
|
||||
data = OBJECT_MAPPER.readValue(jsonStr, new TypeReference<LinkedHashMap<String, String>>() {});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user