I need to handle an older set of data files, where among other infos, a local data time was serialized to the form
“timeOfAcquisition”:[2024,8,13,9,49,52,662000000]
Younger files will additionally contain information about the time zone where the data was created.
In my application, I need to deserialize the local date time with Jackson into a ZonedDateTime. This application needs to handle either case, with or without time zone information, in order to use the older files as well. For the older files, a certain time zone can be assumed although it is not included in the files since I know where the data was generated. The example above corresponds to
2024-08-13T09:49:52.662+01:00
since the time zone where the JSON originates was at UTC offset +01:00 on this date.
How would I create a custom deserializer for Jackson for this?
12
How would I create a custom deserializer for Jackson for this?
Here’s to get you started.
public class ModelDeserializer extends StdDeserializer<MyModel> {
private static final ZoneId assumedZoneId = ZoneId.of("Pacific/Norfolk");
public ModelDeserializer() {
super(MyModel.class);
}
@Override
public MyModel deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
ArrayNode array = (ArrayNode) node.get("timeOfAcquisition");
LocalDateTime ldt = LocalDateTime.of(array.get(0).asInt(),
array.get(1).asInt(), array.get(2).asInt(),
array.get(3).asInt(), array.get(4).asInt(),
array.get(5).asInt(), array.get(6).asInt());
MyModel model = new MyModel();
model.timeOfAcquisition = ldt.atZone(assumedZoneId);
return model;
}
}
The basic trick is to read the array of numbers from the JSON as an ArrayNode
and pass each of the 7 elements as int
to LocalDateTime.of()
. You will want to add validation that the array has length 7. And substitute the time zone where your JSON comes from. Also I am leaving to you to extend the code to handle the case where time zone is included in the JSON.
I have assumed a model class like this:
public class MyModel {
public ZonedDateTime timeOfAcquisition;
@Override
public String toString() {
return "MyModel{timeOfAcquisition=" + timeOfAcquisition + '}';
}
}
To try the whole thing out:
String json = """
{
"timeOfAcquisition":[2024,8,13,9,49,52,662000000]
}""";
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(MyModel.class, new ModelDeserializer());
mapper.registerModule(module);
MyModel obj = mapper.readValue(json, MyModel.class);
System.out.println(obj);
Output from this snippet is:
MyModel{timeOfAcquisition=2024-08-13T09:49:52.662+11:00[Pacific/Norfolk]}
ZonedDateTime
or OffsetDateTime
?
My result includes the time zone identifier (Pacific/Norfolk in my example, I know that you will choose a different one). If you want a result without the time zone (as in 2024-08-13T09:49:52.662+01:00
from your question), you should, all things being equal, prefer an OffsetDateTime
over a ZonedDateTime
. A IANA time zone, for example Europe/London or Africa/Lagos, represents all historic and known future offsets for that time zone, including but not limited to summer time (DST) transitions. Which is needed if you want to calculate, for example, 12 hours ahead of the given time in that time zone.
Abdallah Popović is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.