3

I am parsing this JSON object:

@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class HotelDataForCreate  {


    private OffsetDateTime fromDate;
    private OffsetDateTime toDate;

}

where I put these values:

OffsetDateTime start = OffsetDateTime.now();
OffsetDateTime end = start.plusMinutes(1);

but I am getting this error when parsing:

"message" : "JSON parse error: Unexpected token (START_OBJECT), expected one of [VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT] for java.time.OffsetDateTime value; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected one of [VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT] for java.time.OffsetDateTime value\n at [Source: java.io.PushbackInputStream@ac6960; line: 1, column: 1001] (through reference chain: com.pxs.weboffice.leads.crud.model.LeadDataForCreate["fromDate"])",
      "stackTrace" : "org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected token (START_OBJECT), expected one of [VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT] for java.time.OffsetDateTime value; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected one of [VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT] for java.time.OffsetDateTime value\n at [Source: java.io.PushbackInputStream@ac6960; line: 1, column: 1001] (through reference chain: com.pxs.weboffice.leads.crud.model.LeadDataForCreate[\"fromDate\"])\r\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:238)\r\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:223)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:201)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:150)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128)\r\n\tat org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:707)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)\r\n\tat org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71)\r\n\tat javax.servlet.http.HttpServlet

Here the Json :

{
    ...
    "fromDate":{"offset":{"totalSeconds":7200,"id":"+02:00","rules":{"fixedOffset":true,"transitionRules":[],"transitions":[]}},"month":"JULY","year":2019,"hour":16,"minute":4,"nano":563000000,"second":38,"dayOfMonth":25,"dayOfWeek":"THURSDAY","dayOfYear":206,"monthValue":7},"toDate":{"offset":{"totalSeconds":7200,"id":"+02:00","rules":{"fixedOffset":true,"transitionRules":[],"transitions":[]}},"month":"JULY","year":2019,"hour":16,"minute":5,"nano":563000000,"second":38,"dayOfMonth":25,"dayOfWeek":"THURSDAY","dayOfYear":206,"monthValue":7}}
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80
  • 1
    can you post example of your JSON? – Stefan Golubović Jul 25 '19 at 14:00
  • Apparently, there is no easy way to deserialize this format. I think you already serialized your data by using jackson. If you have a chance to alter your persisted serialized data or resave them in a different format, you may consider using `JavaTimeModule` for your `ObjectMapper` which is located inside `jackson-datatype-jsr310` package. In this way, you can both serialize and deserialize your `OffsetDateTime` fields without hassle. If you are interested I can provide further samples according to your environment. – Yavuz Tas Jul 25 '19 at 15:41
  • 3
    Take a look at [Jackson Serialize Instant to Nanosecond Issue](https://stackoverflow.com/questions/56345200/jackson-serialize-instant-to-nanosecond-issue), [Problem with deserialization of LocalDateTime in Junit test](https://stackoverflow.com/questions/55107588/problem-with-deserialization-of-localdatetime-in-junit-test/55108967#55108967), [Jackson deserialize elasticsearch long as LocalDateTime with Java 8](https://stackoverflow.com/questions/57098784/jackson-deserialize-elasticsearch-long-as-localdatetime-with-java-8) – Michał Ziober Jul 25 '19 at 15:58

0 Answers0