package it.integry.integrywmsnative.core.rest.deserializer;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.core.utility.UtilityString;

public class LocalDateDeserializer implements JsonDeserializer<LocalDate> {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(UtilityDate.COMMONS_DATE_FORMATS.DMY_TIME_SLASH);


    @Override
    public LocalDate deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
        String date = element.getAsString();
        if (UtilityString.isNullOrEmpty(date)) return null;

        return LocalDate.parse(date, formatter);
    }
}
