13693261870
2024-12-30 edc9a6674eb9b40e33a74c5f022d279712ed3b7c
se-modules/se-system/src/main/java/com/se/system/utils/JsonUtils.java
@@ -1,42 +1,74 @@
package com.se.system.utils;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("ALL")
public class JsonUtils {
    @SuppressWarnings("unchecked")
    public static LinkedHashMap<String, Object> json2Map(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        return mapper.readValue(json, LinkedHashMap.class);
    public static final ObjectMapper OM = new ObjectMapper();
    static {
        OM.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        OM.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        OM.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        OM.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        OM.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    }
    public static String map2Json(Object obj) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        return mapper.writeValueAsString(obj);
    public JsonUtils() {
    }
    @SuppressWarnings("unchecked")
    public static List<LinkedHashMap<String, Object>> json2ListMap(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        return mapper.readValue(json, List.class);
    public static String objectToJson(Object data) {
        try {
            return OM.writeValueAsString(data);
        } catch (JsonProcessingException var2) {
            var2.printStackTrace();
            return null;
        }
    }
    public static <T> T json2Object(String json, Class<T> valueType) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        return mapper.readValue(json, valueType);
    public static String objectToJsonWithType(Object data, TypeReference typeReference) {
        try {
            return OM.writerFor(typeReference).writeValueAsString(data);
        } catch (JsonProcessingException var3) {
            var3.printStackTrace();
            return null;
        }
    }
    public static <T> T obj2Vo(Object obj, Class<T> valueType) {
        return JSONObject.parseObject(JSONObject.toJSONString(obj), valueType);
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            return OM.readValue(jsonData, beanType);
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = OM.getTypeFactory().constructParametricType(List.class, new Class[]{beanType});
        try {
            return (List) OM.readValue(jsonData, javaType);
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }
    public static Map<String, Object> parseMap(String jsonStr) throws IOException {
        return (Map) OM.readValue(jsonStr, Map.class);
    }
    public static List<String> parseList(String jsonStr) throws IOException {
        return (List) OM.readValue(jsonStr, new TypeReference<List<String>>() {
        });
    }
}