From e9f44dd851a8f0bf18c00e3e5c53a8f8de9a6b66 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Fri, 25 Jul 2025 12:21:24 +0800 Subject: [PATCH] JacksonUtil --- pom.xml | 14 + .../com/ycwl/basic/utils/JacksonUtil.java | 277 ++++++++++++++++++ 2 files changed, 291 insertions(+) create mode 100644 src/main/java/com/ycwl/basic/utils/JacksonUtil.java diff --git a/pom.xml b/pom.xml index 641de13..c68ebd1 100644 --- a/pom.xml +++ b/pom.xml @@ -129,6 +129,20 @@ ${fastjson.version} + + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.core + jackson-core + + + com.fasterxml.jackson.core + jackson-annotations + + org.apache.commons diff --git a/src/main/java/com/ycwl/basic/utils/JacksonUtil.java b/src/main/java/com/ycwl/basic/utils/JacksonUtil.java new file mode 100644 index 0000000..4d0d65c --- /dev/null +++ b/src/main/java/com/ycwl/basic/utils/JacksonUtil.java @@ -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 泛型类型 + * @return 转换后的对象 + */ + public static T fromJson(String json, Class clazz) { + try { + return objectMapper.readValue(json, clazz); + } catch (IOException e) { + throw new RuntimeException("JSON转对象失败", e); + } + } + + /** + * JSON字符串转对象(支持复杂类型) + * @param json JSON字符串 + * @param typeReference 类型引用 + * @param 泛型类型 + * @return 转换后的对象 + */ + public static T fromJson(String json, TypeReference typeReference) { + try { + return objectMapper.readValue(json, typeReference); + } catch (IOException e) { + throw new RuntimeException("JSON转对象失败", e); + } + } + + /** + * JSON字符串转List + * @param json JSON字符串 + * @param elementClass 列表元素类型 + * @param 泛型类型 + * @return List对象 + */ + public static List fromJsonToList(String json, Class 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 fromJsonToMap(String json) { + try { + return objectMapper.readValue(json, new TypeReference>() {}); + } 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 泛型类型 + * @return 对象 + */ + public static T getObject(String json, String path, Class 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 泛型类型 + * @return List对象 + */ + public static List getArray(String json, String path, Class 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; + } +} \ No newline at end of file