提交 d31db724 编写于 作者: 如梦技术's avatar 如梦技术 🐛 提交者: Gitee

!17 JsonUtil 新增支持 java.io.Reader 读取和操作

Merge pull request !17 from caiqyxyx/master
...@@ -33,6 +33,7 @@ import org.springframework.lang.Nullable; ...@@ -33,6 +33,7 @@ import org.springframework.lang.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.*; import java.util.*;
...@@ -96,7 +97,7 @@ public class JsonUtil { ...@@ -96,7 +97,7 @@ public class JsonUtil {
} }
/** /**
* 将json字符串转成 JsonNode * 将InputStream转成 JsonNode
* *
* @param in InputStream * @param in InputStream
* @return jsonString json字符串 * @return jsonString json字符串
...@@ -110,6 +111,21 @@ public class JsonUtil { ...@@ -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 * 将json字符串转成 JsonNode
* *
...@@ -200,6 +216,27 @@ public class JsonUtil { ...@@ -200,6 +216,27 @@ public class JsonUtil {
} }
} }
/**
* 将java.io.Reader反序列化成对象
*
* @param reader java.io.Reader
* @param valueType class
* @param <T> T 泛型标记
* @return Bean
*/
@Nullable
public static <T> T readValue(@Nullable Reader reader, Class<T> valueType) {
if (reader == null) {
return null;
}
try {
return getInstance().readValue(reader, valueType);
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
}
/** /**
* 将json反序列化成对象 * 将json反序列化成对象
* *
...@@ -260,6 +297,26 @@ public class JsonUtil { ...@@ -260,6 +297,26 @@ public class JsonUtil {
} }
} }
/**
* 将java.io.Reader反序列化成对象
*
* @param reader java.io.Reader
* @param typeReference 泛型类型
* @param <T> T 泛型标记
* @return Bean
*/
@Nullable
public static <T> T readValue(@Nullable Reader reader, TypeReference<T> typeReference) {
if (reader == null) {
return null;
}
try {
return getInstance().readValue(reader, typeReference);
} catch (IOException e) {
throw Exceptions.unchecked(e);
}
}
/** /**
* 将json反序列化成对象 * 将json反序列化成对象
* *
...@@ -320,6 +377,26 @@ public class JsonUtil { ...@@ -320,6 +377,26 @@ public class JsonUtil {
} }
} }
/**
* 将java.io.Reader反序列化成对象
*
* @param reader java.io.Reader
* @param javaType JavaType
* @param <T> T 泛型标记
* @return Bean
*/
@Nullable
public static <T> 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 * 封装 map type,keyClass String
* *
...@@ -404,6 +481,25 @@ public class JsonUtil { ...@@ -404,6 +481,25 @@ public class JsonUtil {
} }
} }
/**
* 读取集合
*
* @param reader java.io.Reader
* @param elementClass elementClass
* @param <T> 泛型
* @return 集合
*/
public static <T> List<T> readList(@Nullable Reader reader, Class<T> 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 { ...@@ -443,6 +539,16 @@ public class JsonUtil {
return readMap(content, Object.class); return readMap(content, Object.class);
} }
/**
* 读取集合
*
* @param reader java.io.Reader
* @return 集合
*/
public static Map<String, Object> readMap(@Nullable Reader reader) {
return readMap(reader, Object.class);
}
/** /**
* 读取集合 * 读取集合
* *
...@@ -477,6 +583,18 @@ public class JsonUtil { ...@@ -477,6 +583,18 @@ public class JsonUtil {
return readMap(content, String.class, valueClass); return readMap(content, String.class, valueClass);
} }
/**
* 读取集合
*
* @param reader java.io.Reader
* @param valueClass 值类型
* @param <V> 泛型
* @return 集合
*/
public static <V> Map<String, V> readMap(@Nullable Reader reader, Class<?> valueClass) {
return readMap(reader, String.class, valueClass);
}
/** /**
* 读取集合 * 读取集合
* *
...@@ -531,6 +649,27 @@ public class JsonUtil { ...@@ -531,6 +649,27 @@ public class JsonUtil {
} }
} }
/**
* 读取集合
*
* @param reader java.io.Reader
* @param keyClass key类型
* @param valueClass 值类型
* @param <K> 泛型
* @param <V> 泛型
* @return 集合
*/
public static <K, V> Map<K, V> 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 { ...@@ -668,6 +807,16 @@ public class JsonUtil {
return isValidJson(mapper -> mapper.readTree(input)); 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 格式 * 检验 json 格式
* *
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册