2525import static java .nio .charset .StandardCharsets .UTF_8 ;
2626import static org .apache .dolphinscheduler .common .constants .DateConstants .YYYY_MM_DD_HH_MM_SS ;
2727
28+ import org .apache .dolphinscheduler .common .constants .SystemConstants ;
29+
2830import org .apache .commons .lang3 .StringUtils ;
2931
3032import java .io .IOException ;
6971@ Slf4j
7072public final class JSONUtils {
7173
72- static {
73- log .info ("init timezone: {}" , TimeZone .getDefault ());
74- }
75-
7674 private static final ObjectMapper objectMapper = JsonMapper .builder ()
7775 .configure (FAIL_ON_UNKNOWN_PROPERTIES , false )
7876 .configure (ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT , true )
@@ -83,7 +81,7 @@ public final class JSONUtils {
8381 .addModule (new SimpleModule ()
8482 .addSerializer (LocalDateTime .class , new LocalDateTimeSerializer ())
8583 .addDeserializer (LocalDateTime .class , new LocalDateTimeDeserializer ()))
86- .defaultTimeZone (TimeZone . getDefault () )
84+ .defaultTimeZone (SystemConstants . DEFAULT_TIME_ZONE )
8785 .defaultDateFormat (new SimpleDateFormat (YYYY_MM_DD_HH_MM_SS ))
8886 .build ();
8987
@@ -110,7 +108,7 @@ public static JsonNode toJsonNode(Object obj) {
110108 /**
111109 * json representation of object
112110 *
113- * @param object object
111+ * @param object object
114112 * @param feature feature
115113 * @return object to json string
116114 */
@@ -133,31 +131,34 @@ public static String toJsonString(Object object, SerializationFeature feature) {
133131 * the fields of the specified object are generics, just the object itself should not be a
134132 * generic type.
135133 *
136- * @param json the string from which the object is to be deserialized
134+ * @param json the string from which the object is to be deserialized
137135 * @param clazz the class of T
138- * @param <T> T
136+ * @param <T> T
139137 * @return an object of type T from the string
140138 * classOfT
141139 */
142140 public static @ Nullable <T > T parseObject (String json , Class <T > clazz ) {
141+ if (clazz == null ) {
142+ throw new IllegalArgumentException ("Class type cannot be null" );
143+ }
144+
143145 if (Strings .isNullOrEmpty (json )) {
144146 return null ;
145147 }
146148
147149 try {
148150 return objectMapper .readValue (json , clazz );
149151 } catch (Exception e ) {
150- log . error ("Parse object exception, jsonStr: {}, class: {}" , json , clazz , e );
152+ throw new IllegalArgumentException ("Parse json: " + json + " to class: " + clazz . getName () + " failed" , e );
151153 }
152- return null ;
153154 }
154155
155156 /**
156- * deserialize
157+ * deserialize
157158 *
158- * @param src byte array
159+ * @param src byte array
159160 * @param clazz class
160- * @param <T> deserialize type
161+ * @param <T> deserialize type
161162 * @return deserialize type
162163 */
163164 public static <T > T parseObject (byte [] src , Class <T > clazz ) {
@@ -171,12 +172,16 @@ public static <T> T parseObject(byte[] src, Class<T> clazz) {
171172 /**
172173 * json to list
173174 *
174- * @param json json string
175+ * @param json json string
175176 * @param clazz class
176- * @param <T> T
177+ * @param <T> T
177178 * @return list
178179 */
179180 public static <T > List <T > toList (String json , Class <T > clazz ) {
181+ if (clazz == null ) {
182+ throw new IllegalArgumentException ("Class type cannot be null" );
183+ }
184+
180185 if (Strings .isNullOrEmpty (json )) {
181186 return Collections .emptyList ();
182187 }
@@ -185,10 +190,10 @@ public static <T> List<T> toList(String json, Class<T> clazz) {
185190 CollectionType listType = objectMapper .getTypeFactory ().constructCollectionType (ArrayList .class , clazz );
186191 return objectMapper .readValue (json , listType );
187192 } catch (Exception e ) {
188- log .error ("parse list exception!" , e );
193+ throw new IllegalArgumentException (
194+ "Parse json: " + json + " to list of class: " + clazz .getName () + " failed" , e );
189195 }
190196
191- return Collections .emptyList ();
192197 }
193198
194199 /**
@@ -222,7 +227,7 @@ public static boolean checkJsonValid(String json, Boolean logFlag) {
222227 * node or its child nodes, and returning value it has.
223228 * If no matching field is found in this node or its descendants, returns null.
224229 *
225- * @param jsonNode json node
230+ * @param jsonNode json node
226231 * @param fieldName Name of field to look for
227232 * @return Value of first matching node found, if any; null if none
228233 */
@@ -238,7 +243,6 @@ public static String findValue(JsonNode jsonNode, String fieldName) {
238243
239244 /**
240245 * json to map
241- * {@link #toMap(String, Class, Class)}
242246 *
243247 * @param json json
244248 * @return json to map
@@ -248,34 +252,10 @@ public static Map<String, String> toMap(String json) {
248252 });
249253 }
250254
251- /**
252- * json to map
253- *
254- * @param json json
255- * @param classK classK
256- * @param classV classV
257- * @param <K> K
258- * @param <V> V
259- * @return to map
260- */
261- public static <K , V > Map <K , V > toMap (String json , Class <K > classK , Class <V > classV ) {
262- if (Strings .isNullOrEmpty (json )) {
263- return Collections .emptyMap ();
264- }
265-
266- try {
267- return objectMapper .readValue (json , new TypeReference <Map <K , V >>() {
268- });
269- } catch (Exception e ) {
270- log .error ("json to map exception!" , e );
271- }
272-
273- return Collections .emptyMap ();
274- }
275-
276255 /**
277256 * from the key-value generated json to get the str value no matter the real type of value
278- * @param json the json str
257+ *
258+ * @param json the json str
279259 * @param nodeName key
280260 * @return the str value of key
281261 */
@@ -305,13 +285,15 @@ public static <T> T parseObject(String json, TypeReference<T> type) {
305285 return null ;
306286 }
307287
288+ if (type == null ) {
289+ throw new IllegalArgumentException ("Type reference cannot be null" );
290+ }
291+
308292 try {
309293 return objectMapper .readValue (json , type );
310294 } catch (Exception e ) {
311- log . error ( " json to map exception! " , e );
295+ throw new IllegalArgumentException ( "Parse json: " + json + " to type: " + type . getType () + " failed " , e );
312296 }
313-
314- return null ;
315297 }
316298
317299 /**
@@ -347,14 +329,12 @@ public static <T> byte[] toJsonByteArray(T obj) {
347329 if (obj == null ) {
348330 return null ;
349331 }
350- String json = "" ;
351332 try {
352- json = toJsonString (obj );
333+ return toJsonString (obj ). getBytes ( UTF_8 );
353334 } catch (Exception e ) {
354- log . error ( " json serialize exception." , e );
335+ throw new IllegalArgumentException ( "Object: " + obj + " to json serialization exception." , e );
355336 }
356337
357- return json .getBytes (UTF_8 );
358338 }
359339
360340 public static ObjectNode parseObject (String text ) {
0 commit comments