package com.njcn.common.utils.serializer;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.njcn.common.utils.PubUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.Instant;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2023年07月24日 13:33
 */
@Component
public class InstantDateDeserializer extends StdDeserializer<Instant> {


    public InstantDateDeserializer() {
        this(null);
    }


    protected InstantDateDeserializer(Class<?> vc) {
        super(vc);
    }


    @Override
    public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String text = p.getValueAsString();
        return PubUtils.dateToInstant(DateUtil.parse(text,DatePattern.NORM_DATETIME_PATTERN));
    }
}
