-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Instant prior to Instant.EPOCH is not equal after serialization/deserialization #120
Labels
Milestone
Comments
From gist: import java.io.IOException;
import java.time.Instant;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
public final class JacksonInstantSerializationBug {
public static void main(String[] args) throws IOException {
final ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule());
final Instant original = Instant.ofEpochMilli(-1);
final String serialized = mapper.writeValueAsString(original);
final Instant deserialized = mapper.readValue(serialized, Instant.class);
System.out.println(String.format("Original: secs=%s, nanos=%s",
original.getEpochSecond(), original.getNano()));
System.out.println(String.format("Serialized: %s", serialized));
System.out.println(String.format("Deserialized: secs=%s, nanos=%s",
deserialized.getEpochSecond(), deserialized.getNano()));
System.out.println(deserialized.equals(original) ? "Equal!" : "Not equal!");
/*
* stdout:
* Original: secs=-1, nanos=999000000
* Serialized: -1.999000000
* Deserialized: secs=-2, nanos=1000000
* Not equal!
*
* This holds for any instant prior to Instant.EPOCH with non-zero nanos;
* i.e. Instant.ofEpochMilli(-1234) is not equal after deserialization, but Instant.ofEpochMilli(-1000) is.
*
* Reproduced with Jackson-datatype-jsr310 version 2.9.8
*/
}
} |
May be wrong but seems like this could be relatively easy to diagnose (figure out the underlying problem), even if solution was not easy. Hence tagging. |
Merged
Ok: as per comments, this is duplicate of #69, so will close in favor of it. |
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
It seems that certain
java.time.Instant
instances prior toInstant.EPOCH
are not equal after being serialized, then deserialized.As you can see from this gist, serializing then deserializing an
Instant
one millisecond prior to the epoch (1969-12-31T23:59:59.999Z
) results in anInstant
1.998 seconds earlier:1969-12-31T23:59:58.001Z
As mentioned above, this affects only Instants with a nonzero nanos field.
Instant.ofEpochMilli(-1001)
is not equal after deserialization, whileInstant.ofEpochMilli(1000)
is equal.Using OpenJDK8 1.8.0_202 and Jackson 2.9.8.
The text was updated successfully, but these errors were encountered: