1

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.

  • mongodb-driver-sync 4.0.4
  • jackson-databind 2.10.1
  • jackson-datatype jsr310 2.10.1
  • mongojack 4.0.2

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?

Dylan Morley
  • 1,656
  • 12
  • 21
  • I've taken a similar approach to https://stackoverflow.com/a/27800583/1538039 for now, a small mapping that converts the POJO LocalDateTime to a Date type, then let MongoJack serializers work with that (and vice-versa for deserialization) . Please do let me know if there's a better way... – Dylan Morley Dec 01 '20 at 09:31
  • `LocalDateTime` cannot be serialized to `ISODate` by design - they are different. But `Instant` should be serialized the same way as `java.util.Date`. Looks like a flaw in mongojack. – Vladimir Petrakovich Mar 02 '21 at 14:11

0 Answers0