|
| 1 | +/** |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
| 3 | + */ |
| 4 | +package com.seam.api.core; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.core.JsonParser; |
| 7 | +import com.fasterxml.jackson.core.JsonToken; |
| 8 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 9 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 10 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 11 | +import java.io.IOException; |
| 12 | +import java.time.Instant; |
| 13 | +import java.time.LocalDateTime; |
| 14 | +import java.time.OffsetDateTime; |
| 15 | +import java.time.ZoneOffset; |
| 16 | +import java.time.format.DateTimeFormatter; |
| 17 | +import java.time.temporal.TemporalAccessor; |
| 18 | +import java.time.temporal.TemporalQueries; |
| 19 | + |
| 20 | +/** |
| 21 | + * Custom deserializer that handles converting ISO8601 dates into {@link OffsetDateTime} objects. |
| 22 | + */ |
| 23 | +class DateTimeDeserializer extends JsonDeserializer<OffsetDateTime> { |
| 24 | + private static final SimpleModule MODULE; |
| 25 | + |
| 26 | + static { |
| 27 | + MODULE = new SimpleModule().addDeserializer(OffsetDateTime.class, new DateTimeDeserializer()); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Gets a module wrapping this deserializer as an adapter for the Jackson ObjectMapper. |
| 32 | + * |
| 33 | + * @return A {@link SimpleModule} to be plugged onto Jackson ObjectMapper. |
| 34 | + */ |
| 35 | + public static SimpleModule getModule() { |
| 36 | + return MODULE; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public OffsetDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException { |
| 41 | + JsonToken token = parser.currentToken(); |
| 42 | + if (token == JsonToken.VALUE_NUMBER_INT) { |
| 43 | + return OffsetDateTime.ofInstant(Instant.ofEpochSecond(parser.getValueAsLong()), ZoneOffset.UTC); |
| 44 | + } else { |
| 45 | + TemporalAccessor temporal = DateTimeFormatter.ISO_DATE_TIME.parseBest( |
| 46 | + parser.getValueAsString(), OffsetDateTime::from, LocalDateTime::from); |
| 47 | + |
| 48 | + if (temporal.query(TemporalQueries.offset()) == null) { |
| 49 | + return LocalDateTime.from(temporal).atOffset(ZoneOffset.UTC); |
| 50 | + } else { |
| 51 | + return OffsetDateTime.from(temporal); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments