From 4e41937d7412bca53a685dbac67c6a2c82639df2 Mon Sep 17 00:00:00 2001 From: caiqy Date: Sat, 11 Dec 2021 09:45:52 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20JsonUtil=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20java.io.Reader=20=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/dreamlu/mica/core/utils/JsonUtil.java | 151 +++++++++++++++++- 1 file changed, 150 insertions(+), 1 deletion(-) diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java index ad69dc4a..760e58aa 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java @@ -33,6 +33,7 @@ import org.springframework.lang.Nullable; import java.io.IOException; import java.io.InputStream; +import java.io.Reader; import java.text.SimpleDateFormat; import java.time.ZoneId; import java.util.*; @@ -96,7 +97,7 @@ public class JsonUtil { } /** - * 将json字符串转成 JsonNode + * 将InputStream转成 JsonNode * * @param in InputStream * @return jsonString json字符串 @@ -110,6 +111,21 @@ public class JsonUtil { } } + /** + * 将java.io.Reader转成 JsonNode + * + * @param reader java.io.Reader + * @return jsonString json字符串 + */ + public static JsonNode readTree(Reader reader) { + Objects.requireNonNull(reader, "Reader in is null"); + try { + return getInstance().readTree(reader); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + /** * 将json字符串转成 JsonNode * @@ -200,6 +216,27 @@ public class JsonUtil { } } + /** + * 将java.io.Reader反序列化成对象 + * + * @param reader java.io.Reader + * @param valueType class + * @param T 泛型标记 + * @return Bean + */ + @Nullable + public static T readValue(@Nullable Reader reader, Class valueType) { + if (reader == null) { + return null; + } + try { + return getInstance().readValue(reader, valueType); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + + /** * 将json反序列化成对象 * @@ -260,6 +297,26 @@ public class JsonUtil { } } + /** + * 将java.io.Reader反序列化成对象 + * + * @param reader java.io.Reader + * @param typeReference 泛型类型 + * @param T 泛型标记 + * @return Bean + */ + @Nullable + public static T readValue(@Nullable Reader reader, TypeReference typeReference) { + if (reader == null) { + return null; + } + try { + return getInstance().readValue(reader, typeReference); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + /** * 将json反序列化成对象 * @@ -320,6 +377,26 @@ public class JsonUtil { } } + /** + * 将java.io.Reader反序列化成对象 + * + * @param reader java.io.Reader + * @param javaType JavaType + * @param T 泛型标记 + * @return Bean + */ + @Nullable + public static T readValue(@Nullable Reader reader, JavaType javaType) { + if (reader == null) { + return null; + } + try { + return getInstance().readValue(reader, javaType); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + /** * 封装 map type,keyClass String * @@ -404,6 +481,25 @@ public class JsonUtil { } } + /** + * 读取集合 + * + * @param reader java.io.Reader + * @param elementClass elementClass + * @param 泛型 + * @return 集合 + */ + public static List readList(@Nullable Reader reader, Class elementClass) { + if (reader == null) { + return Collections.emptyList(); + } + try { + return getInstance().readValue(reader, getListType(elementClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + /** * 读取集合 * @@ -443,6 +539,16 @@ public class JsonUtil { return readMap(content, Object.class); } + /** + * 读取集合 + * + * @param reader java.io.Reader + * @return 集合 + */ + public static Map readMap(@Nullable Reader reader) { + return readMap(reader, Object.class); + } + /** * 读取集合 * @@ -477,6 +583,18 @@ public class JsonUtil { return readMap(content, String.class, valueClass); } + /** + * 读取集合 + * + * @param reader java.io.Reader + * @param valueClass 值类型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable Reader reader, Class valueClass) { + return readMap(reader, String.class, valueClass); + } + /** * 读取集合 * @@ -531,6 +649,27 @@ public class JsonUtil { } } + /** + * 读取集合 + * + * @param reader java.io.Reader + * @param keyClass key类型 + * @param valueClass 值类型 + * @param 泛型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable Reader reader, Class keyClass, Class valueClass) { + if (reader == null) { + return Collections.emptyMap(); + } + try { + return getInstance().readValue(reader, getMapType(keyClass, valueClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + /** * 读取集合 * @@ -668,6 +807,16 @@ public class JsonUtil { return isValidJson(mapper -> mapper.readTree(input)); } + /** + * 检验 json 格式 + * + * @param reader java.io.Reader + * @return 是否成功 + */ + public static boolean isValidJson(Reader reader) { + return isValidJson(mapper -> mapper.readTree(reader)); + } + /** * 检验 json 格式 * -- Gitee