|
|
@@ -6,6 +6,7 @@ import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import flexjson.JSONDeserializer;
|
|
|
import flexjson.JSONSerializer;
|
|
|
|
|
|
@@ -19,6 +20,8 @@ import flexjson.JSONSerializer;
|
|
|
*
|
|
|
*/
|
|
|
public class FlexJsonUtil {
|
|
|
+ private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
+
|
|
|
|
|
|
public static <T> T fromJson(String json, Class<?> cls) {
|
|
|
return new JSONDeserializer<T>().use(null, cls).deserialize(json);
|
|
|
@@ -59,4 +62,21 @@ public class FlexJsonUtil {
|
|
|
public static <T> List<T> fromJsonArray(String json, Class<?> cls) {
|
|
|
return new JSONDeserializer<List<T>>().use(null, ArrayList.class).use("values", cls).deserialize(json);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验 JSON 是否合法(Jackson 默认对非法转义如 \T 会抛异常)
|
|
|
+ */
|
|
|
+ public static boolean isValid(String json) {
|
|
|
+ if (json == null || json.trim().isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // readTree 会触发完整的词法和语法解析
|
|
|
+ mapper.readTree(json);
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 捕获 JsonParseException 等异常
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|