//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cn.anicert.secure.access.utils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class JSONUtil {
    private static final ObjectMapper mapper = new ObjectMapper();
    public static final JavaType[] EMPTY_TYPES = new JavaType[0];
    public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public JSONUtil() {
    }

    public static <T> T json2Object(String jsonStr, Class<T> clazz) {
        try {
            return mapper.readValue(jsonStr, clazz);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static String object2Json(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static String toJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static String toPrettyJason(Object obj) {
        try {
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static String object2DateFormatJson(Object obj) {
        return object2DateFormatJson(obj, "yyyy-MM-dd HH:mm:ss");
    }

    public static String object2DateFormatJson(Object obj, String dateFmt) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(Include.NON_NULL);
            mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
            mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.configure(Feature.ALLOW_COMMENTS, false);
            mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
            mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
            mapper.setDateFormat(new SimpleDateFormat(dateFmt));
            mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            mapper.registerModule(new JavaTimeModule());
            return mapper.writeValueAsString(obj);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static <T> List<T> json2ObjectList(String jsonString, Class<T> clazz) {
        try {
            if (jsonString == null) {
                return null;
            } else {
                List<T> list = (List)mapper.readValue(jsonString, new TypeReference<List<T>>() {
                });
                return list;
            }
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static List<String> json2StringList(String jsonString) {
        try {
            if (!"".equals(jsonString) && null != jsonString) {
                JsonNode jsonNode = mapper.readTree(jsonString);
                List<String> list = new ArrayList();
                jsonNode.forEach((node) -> {
                    list.add(node.toString());
                });
                return list;
            } else {
                return null;
            }
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static Map<String, Object> json2Map(String jsonString) {
        try {
            return (Map)mapper.readValue(jsonString, new TypeReference<Map<String, Object>>() {
            });
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static List<Map<String, Object>> json2MapList(String jsonString) {
        try {
            return (List)mapper.readValue(jsonString, new TypeReference<List<Map<String, Object>>>() {
            });
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static Object[] json2Array(String jsonString) {
        try {
            List<Object> objs = (List)mapper.readValue(jsonString, new TypeReference<List>() {
            });
            return objs.toArray();
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    public static JsonNode json2JSONArray(String jsonString) {
        try {
            JsonNode jsonNode = mapper.readTree(jsonString);
            JsonNodeType nodeType = jsonNode.getNodeType();
            if (nodeType.equals(JsonNodeType.ARRAY)) {
                return jsonNode;
            } else {
                throw new IllegalArgumentException("json字符串不是数组");
            }
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static String array2Json(String[] strs) {
        try {
            ArrayNode arrayNode = new ArrayNode(new JsonNodeFactory(false));
            String[] var2 = strs;
            int var3 = strs.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String str = var2[var4];
                JsonNode jsonNode = mapper.readTree(str);
                arrayNode.add(jsonNode);
            }

            return arrayNode.toString();
        } catch (Exception var7) {
            throw new RuntimeException(var7);
        }
    }

    public static <T> T[] jsonArrayToObjectArray(String jsonStr, Class<T[]> t) {
        try {
            T[] ts = (T[]) mapper.readValue(jsonStr, t);
            return ts;
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    static {
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(Feature.ALLOW_COMMENTS, false);
        mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
        mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
        mapper.setDateFormat(sdf);
        mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        mapper.registerModule(new JavaTimeModule());
    }
}
