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

Can't deserialize OffsetDateTime.MIN: Invalid value for EpochDay #308

Closed
sszuev opened this issue Apr 25, 2024 · 9 comments
Closed

Can't deserialize OffsetDateTime.MIN: Invalid value for EpochDay #308

sszuev opened this issue Apr 25, 2024 · 9 comments
Labels
good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project pr-welcome Issue for which progress most likely if someone submits a Pull Request
Milestone

Comments

@sszuev
Copy link

sszuev commented Apr 25, 2024

jdk-17.0.6,
code:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.OffsetDateTime;
public class Main {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper()
                .registerModule(new JavaTimeModule());
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(OffsetDateTime.MIN);
        System.out.println(json);
        OffsetDateTime value = mapper.readValue(json, OffsetDateTime.class);
        System.out.println(value);
    }
}

pom:

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jackson.version>2.17.0</jackson.version>
</properties>
<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-parameter-names</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>${jackson.version}</version>
    </dependency>
</dependencies>

output:

-31557014135661600.000000000
Exception in thread "main" java.time.DateTimeException: Invalid value for EpochDay (valid values -365243219162 - 365241780471): -365243219163
	at java.base/java.time.temporal.ValueRange.checkValidValue(ValueRange.java:319)
	at java.base/java.time.temporal.ChronoField.checkValidValue(ChronoField.java:718)
	at java.base/java.time.LocalDate.ofEpochDay(LocalDate.java:343)
	at java.base/java.time.LocalDateTime.ofEpochSecond(LocalDateTime.java:424)
	at java.base/java.time.OffsetDateTime.ofInstant(OffsetDateTime.java:330)
	at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.lambda$static$3(InstantDeserializer.java:84)
	at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer._fromDecimal(InstantDeserializer.java:328)
	at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:214)
	at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:52)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4730)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3677)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3645)
@softboy99
Copy link

same problem

@cowtowncoder cowtowncoder added pr-welcome Issue for which progress most likely if someone submits a Pull Request good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project labels Nov 1, 2024
@cowtowncoder
Copy link
Member

cowtowncoder commented Nov 1, 2024

This seems like something that someone with time and interest could pick up -- PRs welcome!

/cc @JooHyukKim in case you were looking for other things to work on :)

@JooHyukKim
Copy link
Member

I wasn't, but always willing 👌🏼
I put it on my playlist

@cowtowncoder
Copy link
Member

@JooHyukKim yeah don't feel like you should work on this; seems like you have many other things in-flight (and waiting for my reviews, some of them :-/ )

@winfriedgerlach
Copy link

possible duplicate of #124 (see comments there, further down)

@cowtowncoder cowtowncoder changed the title Can't deserialize OffsetDateTime.MIN: Invalid value for EpochDay Can't deserialize OffsetDateTime.MIN: Invalid value for EpochDay Nov 8, 2024
@cowtowncoder
Copy link
Member

@winfriedgerlach Related/overlapping, but since reported for different types -- and likely need separate (if possibly similar) fix, so will keep separate. And now we have linkage via refercens.

ps. Thank you for going over issues to find closed/obsolete/overlapping issues. Can help clean up our issue trackers.

@FabianBesner2020
Copy link

Hello everyone,

We looked into this problem and found that it is correlated with the timezone. The timezone of OffsetDateTime.MIN is +18 ZoneOffset. When trying to deserialize the value without providing this timezone, Jackson uses UTC, which cannot be represented in the java standard library.

To solve this issue, you always need to provide the timezone when deserializing. An alternative is to configure the serializer to output timestamps as strings in ISO-8601 format instead of epoch seconds.

ObjectMapper mapper = new ObjectMapper()
        .registerModule(new JavaTimeModule())
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);

@JooHyukKim
Copy link
Member

Awesome, thank you for digging into this @FabianBesner2020!

I honestly failed figuring out possible solution, but your guidance would definitely be helpful for me or anyone trying to fix this issue.

@cowtowncoder
Copy link
Member

Fixed via #325 by @JooHyukKim -- will be in 2.18.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project pr-welcome Issue for which progress most likely if someone submits a Pull Request
Projects
None yet
Development

No branches or pull requests

6 participants