package com.miaxis.common.utils;

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.SneakyThrows;

import java.io.IOException;
import java.util.Date;

public class DateDeserializer extends JsonDeserializer<Date> {

    @SneakyThrows
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
       if(StringUtils.isEmpty(jsonParser.getValueAsString())) {
           return null;
       }
       String value = jsonParser.getValueAsString();
       if(value.length()==10) {
           Date date = new Date(Long.parseLong(value+"000"));
           System.out.println(date);
           return date;
       } else {
           Date date = new Date(Long.parseLong(value));
           return date;
       }
    }

}
