Commit b15036d2fdffa33a8583def95c9194b35ba42d4a
1 parent
84e52044
Exists in
master
json工具类提交
Showing
1 changed file
with
133 additions
and
0 deletions
Show diff stats
cloud/dahua/src/main/java/com/example/dahua/utils/JsonUtils.java
0 → 100644
@@ -0,0 +1,133 @@ | @@ -0,0 +1,133 @@ | ||
1 | +package com.example.dahua.utils; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
4 | +import com.fasterxml.jackson.core.type.TypeReference; | ||
5 | +import com.fasterxml.jackson.databind.DeserializationFeature; | ||
6 | +import com.fasterxml.jackson.databind.JavaType; | ||
7 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
8 | +import org.apache.commons.lang.StringUtils; | ||
9 | +import org.slf4j.Logger; | ||
10 | +import org.slf4j.LoggerFactory; | ||
11 | + | ||
12 | +import java.io.IOException; | ||
13 | +import java.util.Map; | ||
14 | + | ||
15 | +/** | ||
16 | + * 类JsonMapper.java的实现描述:简单封装Jackson,实现JSON String<->Java Object的Mapper. | ||
17 | + * | ||
18 | + * <p> 封装不同的输出风格, 使用不同的builder函数创建实例. </p> | ||
19 | + * | ||
20 | + * @author xuquan | ||
21 | + */ | ||
22 | +public class JsonUtils { | ||
23 | + | ||
24 | + private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class); | ||
25 | + | ||
26 | + private ObjectMapper mapper; | ||
27 | + | ||
28 | + public JsonUtils(){ | ||
29 | + this(null); | ||
30 | + } | ||
31 | + | ||
32 | + public JsonUtils(Include include) { | ||
33 | + mapper = new ObjectMapper(); | ||
34 | + // 设置输出时包含属性的风格 | ||
35 | + if (include != null) { | ||
36 | + mapper.setSerializationInclusion(include); | ||
37 | + } | ||
38 | + // 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性 | ||
39 | + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
40 | + } | ||
41 | + | ||
42 | + /** | ||
43 | + * 创建只输出非Null且非Empty(如List.isEmpty)的属性到Json字符串的Mapper,建议在外部接口中使用. | ||
44 | + */ | ||
45 | + public static JsonUtils nonEmptyMapper() { | ||
46 | + return new JsonUtils(Include.NON_EMPTY); | ||
47 | + } | ||
48 | + | ||
49 | + /** | ||
50 | + * 创建只输出初始值被改变的属性到Json字符串的Mapper, 最节约的存储方式,建议在内部接口中使用。 | ||
51 | + */ | ||
52 | + public static JsonUtils nonDefaultMapper() { | ||
53 | + return new JsonUtils(Include.NON_DEFAULT); | ||
54 | + } | ||
55 | + | ||
56 | + /** | ||
57 | + * Object可以是POJO,也可以是Collection或数组。 | ||
58 | + * 如果对象为Null, 返回"null". | ||
59 | + * 如果集合为空集合, 返回"[]". | ||
60 | + */ | ||
61 | + public String toJson(Object object) { | ||
62 | + | ||
63 | + try { | ||
64 | + return mapper.writeValueAsString(object); | ||
65 | + } catch (IOException e) { | ||
66 | + LOG.warn(String.format("序列化成 JSON 字符串发生错误:%s", object), e); | ||
67 | + return null; | ||
68 | + } | ||
69 | + } | ||
70 | + | ||
71 | + /** | ||
72 | + * 反序列化POJO或简单Collection如List<String>. | ||
73 | + * | ||
74 | + * 如果JSON字符串为Null或"null"字符串, 返回Null. | ||
75 | + * 如果JSON字符串为"[]", 返回空集合. | ||
76 | + * | ||
77 | + * 如需反序列化复杂Collection如List<MyBean>, 请使用fromJson(String, JavaType) | ||
78 | + * | ||
79 | + * @see #fromJson(String, JavaType) | ||
80 | + */ | ||
81 | + public <T> T fromJson(String jsonString, Class<T> clazz) { | ||
82 | + if (StringUtils.isEmpty(jsonString)) { | ||
83 | + return null; | ||
84 | + } | ||
85 | + | ||
86 | + try { | ||
87 | + return mapper.readValue(jsonString, clazz); | ||
88 | + } catch (IOException e) { | ||
89 | + LOG.error(String.format("解析 JSON 字符串发生错误:%s", jsonString), e); | ||
90 | + return null; | ||
91 | + } | ||
92 | + } | ||
93 | + | ||
94 | + /** | ||
95 | + * 反序列化泛型对象 | ||
96 | + * @param <T> | ||
97 | + * @param jsonString | ||
98 | + * @param typeReference | ||
99 | + * @return | ||
100 | + */ | ||
101 | + public <T> T fromJson(String jsonString, TypeReference<T> typeReference) { | ||
102 | + if (StringUtils.isEmpty(jsonString)) { | ||
103 | + return null; | ||
104 | + } | ||
105 | + | ||
106 | + try { | ||
107 | + return mapper.readValue(jsonString, typeReference); | ||
108 | + } catch (IOException e) { | ||
109 | + LOG.error(String.format("解析 JSON 字符串发生错误:%s", jsonString), e); | ||
110 | + return null; | ||
111 | + } | ||
112 | + } | ||
113 | + | ||
114 | + /** | ||
115 | + * 反序列化复杂Collection如List<Bean>, 先使用createCollectionType()或contructMapType()构造类型, 然后调用本函数. | ||
116 | + * | ||
117 | + * @see #(Class, Class...) | ||
118 | + */ | ||
119 | + public <T> T fromJson(String jsonString, JavaType javaType) { | ||
120 | + if (StringUtils.isEmpty(jsonString)) { | ||
121 | + return null; | ||
122 | + } | ||
123 | + | ||
124 | + try { | ||
125 | + return mapper.readValue(jsonString, javaType); | ||
126 | + } catch (IOException e) { | ||
127 | + LOG.error(String.format("解析 JSON 字符串发生错误:%s", jsonString), e); | ||
128 | + return null; | ||
129 | + } | ||
130 | + } | ||
131 | + | ||
132 | + | ||
133 | +} |