Optimize JacksonUtils.java

This commit is contained in:
hongqiaowei
2021-11-08 17:23:53 +08:00
parent 19673988d7
commit 934351f1dd

View File

@@ -22,8 +22,10 @@ import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.TypeFactory;
import we.util.Consts.DP; import we.util.Consts.DP;
import java.io.IOException; import java.io.IOException;
@@ -80,6 +82,10 @@ public abstract class JacksonUtils {
return m; return m;
} }
public static TypeFactory getTypeFactory() {
return m.getTypeFactory();
}
public static <T> T readValue(String json, Class<T> clz) { public static <T> T readValue(String json, Class<T> clz) {
try { try {
return m.readValue(json, clz); return m.readValue(json, clz);
@@ -88,6 +94,25 @@ public abstract class JacksonUtils {
} }
} }
/**
* give priority to {@link #readValue(String, JavaType)}
*/
public static <T> T readValue(String json, TypeReference<T> typeRef) {
try {
return m.readValue(json, typeRef);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static <T> T readValue(String json, JavaType javaType) {
try {
return m.readValue(json, javaType);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static <T> T readValue(byte[] bytes, Class<T> clz) { public static <T> T readValue(byte[] bytes, Class<T> clz) {
try { try {
return m.readValue(bytes, clz); return m.readValue(bytes, clz);
@@ -96,6 +121,25 @@ public abstract class JacksonUtils {
} }
} }
/**
* give priority to {@link #readValue(byte[], JavaType)}
*/
public static <T> T readValue(byte[] bytes, TypeReference<T> typeRef) {
try {
return m.readValue(bytes, typeRef);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static <T> 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) { public static String writeValueAsString(Object value) {
try { try {
return m.writeValueAsString(value); return m.writeValueAsString(value);