1919import cn .hutool .core .text .CharSequenceUtil ;
2020import cn .hutool .extra .spring .SpringUtil ;
2121import com .fasterxml .jackson .core .JsonProcessingException ;
22- import com .fasterxml .jackson .core .type .TypeReference ;
2322import com .fasterxml .jackson .databind .JsonNode ;
2423import com .fasterxml .jackson .databind .ObjectMapper ;
24+ import top .continew .starter .json .jackson .exception .JSONException ;
2525
2626import java .io .IOException ;
2727import java .util .ArrayList ;
2828import java .util .List ;
29- import java .util .Map ;
3029
3130/**
32- * json 工具
31+ * JSON 工具类
32+ *
33+ * @see ObjectMapper
34+ * @see cn.hutool.json.JSONUtil
3335 *
3436 * @author echo
37+ * @author Charles7c
3538 * @since 2.11.0
3639 */
3740public class JSONUtils {
3841
42+ private static final ObjectMapper OBJECT_MAPPER = SpringUtil .getBean (ObjectMapper .class );
43+
3944 private JSONUtils () {
4045 }
4146
4247 /**
43- * Jackson 对象映射器,用于 JSON 解析与序列化。
44- */
45- private static final ObjectMapper OBJECT_MAPPER = SpringUtil .getBean (ObjectMapper .class );
46-
47- /**
48- * 获取 Jackson 对象映射器。
48+ * 获取 Jackson 对象映射器
4949 *
5050 * @return {@link ObjectMapper} Jackson 对象映射器
5151 */
@@ -54,183 +54,136 @@ public static ObjectMapper getObjectMapper() {
5454 }
5555
5656 /**
57- * 对象转为 json 字符串
57+ * 转换对象为JsonNode<br>
5858 *
59- * @param object 对象
60- * @return {@link String }
59+ * @param obj 对象
60+ * @return JsonNode
6161 */
62- public static String toJsonStr (Object object ) {
63- if (object == null ) {
62+ public static JsonNode parseObj (Object obj ) {
63+ if (obj == null ) {
6464 return null ;
6565 }
66- try {
67- return OBJECT_MAPPER .writeValueAsString (object );
68- } catch (JsonProcessingException e ) {
69- throw new RuntimeException (e );
70- }
66+ return OBJECT_MAPPER .valueToTree (obj );
7167 }
7268
7369 /**
74- * 将对象转换为 JsonNode。
70+ * 转换为JSON字符串
7571 *
76- * @param obj 需要转换的对象
77- * @return 转换后的 {@link JsonNode},如果 obj 为空,则返回 null
72+ * @param obj 被转为JSON的对象
73+ * @return JSON字符串
7874 */
79- public static JsonNode toJson (Object obj ) {
75+ public static String toJsonStr (Object obj ) {
8076 if (obj == null ) {
8177 return null ;
8278 }
8379 try {
84- return OBJECT_MAPPER .valueToTree (obj );
85- } catch (IllegalArgumentException e ) {
86- throw new IllegalArgumentException (e );
87- }
88- }
89-
90- /**
91- * 将 List 转换为 JsonNode。
92- *
93- * @param list 输入的 List
94- * @return 转换后的 {@link JsonNode}
95- */
96- public static JsonNode listToJson (List <?> list ) {
97- return toJson (list );
98- }
99-
100- /**
101- * 将 Map 转换为 JsonNode。
102- *
103- * @param map 输入的 Map
104- * @return 转换后的 {@link JsonNode}
105- */
106- public static JsonNode mapToJson (Map <?, ?> map ) {
107- return toJson (map );
108- }
109-
110- /**
111- * 将 JsonNode 转换为 List<String>,用于环境变量格式解析。
112- *
113- * @param jsonNode 需要转换的 JsonNode
114- * @return 转换后的 List<String>
115- */
116- public static List <String > jsonToEnvList (JsonNode jsonNode ) {
117- if (jsonNode == null || jsonNode .isNull ()) {
118- return new ArrayList <>();
80+ return OBJECT_MAPPER .writeValueAsString (obj );
81+ } catch (JsonProcessingException e ) {
82+ throw new JSONException (e );
11983 }
120- List <String > envList = new ArrayList <>();
121- jsonNode .fields ().forEachRemaining (field -> {
122- String key = field .getKey ();
123- JsonNode valueNode = field .getValue ();
124- String value = valueNode .isValueNode () ? valueNode .asText () : valueNode .toString ();
125- envList .add (key + "=" + value );
126- });
127- return envList ;
12884 }
12985
13086 /**
131- * 将 JsonNode 转换为 List<String>。
87+ * JSON字符串转为实体类对象,转换异常将被抛出
13288 *
133- * @param jsonNode 需要转换的 JsonNode
134- * @return 转换后的 List<String>
89+ * @param <T> Bean类型
90+ * @param jsonString JSON字符串
91+ * @param beanClass 实体类对象
92+ * @return 实体类对象
13593 */
136- public static List < String > jsonToStringList ( JsonNode jsonNode ) {
137- if (jsonNode == null || jsonNode . isNull ( )) {
138- return new ArrayList <>() ;
94+ public static < T > T toBean ( String jsonString , Class < T > beanClass ) {
95+ if (CharSequenceUtil . isBlank ( jsonString )) {
96+ return null ;
13997 }
14098 try {
141- return OBJECT_MAPPER .convertValue (jsonNode , new TypeReference <>() {
142- });
143- } catch (IllegalArgumentException e ) {
144- throw new IllegalArgumentException (e );
99+ return OBJECT_MAPPER .readValue (jsonString , beanClass );
100+ } catch (IOException e ) {
101+ throw new JSONException (e );
145102 }
146103 }
147104
148105 /**
149- * 将 JsonNode 转换为指定类型的 Java 对象。
106+ * 将JSON字符串转换为Bean的List,默认为ArrayList
150107 *
151- * @param jsonNode JSON 数据
152- * @param clazz 目标 Java 类
153- * @return 解析后的 Java 对象
108+ * @param jsonStr 需要转换的JSON字符串
109+ * @param elementType 列表元素类型
110+ * @return 转换后的 List
111+ * @since 2.13.2
154112 */
155- public static <T > T fromJson (JsonNode jsonNode , Class <T > clazz ) {
113+ public static <T > List <T > toList (String jsonStr , Class <T > elementType ) {
114+ if (jsonStr == null || jsonStr .trim ().isEmpty ()) {
115+ return new ArrayList <>(0 );
116+ }
156117 try {
157- return OBJECT_MAPPER .treeToValue (jsonNode , clazz );
158- } catch (JsonProcessingException e ) {
159- throw new RuntimeException (e );
118+ return OBJECT_MAPPER .readValue (jsonStr , OBJECT_MAPPER .getTypeFactory ()
119+ .constructCollectionType (List .class , elementType ));
120+ } catch (IOException e ) {
121+ throw new JSONException ("Failed to parse JSON string to list" , e );
160122 }
161123 }
162124
163125 /**
164- * 解析 JSON 字符串为 Java 对象。
126+ * 将JSONArray转换为Bean的List,默认为ArrayList
165127 *
166- * @param str JSON 字符串
167- * @param clazz 目标 Java 类
168- * @return 解析后的 Java 对象
128+ * @param jsonNode 需要转换的 JsonNode
129+ * @param elementType 列表元素类型
130+ * @return 转换后的 List
131+ * @since 2.13.2
169132 */
170- public static <T > T parseObject ( String str , Class <T > clazz ) {
171- if (CharSequenceUtil . isEmpty ( str )) {
172- return null ;
133+ public static <T > List < T > toList ( JsonNode jsonNode , Class <T > elementType ) {
134+ if (jsonNode == null || jsonNode . isNull ( )) {
135+ return new ArrayList <>( 0 ) ;
173136 }
174137 try {
175- return OBJECT_MAPPER .readValue (str , clazz );
176- } catch (IOException e ) {
177- throw new RuntimeException (e );
138+ return OBJECT_MAPPER .convertValue (jsonNode , OBJECT_MAPPER .getTypeFactory ()
139+ .constructCollectionType (List .class , elementType ));
140+ } catch (IllegalArgumentException e ) {
141+ throw new JSONException ("Failed to convert JSON to list" , e );
178142 }
179143 }
180144
181145 /**
182- * 字符串 解析为 list<T>
146+ * 是否为JSON类型字符串,首尾都为大括号或中括号判定为JSON字符串
183147 *
184- * @param str 字符串
185- * @param clazz 目标 Java 类
186- * @return 解析后的 List<T>
148+ * @param str 字符串
149+ * @return 是否为JSON类型字符串
150+ * @since 2.13.2
151+ * @see cn.hutool.json.JSONUtil#isTypeJSON(String)
152+ * @author Looly(<a href="https://gitee.com/dromara/hutool">Hutool</a>)
187153 */
188- public static <T > List <T > parseArray (String str , Class <T > clazz ) {
189- if (CharSequenceUtil .isEmpty (str )) {
190- return new ArrayList <>();
191- }
192- try {
193- return OBJECT_MAPPER .readValue (str , OBJECT_MAPPER .getTypeFactory ()
194- .constructCollectionType (List .class , clazz ));
195- } catch (IOException e ) {
196- throw new RuntimeException (e );
197- }
154+ public static boolean isTypeJSON (String str ) {
155+ return isTypeJSONObject (str ) || isTypeJSONArray (str );
198156 }
199157
200158 /**
201- * 判断字符串是否为 JSON 格式。
159+ * 是否为JSONObject类型字符串,首尾都为大括号判定为JSONObject字符串
202160 *
203161 * @param str 字符串
204- * @return 是否为 JSON 格式
162+ * @return 是否为JSON字符串
163+ * @since 2.13.2
164+ * @see cn.hutool.json.JSONUtil#isTypeJSONObject(String)
165+ * @author Looly(<a href="https://gitee.com/dromara/hutool">Hutool</a>)
205166 */
206- public static boolean isTypeJSON (String str ) {
207- if (CharSequenceUtil .isEmpty (str )) {
208- return false ;
209- }
210- try {
211- OBJECT_MAPPER .readTree (str );
212- return true ;
213- } catch (IOException e ) {
167+ public static boolean isTypeJSONObject (String str ) {
168+ if (CharSequenceUtil .isBlank (str )) {
214169 return false ;
215170 }
171+ return CharSequenceUtil .isWrap (CharSequenceUtil .trim (str ), '{' , '}' );
216172 }
217173
218174 /**
219- * 将 JSON 字符串转换为指定类型的 Java 对象。
175+ * 是否为JSONArray类型的字符串,首尾都为中括号判定为JSONArray字符串
220176 *
221- * @param str 字符串
222- * @param clazz 目标对象的 Class 类型
223- * @return 解析后的 Java 对象
177+ * @param str 字符串
178+ * @return 是否为JSONArray类型字符串
179+ * @since 2.13.2
180+ * @see cn.hutool.json.JSONUtil#isTypeJSONArray(String)
181+ * @author Looly(<a href="https://gitee.com/dromara/hutool">Hutool</a>)
224182 */
225- public static <T > T toBean (String str , Class <T > clazz ) {
226- if (CharSequenceUtil .isEmpty (str )) {
227- return null ;
228- }
229- try {
230- return OBJECT_MAPPER .readValue (str , clazz );
231- } catch (IOException e ) {
232- throw new RuntimeException (e );
183+ public static boolean isTypeJSONArray (String str ) {
184+ if (CharSequenceUtil .isBlank (str )) {
185+ return false ;
233186 }
187+ return CharSequenceUtil .isWrap (CharSequenceUtil .trim (str ), '[' , ']' );
234188 }
235-
236189}
0 commit comments