private static <T> BsonValue writeAndReadBsonUsingGson(T value, Gson gson) throws IOException {
    TypeAdapter<T> adapter = gson.getAdapter((Class<T>) value.getClass());
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    writer.writeStartDocument();
    writer.writeName("value");
    adapter.write(Jsons.asGsonWriter(writer), value);
    writer.writeEndDocument();

    BsonValue bson = writer.getDocument().get("value");
    check(bson).notNull();
    return bson;
  }
Tests that {@code java.time.*} and {@link Date} BSON codecs are properly working with Gson type adapters.
mongo/test/org/immutables/mongo/bson4gson/JavaTimeTest.java