Closed CapekStanislav closed 3 years ago
Which Jackson version? If not 2.11.3, I would suggest trying with that.
I have used version 2.10.2. I switched to version 2.11.3 but I'm getting the same exception.
Note to new contributors: I'd accept just failing test case (put under datetime/src/test/.../failing/
) as successful contribution (i.e. test-only PR) -- bonus points of course if there is something to fix.
Test case can use sample code from above, just replacing file read/write with code that writes value as JSON String, reads that String.
Problem solved! I wrote a test where I realized that I wasn't deserializing an instance of LocalDateTime but an instance of Registration class.
Thank you for your reply.
(JUnit5)
public class DeserializationLocalDateTimeTest {
@Test
void serializationAndDeserializationLocalDateTimeInstance() throws IOException {
final ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule());
final Path path = Paths.get("localDateTime.json");
final LocalDateTime now = LocalDateTime.now();
mapper.writeValue(Files.newBufferedWriter(path), now);
final LocalDateTime localDateTime = mapper.readValue(Files.newBufferedReader(path), LocalDateTime.class);
System.out.println("localDateTime = " + localDateTime);
// no exception thrown
}
@Test
void serializationAndDeserializationWrappedLocalDateTimeInstance() throws IOException {
final ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule());
final Path path = Paths.get("wrapperClassLocalDateTime.json");
final Registration registration = new Registration(LocalDateTime.now());
mapper.writeValue(Files.newBufferedWriter(path), registration);
final Registration desRegistration = mapper.readValue(Files.newBufferedReader(path), Registration.class);
System.out.println("desRegistration = " + desRegistration);
// no exception thrown
}
}
@CapekStanislav ah! Glad you figured it out.
A Simple class for testing:
Construct and calling the serialization and deserialization process:
The produced JSON:
When I try to read value from the file exception is thrown: Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source: (BufferedReader); line: 1, column: 1] at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1442) at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1216) at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1148) at com.fasterxml.jackson.datatype.jsr310.deser.JSR310DeserializerBase._handleUnexpectedToken(JSR310DeserializerBase.java:105) at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:155) at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:38) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3218) at cz.stanislavcapek.testing.jackson.Jackson.serAndDeserLocalDateTime(Jackson.java:49) at cz.stanislavcapek.testing.jackson.Jackson.main(Jackson.java:34)
I couldn't find an answer anywhere.