[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:
parent
5025b5015f
commit
18e4087c7e
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue