From 934351f1dde6108cc4f58ae4060b64d9216eec10 Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Mon, 8 Nov 2021 17:23:53 +0800 Subject: [PATCH] Optimize JacksonUtils.java --- .../src/main/java/we/util/JacksonUtils.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/fizz-common/src/main/java/we/util/JacksonUtils.java b/fizz-common/src/main/java/we/util/JacksonUtils.java index 871026c..aabd53f 100644 --- a/fizz-common/src/main/java/we/util/JacksonUtils.java +++ b/fizz-common/src/main/java/we/util/JacksonUtils.java @@ -22,8 +22,10 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.type.TypeFactory; import we.util.Consts.DP; import java.io.IOException; @@ -80,6 +82,10 @@ public abstract class JacksonUtils { return m; } + public static TypeFactory getTypeFactory() { + return m.getTypeFactory(); + } + public static T readValue(String json, Class clz) { try { return m.readValue(json, clz); @@ -88,6 +94,25 @@ public abstract class JacksonUtils { } } + /** + * give priority to {@link #readValue(String, JavaType)} + */ + public static T readValue(String json, TypeReference typeRef) { + try { + return m.readValue(json, typeRef); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + public static T readValue(String json, JavaType javaType) { + try { + return m.readValue(json, javaType); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + public static T readValue(byte[] bytes, Class clz) { try { return m.readValue(bytes, clz); @@ -96,6 +121,25 @@ public abstract class JacksonUtils { } } + /** + * give priority to {@link #readValue(byte[], JavaType)} + */ + public static T readValue(byte[] bytes, TypeReference typeRef) { + try { + return m.readValue(bytes, typeRef); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static T readValue(byte[] bytes, JavaType javaType) { + try { + return m.readValue(bytes, javaType); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + public static String writeValueAsString(Object value) { try { return m.writeValueAsString(value);