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

package com.ruoyi.xq.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
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 com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonUtils {
    private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);
    private static final ObjectMapper NON_NULL_MAPPER = new ObjectMapper();
    private static final ObjectMapper INCLUDE_NULL_MAPPER = new ObjectMapper();

    public JsonUtils() {
    }

    public static String toJSONString(Object object) {
        return toJSONString(NON_NULL_MAPPER, object);
    }

    public static String toJSONStringWithNull(Object object) {
        return toJSONString(INCLUDE_NULL_MAPPER, object);
    }

    public static String toJSONString(ObjectMapper objectMapper, Object object) {
        StringWriter out = new StringWriter();
        JsonGenerator json = null;

        try {
            json = objectMapper.getFactory().createGenerator(out);
            json.writeObject(object);
            json.flush();
        } catch (Exception var12) {
            throw new IllegalArgumentException(var12);
        } finally {
            if (json != null) {
                try {
                    json.close();
                } catch (IOException var11) {
                }
            }

        }

        return out.toString();
    }

    public static <T> List<T> parseArray(String json, Class<T> clazz) {
        JavaType javaType = NON_NULL_MAPPER.getTypeFactory().constructParametricType(List.class, new Class[]{clazz});

        try {
            return (List)NON_NULL_MAPPER.readValue(json, javaType);
        } catch (IOException var4) {
            throw new RuntimeException("反序列失败", var4);
        }
    }

    public static <T> T parseObject(String json, Class<T> clazz) {
        JsonParser parser = null;
        T object = null;

        try {
            parser = NON_NULL_MAPPER.getFactory().createParser(json);
            object = parser.readValueAs(clazz);
        } catch (Exception var12) {
            throw new IllegalArgumentException(var12);
        } finally {
            if (parser != null) {
                try {
                    parser.close();
                } catch (IOException var11) {
                }
            }

        }

        return object;
    }

    public static <T> T parseObject(String json, TypeReference<T> valueTypeRef) {
        JsonParser parser = null;
        T object = null;

        try {
            parser = NON_NULL_MAPPER.getFactory().createParser(json);
            object = parser.readValueAs(valueTypeRef);
        } catch (Exception var12) {
            throw new IllegalArgumentException(var12);
        } finally {
            if (parser != null) {
                try {
                    parser.close();
                } catch (IOException var11) {
                }
            }

        }

        return object;
    }

    static {
        NON_NULL_MAPPER.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
        NON_NULL_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        NON_NULL_MAPPER.setSerializationInclusion(Include.NON_NULL);
        NON_NULL_MAPPER.registerModule(new JavaTimeModule()).registerModule(new Jdk8Module());
        INCLUDE_NULL_MAPPER.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
        INCLUDE_NULL_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        INCLUDE_NULL_MAPPER.registerModule(new JavaTimeModule()).registerModule(new Jdk8Module());
    }



}
