I am trying to use the MongoDB Codec functionality to read and write a Java ZonedDateTime
object to Mongo in a custom format.
Inserting documents works just fine, but I'm struggling to understand how to get Mongo to return a ZonedDateTime
.
I've written the following test case to try and demonstrate:
public class ZonedDateTimeTest {
@Test
public void serializeAndDeserializeZonedDateTime() throws Exception {
CodecRegistry codecRegistry = fromRegistries(
CodecRegistries.fromCodecs(new ZonedDateTimeCodec()),
MongoClient.getDefaultCodecRegistry()
);
MongoClient mongoClient = new MongoClient(
"localhost:27017",
MongoClientOptions.builder()
.codecRegistry(codecRegistry)
.build()
);
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Europe/London"));
MongoCollection<Document> collection = mongoClient
.getDatabase("test")
.getCollection("test");
// Insert a document
collection.insertOne(new Document("_id", 1).append("zonedDateTime", zonedDateTime));
// Find the document just inserted
Document document = collection
.find(new Document("_id", 1))
.first();
// How to "get" a ZonedDateTime?
document.get("zonedDateTime");
}
private static class ZonedDateTimeCodec implements Codec<ZonedDateTime> {
@Override
public Class<ZonedDateTime> getEncoderClass() {
return ZonedDateTime.class;
}
@Override
public void encode(BsonWriter writer, ZonedDateTime value,
EncoderContext encoderContext) {
writer.writeStartDocument();
writer.writeString("dateTime", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value));
writer.writeString("offset", value.getOffset().toString());
writer.writeString("zone", value.getZone().toString());
writer.writeEndDocument();
}
@Override
public ZonedDateTime decode(BsonReader reader, DecoderContext decoderContext) {
reader.readStartDocument();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
LocalDateTime.from(DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(reader.readString("dateTime"))),
ZoneOffset.of(reader.readString("offset")),
ZoneId.of(reader.readString("zone"))
);
reader.readEndDocument();
return zonedDateTime;
}
}
}
I've made a test here, and checked that document.get("zonedDateTime")
returns a org.bson.Document
instance. So, I've just passed this object to the codec:
import org.bson.BsonReader;
import org.bson.Document;
import org.bson.json.JsonReader;
import org.bson.codecs.DecoderContext;
Object object = document.get("zonedDateTime");
ZonedDateTimeCodec codec = (ZonedDateTimeCodec) codecRegistry.get(ZonedDateTime.class);
BsonReader reader = new JsonReader(((Document) object).toJson());
ZonedDateTime zdt = codec.decode(reader, DecoderContext.builder().build());
System.out.println(zdt);
The ZonedDateTime
variable (zdt
) will be correctly created (in my test, I've got 2017-07-13T18:07:13.603+01:00[Europe/London]
) (the current date/time in London timezone).