一、简介
Jackson是一个流行的Java库,用于处理JSON数据。它提供了丰富的API来序列化和反序列化Java对象为JSON格式,以及将JSON数据解析回Java对象
二、依赖
添加Jackson核心库(jackson-databind
)以及必要的注解库(jackson-annotations
)和数据处理库(jackson-core
)的依赖:
<dependencies>
<!-- Jackson core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.0</version> <!-- 使用时请检查最新版本 -->
</dependency>
<!-- Jackson databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version> <!-- 使用时请检查最新版本 -->
</dependency>
<!-- Jackson annotations (optional since included in jackson-databind) -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.0</version> <!-- 使用时请检查最新版本 -->
</dependency>
<!-- 其他依赖项... -->
</dependencies>
三、工具类(常用方法封装)
这个工具类JacksonUtils
包含了一个静态的ObjectMapper
实例,该实例在类加载时进行了配置。配置包括忽略未知的JSON属性(这样,当JSON中包含Java对象中没有的字段时,不会抛出异常),以及开启缩进输出以使得JSON字符串更易读。
toJson
方法接受一个Java对象,并返回其JSON字符串表示形式。如果序列化失败,它将抛出JsonProcessingException
。
fromJson
方法接受一个JSON字符串和一个目标类的Class
对象,并返回反序列化后的Java对象。如果反序列化失败,它将抛出IOException
。
你可以根据需要扩展这个工具类,添加更多的实用方法,比如处理复杂类型、泛型、日期格式等。
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
/**
* Jackson工具类
*/
@Slf4j
public final class JsonUtil {
private static ObjectMapper MAPPER = new ObjectMapper();
// 日起格式化
private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
static {
//对象的所有字段全部列入
MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
//取消默认转换timestamps形式
MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//忽略空Bean转json的错误
MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
MAPPER.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
//忽略 在json字符串中存在,但是在java对象中不存在对应属性的情况。防止错误
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
private JsonUtil() {
}
/**
* 对象转Json格式字符串
*
* @param object 对象
* @return Json格式字符串
*/
public static String toJson(Object object) {
if (object == null) {
return null;
}
try {
return object instanceof String ? (String) object : MAPPER.writeValueAsString(object);
} catch (Exception e) {
log.error("method=toJson() is convert error, errorMsg:{}", e.getMessage(), e);
return null;
}
}
/**
* Object TO Json String 字符串输出(输出空字符)
*
* @param object 对象
* @return Json格式字符串
*/
public static String toJsonEmpty(Object object) {
if (object == null) {
return null;
}
try {
MAPPER.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object param, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
//设置返回null转为 空字符串""
jsonGenerator.writeString("");
}
});
return MAPPER.writeValueAsString(object);
} catch (Exception e) {
log.error("method=toJsonEmpty() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* Json 转为 Jave Bean
*
* @param text json字符串
* @param clazz 对象类型class
* @param <T> 对象类型
* @return 对象类型
*/
public static <T> T fromJSON(String text, Class<T> clazz) {
if (StringUtils.isEmpty(text) || clazz == null) {
return null;
}
try {
return MAPPER.readValue(text, clazz);
} catch (Exception e) {
log.error("method=toBean() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* Json 转为 Map
*
* @param text json
* @param <K> key
* @param <V> value
* @return map
*/
public static <K, V> Map<K, V> toMap(String text) {
try {
if (StringUtils.isEmpty(text)) {
return null;
}
return toObject(text, new TypeReference<Map<K, V>>() {
});
} catch (Exception e) {
log.error("method=toMap() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* Json 转 List, Class 集合中泛型的类型,非集合本身
*
* @param text json
* @param <T> 对象类型
* @return List
*/
public static <T> List<T> toList(String text) {
if (StringUtils.isEmpty(text)) {
return null;
}
try {
return toObject(text, new TypeReference<List<T>>() {
});
} catch (Exception e) {
log.error("method=toList() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
/**
* Json 转 Object
*/
/**
* @param text json
* @param typeReference TypeReference
* @param <T> 类型
* @return T
*/
public static <T> T toObject(String text, TypeReference<T> typeReference) {
try {
if (StringUtils.isEmpty(text) || typeReference == null) {
return null;
}
return (T) (typeReference.getType().equals(String.class) ? text : MAPPER.readValue(text, typeReference));
} catch (Exception e) {
log.error("method=toObject() is convert error, errorMsg:{}", e.getMessage(), e);
}
return null;
}
}