package com.usky.rule.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class JsonUtil {
    public static final ObjectMapper JSON_MAPPER = (new ObjectMapper()).findAndRegisterModules();
    public static final ObjectMapper IGNORE_UNKNOWN_PROPERTIES_JSON_MAPPER;

    private JsonUtil() {
    }

    public static String toJson(Object object) {
        try {
            return JSON_MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException(String.format("将对象转换为JSON出错,%s", e.getMessage()), e);
        }
    }

    public static String toJson(ObjectMapper mapper, Object object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException(String.format("将对象转换为JSON出错,%s", e.getMessage()), e);
        }
    }

    public static <T> T toObject(String content, Class<T> type) {
        return (T)toObject(JSON_MAPPER, content, type);
    }

    public static <T> T toObject(Map<String, Object> map, Class<T> type) {
        try {
            return (T)JSON_MAPPER.convertValue(map, type);
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format("将JSON转换为对象出错,%s", e.getMessage()), e);
        }
    }

    public static <T> T toObject(ObjectMapper mapper, String content, Class<T> type) {
        try {
            return (T)mapper.readValue(content, type);
        } catch (IOException e) {
            throw new IllegalArgumentException(String.format("将JSON转换为对象出错,%s", e.getMessage()), e);
        }
    }

    public static <T> T parse(String content, TypeReference<T> typeReference) {
        try {
            return (T)JSON_MAPPER.readValue(content, typeReference);
        } catch (IOException e) {
            throw new IllegalArgumentException(String.format("将JSON转换为对象出错,%s", e.getMessage()), e);
        }
    }

    public static <T> List<T> parseJsonArray(String content, Class<T> type) {
        try {
            return (List)JSON_MAPPER.readValue(content, JSON_MAPPER.getTypeFactory().constructCollectionType(List.class, type));
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException(String.format("将对象转换为JSON数组出错,%s", e.getMessage()), e);
        }
    }

    public static byte[] toByteArray(Object object) {
        try {
            return JSON_MAPPER.writeValueAsBytes(object);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException(String.format("将对象转换为JSON出错,%s", e.getMessage()), e);
        }
    }

    static {
        IGNORE_UNKNOWN_PROPERTIES_JSON_MAPPER = (new ObjectMapper()).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).findAndRegisterModules();
    }
}
