1

I want to deserialize the OffsetDateTime JSON object into ISO8601 format

I have generated the JacksonJSONProvider classes through swagger-code-gen but I am not able to figure out how to use class...

Here is the Code Of The Class


@Provider
@Produces({MediaType.APPLICATION_JSON})
public class JacksonJsonProvider extends JacksonJaxbJsonProvider {

    public JacksonJsonProvider() {

        ObjectMapper objectMapper = new ObjectMapper()
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .registerModule(new JavaTimeModule())
                .setDateFormat(new RFC3339DateFormat());

        setMapper(objectMapper);
    }
}

Actual result

"offset": {
            "totalSeconds": 19800,
            "id": "+05:30",
            "rules": {
                "transitions": [],
                "transitionRules": [],
                "fixedOffset": true
            }
        },
        "year": 2006,
        "month": "NOVEMBER",
        "monthValue": 11,
        "dayOfMonth": 8,
        "hour": 15,
        "minute": 57,
        "second": 0,
        "nano": 0,
        "dayOfWeek": "WEDNESDAY",
        "dayOfYear": 312

Expected Result

"2006-11-08T21:27:00.000+0000"
Usman Ali
  • 425
  • 1
  • 9
  • 31
  • Can you show your dependencies. – Paul Samsotha Jun 17 '19 at 15:56
  • com.fasterxml.jackson.core jackson-annotations com.fasterxml.jackson.core jackson-core com.fasterxml.jackson.core jackson-databind com.fasterxml.jackson.datatype jackson-datatype-jsr310 com.fasterxml.jackson.jaxrs jackson-jaxrs-json-provider com.fasterxml.jackson.module jackson-module-jaxb-annotations – Aayush Chachan Jun 18 '19 at 05:41
  • with version 2.9.9 – Aayush Chachan Jun 18 '19 at 05:41
  • Could you please edit your post instead of putting this information as a comment. There is an "edit" link at the bottom of your post. Also would you please post all your dependencies and not just the Jackson ones. Also please tell us which server and JAX-RS implementation you you are using, Thank you. – Paul Samsotha Jun 18 '19 at 06:10
  • Most of my dependencies are company specific so I can't mention it all over here – Aayush Chachan Jun 18 '19 at 06:22

1 Answers1

1

Your Expected result

"2006-11-08T21:27:00.000+0000"

is not in JSON format at all, so a JSON formatter will not help you. To parse a OffsetDateTime into your desired format you need to use DateTimeFormatter class. However, if you have a class that has a member of type OffsetDateTime and you want to serialize your entire class to JSON then here is the link to the question that gives you the right answer: Spring Data JPA - ZonedDateTime format for json serialization . Basically the solution will look like

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss.SSSZ", locale = "en")
    private OffsetDateTime myTime;
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • I have already tried @JSONFormat but it's not working. I am having problem in deserializing the OffsetDateTime – Aayush Chachan Jun 17 '19 at 07:20
  • "Not working" is too broad of a description. Also your entire problem description is not complete. Provide the class that you work with and what exactly "not working" means – Michael Gantman Jun 17 '19 at 08:01
  • What I meant was that I have tried using @JSONFormat annotation but it was not changing my output into this pattern = "dd-MM-yyyy HH:mm:ss.SSSZ". I am trying to use this JacksonJSONProvider class in my REST API for one of my model class. Example public class GetOrderDetailsResponse { @JsonProperty("id") private String id = null; @JsonProperty("receivedOn") private OffsetDateTime receivedOn = null; } I want receivedOn output to be in ISO8601 format but I guess this jackson provider class is not able to deserialize it. – Aayush Chachan Jun 17 '19 at 10:28
  • I have generated this JacksonJSONProvider class from swagger-codegen. – Aayush Chachan Jun 17 '19 at 10:28
  • I still would say that @JSONFormat is correct solution. And my suggestion is to figure out why it is not working for you, because it worked for many others and considered an appropriate solution. Please look up the answer to the question I linked in my answer. There you will see the required dependencies. May be you are missing those dependencies: com.fasterxml.jackson.datatype jackson-datatype-jsr310 2.6.0 – Michael Gantman Jun 17 '19 at 11:27
  • I have already included this dependency with a higher version - 2.9.9 – Aayush Chachan Jun 18 '19 at 05:35