org.kyojo.schemaorg.m3n3.gson.LocalDateDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kyojo-schemaorg-m3n3-gson Show documentation
Show all versions of kyojo-schemaorg-m3n3-gson Show documentation
A Java implementation of schema.org data structure
The newest version!
package org.kyojo.schemaorg.m3n3.gson;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import org.kyojo.gson.JsonDeserializationContext;
import org.kyojo.gson.JsonDeserializer;
import org.kyojo.gson.JsonElement;
import org.kyojo.gson.JsonParseException;
public class LocalDateDeserializer implements JsonDeserializer {
@Override
public LocalDate deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
throws JsonParseException {
if(jsonElement.isJsonNull() || jsonElement.getAsString().equals("")) {
return null;
}
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
return LocalDate.parse(jsonElement.getAsString(), dtf);
} catch(DateTimeParseException dtpe1) {
return null;
}
}
}