Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica
提交
d31db724
mica
项目概览
mica
/
mica
通知
10
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d31db724
编写于
12月 11, 2021
作者:
如梦技术
🐛
提交者:
Gitee
12月 11, 2021
浏览文件
操作
浏览文件
下载
差异文件
!17 JsonUtil 新增支持 java.io.Reader 读取和操作
Merge pull request !17 from caiqyxyx/master
上级
893bd91a
4e41937d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
150 addition
and
1 deletion
+150
-1
mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java
...e/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java
+150
-1
未找到文件。
mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java
浏览文件 @
d31db724
...
...
@@ -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> 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反序列化成对象
*
...
...
@@ -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反序列化成对象
*
...
...
@@ -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
*
...
...
@@ -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 {
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 {
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 {
}
}
/**
* 读取集合
*
* @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 {
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 格式
*
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录