JacksonUtil

This commit is contained in:
2025-07-25 12:21:24 +08:00
parent 1a1eb79914
commit e9f44dd851
2 changed files with 291 additions and 0 deletions

14
pom.xml
View File

@@ -129,6 +129,20 @@
<version>${fastjson.version}</version>
</dependency>
<!-- Jackson JSON处理库 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<!-- 引入commons-lang3 工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>

View File

@@ -0,0 +1,277 @@
package com.ycwl.basic.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* Jackson JSON工具类
* 提供简单易用的JSON序列化和反序列化功能
*/
public class JacksonUtil {
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* 对象转JSON字符串
* @param obj 要转换的对象
* @return JSON字符串
*/
public static String toJson(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException("对象转JSON失败", e);
}
}
/**
* JSON字符串转对象
* @param json JSON字符串
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 转换后的对象
*/
public static <T> T fromJson(String json, Class<T> clazz) {
try {
return objectMapper.readValue(json, clazz);
} catch (IOException e) {
throw new RuntimeException("JSON转对象失败", e);
}
}
/**
* JSON字符串转对象(支持复杂类型)
* @param json JSON字符串
* @param typeReference 类型引用
* @param <T> 泛型类型
* @return 转换后的对象
*/
public static <T> T fromJson(String json, TypeReference<T> typeReference) {
try {
return objectMapper.readValue(json, typeReference);
} catch (IOException e) {
throw new RuntimeException("JSON转对象失败", e);
}
}
/**
* JSON字符串转List
* @param json JSON字符串
* @param elementClass 列表元素类型
* @param <T> 泛型类型
* @return List对象
*/
public static <T> List<T> fromJsonToList(String json, Class<T> elementClass) {
try {
TypeFactory typeFactory = objectMapper.getTypeFactory();
CollectionType listType = typeFactory.constructCollectionType(List.class, elementClass);
return objectMapper.readValue(json, listType);
} catch (IOException e) {
throw new RuntimeException("JSON转List失败", e);
}
}
/**
* JSON字符串转Map
* @param json JSON字符串
* @return Map对象
*/
public static Map<String, Object> fromJsonToMap(String json) {
try {
return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
} catch (IOException e) {
throw new RuntimeException("JSON转Map失败", e);
}
}
/**
* 获取JSON节点
* @param json JSON字符串
* @return JsonNode对象
*/
public static JsonNode getJsonNode(String json) {
try {
return objectMapper.readTree(json);
} catch (IOException e) {
throw new RuntimeException("解析JSON失败", e);
}
}
/**
* 从JSON中获取字符串值
* @param json JSON字符串
* @param path 路径(支持嵌套,如:"user.name")
* @return 字符串值
*/
public static String getString(String json, String path) {
JsonNode node = getValueByPath(json, path);
return node != null && !node.isNull() ? node.asText() : null;
}
/**
* 从JSON中获取整数值
* @param json JSON字符串
* @param path 路径
* @return 整数值
*/
public static Integer getInt(String json, String path) {
JsonNode node = getValueByPath(json, path);
return node != null && !node.isNull() ? node.asInt() : null;
}
/**
* 从JSON中获取长整数值
* @param json JSON字符串
* @param path 路径
* @return 长整数值
*/
public static Long getLong(String json, String path) {
JsonNode node = getValueByPath(json, path);
return node != null && !node.isNull() ? node.asLong() : null;
}
/**
* 从JSON中获取双精度浮点数值
* @param json JSON字符串
* @param path 路径
* @return 双精度浮点数值
*/
public static Double getDouble(String json, String path) {
JsonNode node = getValueByPath(json, path);
return node != null && !node.isNull() ? node.asDouble() : null;
}
/**
* 从JSON中获取布尔值
* @param json JSON字符串
* @param path 路径
* @return 布尔值
*/
public static Boolean getBoolean(String json, String path) {
JsonNode node = getValueByPath(json, path);
return node != null && !node.isNull() ? node.asBoolean() : null;
}
/**
* 从JSON中获取对象
* @param json JSON字符串
* @param path 路径
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 对象
*/
public static <T> T getObject(String json, String path, Class<T> clazz) {
JsonNode node = getValueByPath(json, path);
if (node != null && !node.isNull()) {
try {
return objectMapper.treeToValue(node, clazz);
} catch (JsonProcessingException e) {
throw new RuntimeException("JSON节点转对象失败", e);
}
}
return null;
}
/**
* 从JSON中获取数组
* @param json JSON字符串
* @param path 路径
* @param elementClass 数组元素类型
* @param <T> 泛型类型
* @return List对象
*/
public static <T> List<T> getArray(String json, String path, Class<T> elementClass) {
JsonNode node = getValueByPath(json, path);
if (node != null && node.isArray()) {
try {
TypeFactory typeFactory = objectMapper.getTypeFactory();
CollectionType listType = typeFactory.constructCollectionType(List.class, elementClass);
return objectMapper.treeToValue(node, listType);
} catch (JsonProcessingException e) {
throw new RuntimeException("JSON节点转数组失败", e);
}
}
return null;
}
/**
* 检查JSON中是否存在指定路径
* @param json JSON字符串
* @param path 路径
* @return 是否存在
*/
public static boolean hasPath(String json, String path) {
JsonNode node = getValueByPath(json, path);
return node != null && !node.isMissingNode();
}
/**
* 根据路径获取JSON节点值
* @param json JSON字符串
* @param path 路径(支持嵌套,如:"user.name"或"users[0].name")
* @return JsonNode对象
*/
private static JsonNode getValueByPath(String json, String path) {
try {
JsonNode rootNode = objectMapper.readTree(json);
String[] pathParts = path.split("\\.");
JsonNode currentNode = rootNode;
for (String part : pathParts) {
if (currentNode == null || currentNode.isMissingNode()) {
return null;
}
// 处理数组索引,如:users[0]
if (part.contains("[") && part.contains("]")) {
String fieldName = part.substring(0, part.indexOf("["));
String indexStr = part.substring(part.indexOf("[") + 1, part.indexOf("]"));
int index = Integer.parseInt(indexStr);
currentNode = currentNode.get(fieldName);
if (currentNode != null && currentNode.isArray() && index < currentNode.size()) {
currentNode = currentNode.get(index);
} else {
return null;
}
} else {
currentNode = currentNode.get(part);
}
}
return currentNode;
} catch (Exception e) {
throw new RuntimeException("获取JSON路径值失败", e);
}
}
/**
* 美化JSON字符串(格式化输出)
* @param json JSON字符串
* @return 格式化后的JSON字符串
*/
public static String prettyPrint(String json) {
try {
JsonNode node = objectMapper.readTree(json);
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
} catch (IOException e) {
throw new RuntimeException("JSON格式化失败", e);
}
}
/**
* 获取ObjectMapper实例(用于高级用法)
* @return ObjectMapper实例
*/
public static ObjectMapper getObjectMapper() {
return objectMapper;
}
}