Skip to content

Commit

Permalink
fix LocalDateTime json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioT90 committed Feb 5, 2025
1 parent 998c60c commit 5917999
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class OffsetDateTimeToLocalDateTimeDeserializer extends JsonDeserializer<
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {

String dateString = p.getValueAsString();
if(dateString.contains("+")){
if(dateString.contains("+") || dateString.endsWith("Z")){
return OffsetDateTime.parse(dateString).toLocalDateTime();
} else {
return LocalDateTime.parse(dateString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

class OffsetDateTimeToLocalDateTimeDeserializerTest {

Expand All @@ -28,6 +29,21 @@ void givenOffsetDateTimeWhenThenOk() throws IOException {
Assertions.assertEquals(offsetDateTime.toLocalDateTime(), result);
}

@Test
void givenUTCOffsetDateTimeWhenThenOk() throws IOException {
// Given
OffsetDateTime offsetDateTime = OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.UTC);
JsonParser parser = Mockito.mock(JsonParser.class);
Mockito.when(parser.getValueAsString())
.thenReturn(offsetDateTime.toString());

// When
LocalDateTime result = deserializer.deserialize(parser, null);

// Then
Assertions.assertEquals(offsetDateTime.toLocalDateTime(), result);
}

@Test
void givenLocalDateTimeWhenThenOk() throws IOException {
// Given
Expand Down

0 comments on commit 5917999

Please sign in to comment.