Custom time deserialization with Jackson

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.

New contributor

Abdallah Popović is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật