Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize MongoDB date fields to Java POJO Using Jackson

Using Spring Boot 1.4.4.RELEASE, have saved a RequestBody to MongoDB as below:

{
    "startTime" : NumberLong("1483542955570"),
    "startDate" : ISODate("2017-01-04T15:15:55.570Z"),
    "endTime" : NumberLong("1483542955570"),
    "endDate" : ISODate("2017-01-04T15:15:55.570Z")
}

While mapping this back to a Java POJO, I am trying the below code.

public <T> T getPOJOFromMongoDocument(Document resourceDocument, Class<T> clazz) {
        String serialize = JSON.serialize(resourceDocument);
        return objectMapper.readValue(serialize,
                                      clazz);
}

serialize has the date fields returned as following

"startDate" : { "$date" : "2017-01-04T15:15:55.570Z"}

Due to $date, Jackson ObjectMapper returns the below exception during parsing:

java.lang.RuntimeException: Error parsing mongoDoc to Pojo : errorMessage : {Can not deserialize instance of java.util.Date out of START_OBJECT token at [Source: {
"startTime": 1483542955570,
"startDate": {
    "$date": "2017-01-04T15:15:55.570Z"
},
"endTime": 1483542955570,
"endDate": {
    "$date": "2017-01-04T15:15:55.570Z"
}}; line: 1, column: 381] (through reference chain: com.gofynd.engine.mongo.models.RuleWithDataVO["validity"]->com.gofynd.engine.mongo.models.ValidityVO["startDate"])}

Is there way to solve this without using an ODM?

like image 404
mukulSharma Avatar asked Mar 17 '17 11:03

mukulSharma


People also ask

How does Jackson deserialize dates from JSON?

This is how i'm deserializing the date: SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); getObjectMapper(). getDeserializationConfig(). setDateFormat(dateFormat);

Does Jackson use Java serialization?

This short tutorial shows how the Jackson library can be used to serialize Java object to XML and deserialize them back to objects.

How does Jackson deserialization work?

Jackson uses default (no argument) constructor to create object and then sets value using setters. so you only need @NoArgsConstructor and @Setter. Save this answer.

Which constructor does Jackson use?

Jackson Deserialization Using Lombok Builders This class is also immutable and it has a private constructor. Hence, we can create instances only through its builder. This is enough to use this class for deserialization with Jackson. Also notice that Lombok uses build as the default name of the build method.


1 Answers

When deserializing to Date Jackson expects a String like "2017-01-04T15:15:55.570Z". Instead, it sees the start of another object (the { char) inside the JSON hence the exception.

Consider specifying your Pojo class and another MongoDate class similar to this:

class MongoDate {
    @JsonProperty("$date")
    Date date;
}

class Pojo {
    long startTime;
    long endTime;
    MongoDate startDate;
    MongoDate endDate;
}

Alternatively if you can't / don't want to add a MongoDate class you can introduce a custom deserializer for Date fields. In that case Pojo:

class Pojo {
    long startTime;
    long endTime;
    @JsonDeserialize(using = MongoDateConverter.class)
    Date startDate;
    @JsonDeserialize(using = MongoDateConverter.class)
    Date endDate;
}

And the deserializer would look like this:

class MongoDateConverter extends JsonDeserializer<Date> {
    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonNode node = jp.readValueAsTree();
        try {
            return formatter.parse(node.get("$date").asText());
        } catch (ParseException e) {
            return null;
        }
    }
}
like image 166
Manos Nikolaidis Avatar answered Oct 19 '22 10:10

Manos Nikolaidis