love.keeping.starter.web.utils.JsonUtil Maven / Gradle / Ivy
The newest version!
package love.keeping.starter.web.utils;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import love.keeping.starter.common.exceptions.impl.DefaultSysException;
import love.keeping.starter.common.utils.StringUtil;
import love.keeping.starter.web.common.utils.ApplicationUtil;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class JsonUtil extends JSONUtil {
private static final ObjectMapper OBJECT_MAPPER = ApplicationUtil.getBean(ObjectMapper.class);
public static String toJsonString(Object obj) {
if (obj == null) {
return null;
}
try {
return OBJECT_MAPPER.writer().writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
throw new DefaultSysException(e.getMessage());
}
}
public static T parseObject(String jsonStr, Class clazz) {
if (StringUtil.isEmpty(jsonStr) || clazz == null) {
return null;
}
try {
return OBJECT_MAPPER.readValue(jsonStr, clazz);
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
throw new DefaultSysException(e.getMessage());
}
}
public static Map parseMap(String jsonStr, Class keyClazz, Class valueClazz) {
if (StringUtil.isEmpty(jsonStr) || keyClazz == null || valueClazz == null) {
return null;
}
try {
return OBJECT_MAPPER.readValue(jsonStr,
OBJECT_MAPPER.getTypeFactory().constructMapType(Map.class, keyClazz, valueClazz));
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
throw new DefaultSysException(e.getMessage());
}
}
public static List parseList(String jsonStr, Class clazz) {
if (StringUtil.isEmpty(jsonStr) || clazz == null) {
return null;
}
try {
return OBJECT_MAPPER.readValue(jsonStr,
OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
throw new DefaultSysException(e.getMessage());
}
}
public static T convert(Object obj, Class clazz) {
if (obj == null || clazz == null) {
return null;
}
return OBJECT_MAPPER.convertValue(obj, clazz);
}
public static boolean isJsonObject(String jsonStr) {
if (StringUtil.isBlank(jsonStr)) {
return false;
}
try {
JsonNode jsonNode = OBJECT_MAPPER.readTree(jsonStr);
return jsonNode.getNodeType() != JsonNodeType.ARRAY;
} catch (JsonProcessingException e) {
return false;
}
}
public static boolean isJsonArray(String jsonStr) {
if (StringUtil.isBlank(jsonStr)) {
return false;
}
try {
JsonNode jsonNode = OBJECT_MAPPER.readTree(jsonStr);
return jsonNode.getNodeType() == JsonNodeType.ARRAY;
} catch (JsonProcessingException e) {
return false;
}
}
public static boolean isJson(String jsonStr) {
if (StringUtil.isBlank(jsonStr)) {
return false;
}
try {
OBJECT_MAPPER.readTree(jsonStr);
} catch (JsonProcessingException e) {
return false;
}
return true;
}
}