package net.yyc.common.taxhelper.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * json工具类
 *
 * @author zhangwt
 * @since 1.0
 */
@Slf4j
public class JsonUtils {
    private JsonUtils() {
    }

    public static String serializer(Object obj) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(obj);
        } catch (IOException ex) {
            log.error("json序列化失败", ex);
        }

        return null;
    }

    public static <T> T deserializer(String json, Class<T> valueType) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            T response = mapper.readValue(json, valueType);
            return response;
        } catch (IOException ex) {
            log.error("json反序列化失败", ex);
        }

        return null;
    }
}
