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

* Improvement JSONUtils parse object method.

* update JSONUtils toMap method.

* rerun ut test.
This commit is contained in:
zhuangchong 2021-02-16 20:22:01 +08:00 committed by GitHub
parent 5025b5015f
commit 18e4087c7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 15 deletions

View File

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