cn.takujo.common_api.util.JsonUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-api Show documentation
Show all versions of common-api Show documentation
takujoframework common-api
package cn.takujo.common_api.util;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;
import cn.takujo.common_api.exception.JsonException;
public final class JsonUtil {
public static Map toMap(String json) throws JsonException {
ObjectMapper om = new ObjectMapper();
try {
MapType mapType = om.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
return om.readValue(json, mapType);
} catch (Exception e) {
throw new JsonException("json01", "json to map 失败");
}
}
public static String toString(Map map) throws JsonException {
ObjectMapper om = new ObjectMapper();
try {
return om.writeValueAsString(map);
} catch (JsonProcessingException e) {
throw new JsonException("json02", "map to json失败");
}
}
}