package com.cirle.scientific.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.google.common.base.Strings;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 反序列化LocalDateTime
 *
 * @author heihaier@mmears.com
 * Date: 2018/8/27
 * Time: 下午8:15
 */
public class LocalDateTimeJsonDeserializer extends JsonDeserializer<LocalDateTime> {

    /**
     * 反序列化LocalDateTime
     *
     * @param p
     * @param ctxt
     * @return
     * @throws IOException
     */
    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return !Objects.isNull(p) && !Strings.isNullOrEmpty(p.getText()) ?
                LocalDateTime.parse(p.getText(), DatePatternConst.DATE_TIME_FORMATTER) : null;
    }
}
