Skip to content

Commit

Permalink
Merge pull request #11 from kit-data-manager/dev
Browse files Browse the repository at this point in the history
Added some more logging to CustomInstantDeserializer
Removed plugins requiring JGit for preserving JDK < 11 compatibility
  • Loading branch information
ThomasJejkal authored Nov 30, 2021
2 parents 9ec440b + d23f379 commit 1d6c184
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ plugins {
id "io.freefair.lombok" version "6.1.0"
id "io.freefair.maven-publish-java" version "6.1.0"
id "io.spring.dependency-management" version "1.0.7.RELEASE"
id "com.github.kt3k.coveralls" version "2.8.1"
//id "com.github.kt3k.coveralls" version "2.8.1"
id "org.owasp.dependencycheck" version "3.1.2"
id "org.ajoberstar.grgit" version "2.0.1"
//id "org.ajoberstar.grgit" version "2.0.1"
id "com.jfrog.bintray" version "1.7.3"
id "java"
id "jacoco"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author jejkal
*/
public class CustomInstantDeserializer extends JsonDeserializer<Instant> {

private static final Logger LOGGER = LoggerFactory.getLogger(CustomInstantDeserializer.class);

private final DateTimeFormatter isoFormat = DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC);
//additional date patterns according to DataCite JSON spec ordered by probability of use
private final DateTimeFormatter[] additionalFormats = new DateTimeFormatter[]{
Expand All @@ -53,23 +57,26 @@ public class CustomInstantDeserializer extends JsonDeserializer<Instant> {

@Override
public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
if (p.getText() == null || p.getText().length() == 0) {
String text = p.getText();
if (text == null || text.length() == 0) {
return null;
}

try {
return Instant.from(isoFormat.parse(p.getText())).truncatedTo(ChronoUnit.MILLIS);
LOGGER.trace("Trying to parse date {} using ISO_DATE_TIME.", text);
return Instant.from(isoFormat.parse(text)).truncatedTo(ChronoUnit.MILLIS);
} catch (DateTimeParseException ex) {
//no iso format...continue with other formats
LOGGER.trace("Date {} is no ISO_DATE_TIME, trying alternatives.", text);
}
for (DateTimeFormatter formatter : additionalFormats) {
try {
LocalDate date = LocalDate.from(formatter.parse(p.getText()));
LOGGER.trace("Parsing date {} using formatter {}.", text, formatter);
LocalDate date = LocalDate.from(formatter.parse(text));
return Instant.from(date.atStartOfDay(ZoneOffset.UTC).toInstant());
} catch (DateTimeParseException ex) {
//no valid date according to the current format, continue if possible
} catch (DateTimeException ex) {
ex.printStackTrace();
//no valid date according to the current format, continue if possible
LOGGER.trace("Date {} cannot be parsed using formatter {}.", text, formatter);
}
}
throw new DateTimeParseException("Invalid date string. Supported format patterns are: yyyy-MM-dd'T'HH:mm:ss'Z', yyyy, yyyy-MM-dd and yyyy-MM", p.getText(), 0);
Expand Down

0 comments on commit 1d6c184

Please sign in to comment.