package cn.gyee.tamplate.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;

/**
 *@ClassName JsonUtils
 *@Description TODO
 *@Author 谢生杰
 *@Date 2020/10/13 16:14
 *@Version 1.0
 **/
public class JsonUtils {
    private static ObjectMapper mapper = new ObjectMapper();
    public static final String SIMPLE_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final SimpleDateFormat df = new SimpleDateFormat(SIMPLE_DATETIME_FORMAT);

    public static ObjectMapper getMapperBinder(){
        return mapper;
    }

    /**
     * 将各种形式转换为json格式
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将json串 转换为已知对象
     * @param json
     * @param clazz
     * @return
     */
    public static Object fromJson(String json, Class<?> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
