Browse Source

[Improvement][JSONUtils] Improvement JSONUtils parse object method (#4781)

* Improvement JSONUtils parse object method.

* update JSONUtils toMap method.

* rerun ut test.
pull/3/MERGE
zhuangchong 4 years ago committed by GitHub
parent
commit
18e4087c7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java

29
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java

@ -211,25 +211,13 @@ public class JSONUtils {
/** /**
* json to map * json to map
* <p>
* {@link #toMap(String, Class, Class)} * {@link #toMap(String, Class, Class)}
* *
* @param json json * @param json json
* @return json to map * @return json to map
*/ */
public static Map<String, String> toMap(String json) { public static Map<String, String> toMap(String json) {
if (StringUtils.isEmpty(json)) { return parseObject(json, new TypeReference<Map<String, String>>() {});
return null;
}
try {
return objectMapper.readValue(json, new TypeReference<Map<String, String>>() {
});
} catch (Exception e) {
logger.error("json to map exception!", e);
}
return null;
} }
/** /**
@ -243,13 +231,24 @@ public class JSONUtils {
* @return to map * @return to map
*/ */
public static <K, V> Map<K, V> toMap(String json, Class<K> classK, Class<V> classV) { public static <K, V> Map<K, V> toMap(String json, Class<K> classK, Class<V> classV) {
return parseObject(json, new TypeReference<Map<K, V>>() {});
}
/**
* json to object
*
* @param json json string
* @param type type reference
* @param <T>
* @return return parse object
*/
public static <T> T parseObject(String json, TypeReference<T> type) {
if (StringUtils.isEmpty(json)) { if (StringUtils.isEmpty(json)) {
return null; return null;
} }
try { try {
return objectMapper.readValue(json, new TypeReference<Map<K, V>>() { return objectMapper.readValue(json, type);
});
} catch (Exception e) { } catch (Exception e) {
logger.error("json to map exception!", e); logger.error("json to map exception!", e);
} }

Loading…
Cancel
Save