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 a2a1080
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,26 @@
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.
*/
private final SimpleDateFormat[] dateFormatters
= new SimpleDateFormat[]{
new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss'Z'"),
new SimpleDateFormat("ss.SSS")
};
private final SimpleDateFormat[] dateFormatters = new SimpleDateFormat[]{
// RFC-3339
new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss'Z'"), new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"),

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

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"), 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:ss"),

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

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

/**
* Constructs a {@link CustomDateDeserializer} instance.
Expand Down Expand Up @@ -80,10 +92,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 a2a1080

Please sign in to comment.