|
@@ -5,6 +5,7 @@ import cn.hutool.core.util.ArrayUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
|
|
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
|
|
|
import com.vber.common.core.utils.SpringUtils;
|
|
import com.vber.common.core.utils.SpringUtils;
|
|
@@ -48,7 +49,6 @@ public class JsonUtils {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 将JSON格式的字符串转换为指定类型的对象
|
|
* 将JSON格式的字符串转换为指定类型的对象
|
|
|
*
|
|
*
|
|
@@ -69,7 +69,6 @@ public class JsonUtils {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 将字节数组转换为指定类型的对象
|
|
* 将字节数组转换为指定类型的对象
|
|
|
*
|
|
*
|
|
@@ -169,4 +168,58 @@ public class JsonUtils {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 判断字符串是否为合法 JSON(对象或数组)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param str 待校验字符串
|
|
|
|
|
+ * @return true = 合法 JSON,false = 非法或空
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean isJson(String str) {
|
|
|
|
|
+ if (StringUtils.isBlank(str)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ OBJECT_MAPPER.readTree(str);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 判断字符串是否为 JSON 对象({})
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param str 待校验字符串
|
|
|
|
|
+ * @return true = JSON 对象
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean isJsonObject(String str) {
|
|
|
|
|
+ if (StringUtils.isBlank(str)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ JsonNode node = OBJECT_MAPPER.readTree(str);
|
|
|
|
|
+ return node.isObject();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 判断字符串是否为 JSON 数组([])
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param str 待校验字符串
|
|
|
|
|
+ * @return true = JSON 数组
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean isJsonArray(String str) {
|
|
|
|
|
+ if (StringUtils.isBlank(str)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ JsonNode node = OBJECT_MAPPER.readTree(str);
|
|
|
|
|
+ return node.isArray();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|