package com.jay.monitor.data.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * JSON工具处理类
 *
 * @author liukaixiong
 * @date 2022/7/14 - 9:18
 */
public class JsonHelper {
    private final static Logger LOGGER = LoggerFactory.getLogger(JsonHelper.class);

    public static String toJsonString(Object obj) {

        if (obj == null) {
            return null;
        }

        try {
            if (obj instanceof String) {
                return (String)obj;
            }
            return JSON.toJSONString(obj);
        } catch (Exception e) {
            LOGGER.warn("json 转换异常 : {}", e.getMessage());
            return obj.toString();
        }
    }

    public static <T> T parseObject(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
    }

    public static String toPrettyJsonString(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.SkipTransientField);
    }
}
