Dylan Morley
Dylan Morley

Reputation: 1726

MongoJack with java.time

Using MongoJack with the libraries at the following versions, I want to store a POJO into Mongo (Azure Mongo API for Cosmos, 3.6) and have a field that uses java.time serialized into a Mongo Date ISODate() format.

If I use java.util.Date on the POJO, all works as expected - the data is stored in the following format in Mongo

enter image description here

However, if I use java.time.LocalDateTime then the data is stored as a string in Mongo. I want this stored as ISODate type in Mongo, and I don't want to use java.util.Date on my POJOs. Likewise, if I use Instant as the field type - the data is stored as String - it correctly serializes to/from the Instant field on the POJO, but data is not stored as the expected type in Mongo.

Am I missing something obvious here? Looking into Mongo Jack, the deserializers it does define are extending StdDeserializer from jackson.databind, which uses these parseDate methods to convert to java.util.Date - the only java.time deserializer it does define is InstantDeserializer but as mentioned that's not being stored in the format I want.

I've called ObjectMapperConfigurer.configureObjectMapper as per documentation, which adds the Javatime module, sets features and serializers etc, https://github.com/mongojack/mongojack/blob/9e2339d02624b1357ecab6e27c35d5db96bd7d60/src/main/java/org/mongojack/internal/MongoJackModule.java#L46

Should this work out of the box, or do I need to write some serializers to support this?

Upvotes: 1

Views: 256

Answers (0)

Related Questions