I'm trying to parse timestamps in the RFC3339 format in a JSON-string using Jackson. How do I allow for the variable amount of decimal places after the seconds?
For the JSON file
{
"timestamp": "2019-07-02T13:00:34.836+02:00"
}
I've deserialized it with the class
public abstract class Attribute {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
public Date timestamp;
}
and an ObjectMapper with the JavaTimeModule:
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
mapper.readValue(jsonFile, Attribute.class);
This works fine. However, it should also work for "timestamp": "2019-07-02T13:00:34+02:00" and "timestamp": "2019-07-02T13:00:34.090909090+02:00". I found this answer showing how to parse such strings with a DateTimeFormatter, but as far as I can tell, @JsonFormat only takes a SimpleDateFormat string, which do not have support for variable amounts of second decimals.
Removing the pattern-property all together, so the annotation becomes
@JsonFormat(shape = JsonFormat.Shape.STRING)
allows me to parse the incoming dates, but also accepts non-RFC3339 timestamps like 1990-01-01T12:53:01-0110 (missing a colon in the timezone).