-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f1f607
commit 0e1e7c9
Showing
16 changed files
with
287 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/squidex/api/core/DateTimeDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.squidex.api.core; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.time.temporal.TemporalQueries; | ||
|
||
/** | ||
* Custom deserializer that handles converting ISO8601 dates into {@link OffsetDateTime} objects. | ||
*/ | ||
class DateTimeDeserializer extends JsonDeserializer<OffsetDateTime> { | ||
private static final SimpleModule MODULE; | ||
|
||
static { | ||
MODULE = new SimpleModule().addDeserializer(OffsetDateTime.class, new DateTimeDeserializer()); | ||
} | ||
|
||
/** | ||
* Gets a module wrapping this deserializer as an adapter for the Jackson ObjectMapper. | ||
* | ||
* @return A {@link SimpleModule} to be plugged onto Jackson ObjectMapper. | ||
*/ | ||
public static SimpleModule getModule() { | ||
return MODULE; | ||
} | ||
|
||
@Override | ||
public OffsetDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException { | ||
JsonToken token = parser.currentToken(); | ||
if (token == JsonToken.VALUE_NUMBER_INT) { | ||
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(parser.getValueAsLong()), ZoneOffset.UTC); | ||
} else { | ||
TemporalAccessor temporal = DateTimeFormatter.ISO_DATE_TIME.parseBest( | ||
parser.getValueAsString(), OffsetDateTime::from, LocalDateTime::from); | ||
|
||
if (temporal.query(TemporalQueries.offset()) == null) { | ||
return LocalDateTime.from(temporal).atOffset(ZoneOffset.UTC); | ||
} else { | ||
return OffsetDateTime.from(temporal); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.