Skip to content

Commit

Permalink
Extend Java deserialization support for xen-api dates
Browse files Browse the repository at this point in the history
non-Zulu dates were not parsed correctly

Signed-off-by: Danilo Del Busso <danilo.delbusso@cloud.com>
  • Loading branch information
danilo-delbusso committed Jul 10, 2024
1 parent b0e0bab commit 001ea15
Showing 1 changed file with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,36 @@
public class CustomDateDeserializer extends StdDeserializer<Date> {

/**
* Array of {@link SimpleDateFormat} objects representing the custom date formats
* used in XenServer API responses.
* Array of {@link SimpleDateFormat} objects representing the date formats
* used in xen-api responses.
* RFC-3339 date formats can be returned in either Zulu or time zone agnostic.
* This list is not an exhaustive list of formats supported by RFC-3339, rather
* a set of formats that will enable the deserialization of xen-api dates.
* Formats are listed from in order of decreasing precision. When adding
* to this list, please ensure the order is kept.
*/
private final SimpleDateFormat[] dateFormatters
private final SimpleDateFormat[] dateFormatters = new SimpleDateFormat[]{
private static final SimpleDateFormat[] dateFormatters
= new SimpleDateFormat[]{

// RFC-3339
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"),
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"),
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"),

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"),
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ"),
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"),
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"),

new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss'Z'"),
new SimpleDateFormat("ss.SSS")
};
new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZZZZ"),
new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssX"),
new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"),

// Other
new SimpleDateFormat("ss.SSS"),};

/**
* Constructs a {@link CustomDateDeserializer} instance.
Expand Down Expand Up @@ -80,10 +102,10 @@ public CustomDateDeserializer(Class t) {
*/
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {

var text = jsonParser.getText();
for (SimpleDateFormat formatter : dateFormatters) {
try {
return formatter.parse(jsonParser.getText());
return formatter.parse(text);
} catch (ParseException e) {
// ignore
}
Expand Down

0 comments on commit 001ea15

Please sign in to comment.