Skip to content

Commit

Permalink
Merge branch 'custom-date' into template-layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalosh committed Jun 25, 2019
2 parents d5c9e27 + 53eef01 commit 18c8abd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.wildbit.java.postmark.client.data.model.messages;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.wildbit.java.postmark.client.jackson.CustomDateDeserializer;

import java.util.List;
import java.util.Date;
Expand All @@ -20,6 +22,8 @@ public class InboundMessage {
private String replyTo;
private String originalRecipient;
private String subject;

@JsonDeserialize(using = CustomDateDeserializer.class)
private Date date;
private String mailboxHash;
private String tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.wildbit.java.postmark.client.jackson;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
* In some of the responses from Postmark API, date format is not detected correctly by Jackson.
* This class handles additional custom date formats when deserializing data.
*/
public class CustomDateDeserializer extends StdDeserializer<Date> {
private static final long serialVersionUID = 1L;

private static final String[] DATE_FORMATS = new String[] {
"EEE, dd MMM yyyy HH:mm:ss zzz",
"dd MMM yyyy HH:mm:ss zzz",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
"yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy-MM-dd"
};


public CustomDateDeserializer() {
this(null);
}

public CustomDateDeserializer(Class<?> vc) {
super(vc);
}

@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
final String date = node.textValue();

for (String DATE_FORMAT : DATE_FORMATS) {
try {
return new SimpleDateFormat(DATE_FORMAT).parse(date);
} catch (ParseException e) { }
}

throw new JsonParseException(jsonParser, getParsingErrorMessage(date));
}

private String getParsingErrorMessage(String date) {
return "Not parsable date: \"" + date + "\". Supported formats: " + Arrays.toString(DATE_FORMATS);
}

}

0 comments on commit 18c8abd

Please sign in to comment.