From 155b3c8fe96af997a5f726526c731c6e76e1d42a Mon Sep 17 00:00:00 2001 From: ycl Date: Mon, 22 Dec 2025 14:11:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20template/common/src/main/j?= =?UTF-8?q?ava/{{.packagePath}}/common/utils/JsonUtils.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/JsonUtils.java | 112 ------------------ 1 file changed, 112 deletions(-) delete mode 100644 template/common/src/main/java/{{.packagePath}}/common/utils/JsonUtils.java diff --git a/template/common/src/main/java/{{.packagePath}}/common/utils/JsonUtils.java b/template/common/src/main/java/{{.packagePath}}/common/utils/JsonUtils.java deleted file mode 100644 index 67344c8..0000000 --- a/template/common/src/main/java/{{.packagePath}}/common/utils/JsonUtils.java +++ /dev/null @@ -1,112 +0,0 @@ -package {{ .package }}.common.utils; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.MissingNode; - -import java.io.IOException; -import java.util.Optional; - -public class JsonUtils { - - private static final ObjectMapper objectMapper = ObjectMapperFactory.getDefaultObjectMapper(); - - static { - objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); - } - - public static ObjectMapper getObjectMapper() { - return objectMapper; - } - - public static String toJson(Object value) { - try { - return objectMapper.writeValueAsString(value); - } catch (JsonProcessingException e) { - throw new IllegalStateException("to Json error", e); - } - } - - public static T readObject(String json, TypeReference typeReference) { - return readObject(json, objectMapper.getTypeFactory().constructType(typeReference)); - } - - public static T readObject(String json, Class clazz) { - return readObject(json, objectMapper.getTypeFactory().constructType(clazz)); - } - - public static T readObject(byte[] json, Class clazz) { - try { - return objectMapper.readValue(json, clazz); - } catch (IOException e) { - throw new IllegalStateException("read Json error", e); - } - } - - public static T readObject(String json, JavaType javaType) { - if (isBlank(json)) { - return null; - } - try { - return objectMapper.readValue(json, javaType); - } catch (IOException e) { - throw new IllegalStateException("read Json error", e); - } - } - - public static JsonNode path(String json, String path) { - if (isBlank(json)) { - return MissingNode.getInstance(); - } - try { - String atPath = path; - if (!path.startsWith("/")) { - atPath = "/" + path; - } - return objectMapper.readTree(json).at(atPath); - } catch (IOException e) { - throw new IllegalStateException("read Json error", e); - } - } - - private static boolean isBlank(String str) { - int strLen; - if (str == null || (strLen = str.length()) == 0) { - return true; - } - for (int i = 0; i < strLen; i++) { - if ((!Character.isWhitespace(str.charAt(i)))) { - return false; - } - } - return true; - } - - public static Optional readPath(String json, String path, TypeReference typeReference) { - return readPath(json, path, objectMapper.getTypeFactory().constructType(typeReference)); - } - - public static Optional readPath(String json, String path, Class clazz) { - JsonNode jsonNode = path(json, path); - if (jsonNode.isMissingNode()) { - return Optional.empty(); - } - try { - return Optional.of(objectMapper.treeToValue(jsonNode, clazz)); - } catch (JsonProcessingException e) { - throw new IllegalStateException(e); - } - } - - public static Optional readPath(String json, String path, JavaType javaType) { - JsonNode jsonNode = path(json, path); - if (jsonNode.isMissingNode()) { - return Optional.empty(); - } - return Optional.of(objectMapper.convertValue(jsonNode, javaType)); - } -}