22
33import cn .hutool .core .util .StrUtil ;
44import com .fasterxml .jackson .annotation .JsonInclude ;
5+ import com .fasterxml .jackson .annotation .JsonTypeInfo .As ;
56import com .fasterxml .jackson .core .JsonProcessingException ;
67import com .fasterxml .jackson .core .type .TypeReference ;
78import com .fasterxml .jackson .databind .DeserializationFeature ;
89import com .fasterxml .jackson .databind .ObjectMapper ;
10+ import com .fasterxml .jackson .databind .ObjectMapper .DefaultTyping ;
911import com .fasterxml .jackson .databind .SerializationFeature ;
1012import com .github .cadecode .uniboot .common .core .exception .UtilException ;
1113import lombok .extern .slf4j .Slf4j ;
2729@ Component
2830public class JacksonUtil implements InitializingBean {
2931
30- private static ObjectMapper OBJECT_MAPPER ;
32+ private static ObjectMapper PROJECT_OBJECT_MAPPER ;
3133
34+ /**
35+ * ObjectMapper,序列化结果中携带类型
36+ */
37+ private static ObjectMapper TYPING_OBJECT_MAPPER ;
38+
39+ /**
40+ * SpringBoot 项目默认 ObjectMapper Bean
41+ */
3242 private ObjectMapper objectMapper ;
3343
3444 /**
@@ -40,7 +50,14 @@ public void setObjectMapper(ObjectMapper objectMapper) {
4050 }
4151
4252 public static ObjectMapper getMapper () {
43- return OBJECT_MAPPER ;
53+ return getMapper (false );
54+ }
55+
56+ public static ObjectMapper getMapper (boolean typing ) {
57+ if (typing ) {
58+ return TYPING_OBJECT_MAPPER ;
59+ }
60+ return PROJECT_OBJECT_MAPPER ;
4461 }
4562
4663 /**
@@ -50,7 +67,7 @@ public static ObjectMapper getMapper() {
5067 * @return json 字符串
5168 */
5269 public static String toJson (Object bean ) {
53- return toJson (bean , false );
70+ return toJson (bean , false , false );
5471 }
5572
5673 /**
@@ -61,6 +78,18 @@ public static String toJson(Object bean) {
6178 * @return json 字符串
6279 */
6380 public static String toJson (Object bean , boolean isPretty ) {
81+ return toJson (bean , false , isPretty );
82+ }
83+
84+ /**
85+ * 转换对象到 json 串
86+ *
87+ * @param bean 需要转换的对象
88+ * @param typing 是否序列化类型
89+ * @param isPretty 是否美化输出(换行)
90+ * @return json 字符串
91+ */
92+ public static String toJson (Object bean , boolean typing , boolean isPretty ) {
6493 if (bean == null ) {
6594 return null ;
6695 }
@@ -69,77 +98,114 @@ public static String toJson(Object bean, boolean isPretty) {
6998 }
7099 try {
71100 if (!isPretty ) {
72- return OBJECT_MAPPER .writeValueAsString (bean );
101+ return getMapper ( typing ) .writeValueAsString (bean );
73102 }
74- return OBJECT_MAPPER .writerWithDefaultPrettyPrinter ().writeValueAsString (bean );
103+ return getMapper ( typing ) .writerWithDefaultPrettyPrinter ().writeValueAsString (bean );
75104 } catch (JsonProcessingException e ) {
76- throw new UtilException ("cast bean to json fail" , e );
105+ throw new UtilException ("Cast bean to json fail" , e );
77106 }
78107 }
79108
80109 /**
81- * 转换字符串到自定义对象
110+ * 转换字符串到自定义对象,指定 class
82111 *
83112 * @param json 需要转换的字符串
84113 * @param clazz 自定义对象的 class 对象
85114 * @return 自定义对象
86115 */
87- @ SuppressWarnings ("unchecked" )
88116 public static <T > T toBean (String json , Class <T > clazz ) {
117+ return toBean (json , false , clazz );
118+ }
119+
120+ /**
121+ * 转换字符串到集合对象,指定泛型类
122+ *
123+ * @param json 需要转换的字符串
124+ * @param typeReference 自定义对象的 TypeReference 对象
125+ * @return 自定义对象
126+ */
127+ public static <T > T toBean (String json , TypeReference <T > typeReference ) {
128+ return toBean (json , false , typeReference );
129+ }
130+
131+ /**
132+ * 转换字符串到自定义对象,序列化类型
133+ *
134+ * @param json 需要转换的字符串
135+ * @return 自定义对象
136+ */
137+ public static Object toBean (String json ) {
138+ return toBean (json , true , Object .class );
139+ }
140+
141+ /**
142+ * 转换字符串到自定义对象
143+ *
144+ * @param json 需要转换的字符串
145+ * @param typing 是否序列化类型
146+ * @param clazz 自定义对象的 class 对象
147+ * @return 自定义对象
148+ */
149+ @ SuppressWarnings ("unchecked" )
150+ public static <T > T toBean (String json , boolean typing , Class <T > clazz ) {
89151 if (StrUtil .isEmpty (json ) || Objects .isNull (clazz )) {
90152 return null ;
91153 }
92154 if (clazz .equals (String .class )) {
93155 return (T ) json ;
94156 }
95157 try {
96- return OBJECT_MAPPER .readValue (json , clazz );
158+ return getMapper ( typing ) .readValue (json , clazz );
97159 } catch (Exception e ) {
98- throw new UtilException ("cast json to bean fail" , e );
160+ throw new UtilException ("Cast json to bean fail" , e );
99161 }
100162 }
101163
102164 /**
103165 * 转换字符串到集合对象,可以保留泛型
104166 *
105167 * @param json 需要转换的字符串
168+ * @param typing 是否序列化类型
106169 * @param typeReference 自定义对象的 TypeReference 对象
107170 * @return 自定义对象
108171 */
109172 @ SuppressWarnings ("unchecked" )
110- public static <T > T toBean (String json , TypeReference <T > typeReference ) {
173+ public static <T > T toBean (String json , boolean typing , TypeReference <T > typeReference ) {
111174 if (StrUtil .isEmpty (json ) || Objects .isNull (typeReference )) {
112175 return null ;
113176 }
114177 if (typeReference .getType ().equals (String .class )) {
115178 return (T ) json ;
116179 }
117180 try {
118- return OBJECT_MAPPER .readValue (json , typeReference );
181+ return getMapper ( typing ) .readValue (json , typeReference );
119182 } catch (IOException e ) {
120- throw new UtilException ("cast json to bean fail" , e );
183+ throw new UtilException ("Cast json to bean fail" , e );
121184 }
122185 }
123186
124187 @ Override
125188 public void afterPropertiesSet () {
126- OBJECT_MAPPER = this . objectMapper ;
127- if (Objects .isNull (OBJECT_MAPPER )) {
189+ PROJECT_OBJECT_MAPPER = objectMapper ;
190+ if (Objects .isNull (PROJECT_OBJECT_MAPPER )) {
128191 log .warn ("Bean objectMapper not found, use default config by JacksonUtil" );
129- OBJECT_MAPPER = new ObjectMapper ();
192+ PROJECT_OBJECT_MAPPER = new ObjectMapper ();
130193 // 定义日期格式
131194 String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss" ;
132- // Jackson 详细
195+ // Jackson 详细配置
133196 // 列入对象的全部字段
134- OBJECT_MAPPER .setSerializationInclusion (JsonInclude .Include .ALWAYS );
197+ PROJECT_OBJECT_MAPPER .setSerializationInclusion (JsonInclude .Include .ALWAYS );
135198 // 取消默认转换 timestamps 形式
136- OBJECT_MAPPER .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
199+ PROJECT_OBJECT_MAPPER .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
137200 // 忽略空Bean转json的错误
138- OBJECT_MAPPER .configure (SerializationFeature .FAIL_ON_EMPTY_BEANS , false );
201+ PROJECT_OBJECT_MAPPER .configure (SerializationFeature .FAIL_ON_EMPTY_BEANS , false );
139202 // 统一所有的日期格式为以下的样式,即 yyyy-MM-dd HH:mm:ss
140- OBJECT_MAPPER .setDateFormat (new SimpleDateFormat (STANDARD_FORMAT ));
203+ PROJECT_OBJECT_MAPPER .setDateFormat (new SimpleDateFormat (STANDARD_FORMAT ));
141204 // 忽略在 json 字符串中存在,但是在 java 对象中不存在对应属性的情况。防止错误
142- OBJECT_MAPPER .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
205+ PROJECT_OBJECT_MAPPER .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
143206 }
207+ // 启用序列化类型
208+ TYPING_OBJECT_MAPPER = PROJECT_OBJECT_MAPPER .copy ();
209+ TYPING_OBJECT_MAPPER .activateDefaultTyping (TYPING_OBJECT_MAPPER .getPolymorphicTypeValidator (), DefaultTyping .NON_FINAL , As .PROPERTY );
144210 }
145211}
0 commit comments