Search code examples
jsonjava-8jackson-modulesjackson2

Jackson mapper expands LocalDate


We have a DTO defined with a LocalDate:

@JsonProperty("dob")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate dob;

We have code with an ObjectMapper defined as follows:

private static final ObjectMapper makeMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new ParameterNamesModule());
    mapper.registerModule(new Jdk8Module());
    mapper.registerModule(new JavaTimeModule());
    return mapper;
}

We have all the jackson-datababindm core, jsr310 in our pom.xml file:

    <!-- Jackson JSON Mapping -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.8.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-parameter-names</artifactId>
        <version>2.8.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jdk8</artifactId>
        <version>2.8.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.8.8</version>
    </dependency>

So, when I convert my object to a JSON string:

    String json = makeMapper().writeValueAsString(myobject);

The dob writes to the JSON string as:

"dob":{ "year": 1964, "month": "FEBRUARY", "chronology": { "calendarType": "iso8601", "id": "ISO" }, "monthValue": 2, "dayOfMonth": 13, "dayOfWeek": "THURSDAY", "era": "CE", "dayOfYear": 44, "leapYear": true}

instead of: "dob":"1964-02-13" which is correct.

So, I am not sure how I made this happen?
I'd like to make sure the date gets written out correct, so I can re-parse back to a LocalDate. Or, is there a way I can take the existing JSON (expanded) and parse that back to a LocalDate?

Thanks!


Solution

  • Turns out this was a ClassLoader issue. In the smallest case, the code works and the LocalDate is turned into JSON accordingly. When you emebed one project into another into another, then what happens is that the ObjectMapper was defined elsewhere without the JavaTimeModule.

    I took the code:

    private static final ObjectMapper makeMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new ParameterNamesModule());
        mapper.registerModule(new Jdk8Module());
        mapper.registerModule(new JavaTimeModule());
        return mapper;
    }
    

    and brought it right into my class where I needed it, and that worked. Reaching out to a utility from a different project didn't work for me.

    Hope this helps someone else.