package com.mortals.xhx.base.framework;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: zxfei
 * @date: 2022/6/30 10:49
 * @description:
 **/
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = jp.getText();

        if (!ObjectUtils.isEmpty(date)) {
            try {
                return format.parse(date);
            } catch (ParseException e) {
                return null;
            }
        } else {
            return null;
        }


    }
}
