All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kyojo.schemaorg.m3n3.gson.LocalDateDeserializer Maven / Gradle / Ivy

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;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy