io.voucherify.client.json.deserializer.DateDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of voucherify-java-sdk Show documentation
Show all versions of voucherify-java-sdk Show documentation
Voucherify-java-sdk is a Java client (can be used in Android application as well) which was created to simplify integration with Voucherify backend (http://www.voucherify.io)
package io.voucherify.client.json.deserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import io.voucherify.client.error.VoucherifyError;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DateDeserializer extends JsonDeserializer {
private final List dateFormats = new ArrayList();
public DateDeserializer(String... dateFormats) {
for (String df : dateFormats) {
this.dateFormats.add(new SimpleDateFormat(df));
}
}
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
if (jsonParser != null) {
return parseDate(jsonParser.getText());
}
return null;
}
private Date parseDate(String str) {
if (dateFormats.isEmpty()) {
throw VoucherifyError.from("No date format provided");
}
for (DateFormat df : dateFormats) {
try {
return df.parse(str);
} catch (ParseException e) {
// ignore and check next
}
}
throw VoucherifyError.from("Invalid date format: " + str);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy