All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.doudou.core.util.JsonUtil Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package top.doudou.core.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import top.doudou.core.exception.ExceptionUtils;

import java.text.SimpleDateFormat;

/**
 * @Description json工具类
 * @version: 1.0
 * @Created 傻男人 <[email protected]>
 * @Date 2021-11-15 13:57
 */
@Slf4j
public class JsonUtil {

    private static ObjectMapper mapper;

    static{
        //jackson
        mapper = new ObjectMapper();

        //设置日期格式
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        //禁用空对象转换json
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        //设置null值不参与序列化(字段不被显示)
//        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    /**
     * json字符串转对象
     */
    public static  T parse(String jsonStr,Class clazz){
        try {
            return mapper.readValue(jsonStr, clazz);
        } catch (Exception e) {
            //输出到日志文件中
            log.error(ExceptionUtils.toString(e));
        }
        return null;
    }

    /**
     * 对象转json字符串
     */
    public static String stringify(Object obj){
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            //输出到日志文件中
            log.error(ExceptionUtils.toString(e));
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy