net.nemerosa.ontrack.json.JDKLocalDateTimeDeserializer Maven / Gradle / Ivy
package net.nemerosa.ontrack.json;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class JDKLocalDateTimeDeserializer extends JsonDeserializer {
@Override
public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
String s = jp.readValueAs(String.class);
if (StringUtils.isNotBlank(s)) {
// Tries first with local date time
try {
return LocalDateTime.parse(s, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
} catch (DateTimeParseException ignored) {
// ... with a time zone
return LocalDateTime.ofInstant(Instant.parse(s), ZoneOffset.UTC);
}
} else {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy