Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson ZonedDateTime support #38

Closed
gkocak-scottlogic opened this issue Mar 21, 2023 · 5 comments
Closed

Jackson ZonedDateTime support #38

gkocak-scottlogic opened this issue Mar 21, 2023 · 5 comments

Comments

@gkocak-scottlogic
Copy link
Collaborator

gkocak-scottlogic commented Mar 21, 2023

Some tests are failing due to:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.ZonedDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling

Probably can be resolved by adding:

<dependency>
	<groupId>com.fasterxml.jackson.datatype</groupId>
	<artifactId>jackson-datatype-jsr310</artifactId>
	<version>2.6.0</version>
</dependency>

to pom.

@gkocak-scottlogic
Copy link
Collaborator Author

Apparently, the "old" JSR310Module is deprecated. [1]

A way to deal with this can be:

ObjectMapper mapper = JsonMapper.builder()
        .findAndAddModules()
        .build();

instead of

ObjectMapper mapper = new ObjectMapper();

@gkocak-scottlogic
Copy link
Collaborator Author

Even with this potential change, java time types are flexible and create some additional errors.

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.ZonedDateTime` from String "2013-07-21": Failed to deserialize java.time.ZonedDateTime: (java.time.format.DateTimeParseException) Text '2013-07-21' could not be parsed at index 10

i.e. 2013-07-21 is not parsed as 2013-07-21T00:00:00.000Z directly.

ZonedDateTime is very strict where may or may not include the time info.

@gkocak-scottlogic
Copy link
Collaborator Author

gkocak-scottlogic commented Mar 23, 2023

Defaulting some fields can be done by (from [1]

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
        .appendValue(ChronoField.YEAR)
        .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
        .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
        .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
        .toFormatter()
        .withZone(ZoneOffset.UTC);

Giving datetimeformatter to the objectmapper by (from [2]

JavaTimeModule javaTimeModule = new JavaTimeModule(); 
LocalDateTimeDeserializer dateTimeDeserializer = new LocalDateTimeDeserializer(formatter);
LocalDateTimeSerializer dateTimeSerializer = new LocalDateTimeSerializer(formatter);
javaTimeModule.addDeserializer(LocalDateTime.class, dateTimeDeserializer);
javaTimeModule.addSerializer(LocalDateTime.class, dateTimeSerializer);

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(javaTimeModule);

I really don't like cluterring the code with these to be honest but I guess we might not have much option. And these examples are for for LocalDateTime and might not work on ZonedDateTime.

@gkocak-scottlogic
Copy link
Collaborator Author

Probably we will need a custom deser [1].

@gkocak-scottlogic
Copy link
Collaborator Author

Fixed with #45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant