package cn.superdesk.uniorder.common.json.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/10/26 下午7:15
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/26 下午7:15
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
public class DateConvertDeserializer extends JsonDeserializer<Date> {

	private static final String pattern = "yyyy-MM-dd";

	@Override
	public Date deserialize(JsonParser jsonParser, DeserializationContext dc) throws JsonProcessingException {
		Date date = null;
		try {
			DateFormat dateFormat = new SimpleDateFormat(pattern);
			String     val        = jsonParser.getText();
			date = dateFormat.parse(val);
		} catch (ParseException | IOException pex) {
			throw new RuntimeException("json转换Date异常,格式:" + pattern);
		}
		return date;
	}

}
