title: MongoDB BSON codec not being used while encoding object
body: <p>I'm attempting to store an object in a MongoDB database (using MongoDB 3.0.2) and am getting a <code>CodecConfigurationException</code> when attempting to encode the object with error message </p>
  
  <pre><code>Can't find a codec for class java.time.LocalDate. 
  </code></pre>
  
  <p>I have written and included a codec for the <code>LocalDate</code> objects. Details follow.</p>
  
  <p>The object, <code>DutyBlock</code>, that I'm attempting to store has these member variables:</p>
  
  <pre><code>public class DutyBlock {
      private LocalDate startDate;
      private LocalDate endDate; //Inclusive
      private int blockLength;
      private double pointValue;
      private ArrayList&lt;Ra&gt; assigned;
  }
  </code></pre>
  
  <p>I wrote the following codec to encode the <code>DutyBlock</code> objects within the database:</p>
  
  <pre><code>public class DutyBlockCodec implements Codec&lt;DutyBlock&gt; {
  
      @Override
      public void encode(BsonWriter writer, DutyBlock t, EncoderContext ec) {
          Document document = new Document();
          document.append("startDate", t.getStartDate());
          document.append("endDate", t.getEndDate());
          document.append("blockLength", t.getBlockLength());
          document.append("pointValue", t.getPointValue());
          document.append("assigned", t.getRasOnDuty());
  
          writer.writeString(document.toJson());  //Line 27 in the error message.
      }
  
      @Override
      public Class&lt;DutyBlock&gt; getEncoderClass() {
          return DutyBlock.class;
      }
  
      @Override
      public DutyBlock decode(BsonReader reader, DecoderContext dc) {
          String json = reader.readString();
          return new DutyBlock(Document.parse(json));
      }
  
  }
  </code></pre>
  
  <p>Since MongoDB currently does not support the <code>java.time.LocalDate class</code>, I've written the following codec to encode the <code>LocalDate</code> objects within the database:</p>
  
  <pre><code>public class LocalDateCodec implements Codec&lt;LocalDate&gt; {
  
      @Override
      public void encode(BsonWriter writer, LocalDate t, EncoderContext ec) {
          writer.writeString(t.toString());
      }
  
      @Override
      public Class&lt;LocalDate&gt; getEncoderClass() {
          return LocalDate.class;
      }
  
      @Override
      public LocalDate decode(BsonReader reader, DecoderContext dc) {
          String date = reader.readString();
          return LocalDate.parse(date);
      }
  }
  </code></pre>
  
  <p>I've added the two <code>Codec</code>'s (along with one for the <code>Ra</code> type) to the <code>CodecRegistry</code> at the MongoClient level while instantiating the MongoClient.</p>
  
  <pre><code>public class DutyScheduleDB {
      private MongoClient mongoClient;
      private MongoDatabase db;
  
      public DutyScheduleDB() {
          CodecRegistry codecRegistry = 
                  CodecRegistries.fromRegistries(
                          CodecRegistries.fromCodecs(new LocalDateCodec(), new DutyBlockCodec(), new RaCodec()),
                          MongoClient.getDefaultCodecRegistry());
          MongoClientOptions options = MongoClientOptions.builder()
                  .codecRegistry(codecRegistry).build();
          mongoClient = new MongoClient(new ServerAddress(), options);
          db = mongoClient.getDatabase("DutySchedulerDB");
      }
      . (More code not shown)
      .
      .
  }
  </code></pre>
  
  <p>I attempt to store an <code>ArrayList</code> of <code>DutyBlock</code> objects as part of a <code>org.bson.Document</code> within the MongoDB database.</p>
  
  <pre><code>public void storeScheduledCalendar(String id,
          String calendarName,
          ArrayList&lt;DutyBlock&gt; cal) {
      //Access collection of scheduled calendars.
      MongoCollection collection = db.getCollection("ScheduledCalendars");
      //Query parameter is uuid + calendarName.
      Document doc = new Document("name", id + calendarName);
      doc.append("dutyBlocks", cal);
      //Insert doc to collection.
      collection.insertOne(doc); //Line 59 in the error message.
  }
  </code></pre>
  
  <p>However, I'm running into this error message:</p>
  
  <pre><code>Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.time.LocalDate.
  at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
  at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
  at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
  at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:174)
  at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:189)
  at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:131)
  at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
  at org.bson.Document.toJson(Document.java:294)
  at org.bson.Document.toJson(Document.java:268)
  at org.bson.Document.toJson(Document.java:255)
  at SchedulingHeuristic.DutyBlockCodec.encode(DutyBlockCodec.java:27)
  at SchedulingHeuristic.DutyBlockCodec.encode(DutyBlockCodec.java:16)
  at org.bson.codecs.EncoderContext.encodeWithChildContext(EncoderContext.java:91)
  at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:175)
  at org.bson.codecs.DocumentCodec.writeIterable(DocumentCodec.java:197)
  at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:170)
  at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:189)
  at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:131)
  at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
  at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
  at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
  at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:99)
  at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:43)
  at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBody(BaseWriteCommandMessage.java:112)
  at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBody(BaseWriteCommandMessage.java:35)
  at com.mongodb.connection.RequestMessage.encode(RequestMessage.java:132)
  at com.mongodb.connection.BaseWriteCommandMessage.encode(BaseWriteCommandMessage.java:89)
  at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:170)
  at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:73)
  at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:66)
  at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:37)
  at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:155)
  at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:219)
  at com.mongodb.connection.DefaultServerConnection.insertCommand(DefaultServerConnection.java:108)
  at com.mongodb.operation.MixedBulkWriteOperation$Run$2.executeWriteCommandProtocol(MixedBulkWriteOperation.java:416)
  at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:604)
  at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:363)
  at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:148)
  at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:141)
  at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:186)
  at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:177)
  at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:141)
  at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:72)
  at com.mongodb.Mongo.execute(Mongo.java:747)
  at com.mongodb.Mongo$2.execute(Mongo.java:730)
  at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:482)
  at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:277)
  at DutyScheduleDB.storeScheduledCalendar(DutyScheduleDB.java:59)
  at DutyScheduleDB.main(DutyScheduleDB.java:106)
  </code></pre>
  
  <p>It seems that my codec for <code>LocalDate</code> isn't being used when attempting to encode <code>DutyBlock</code> objects, though I've verified that the collection that I am attempting to store the <code>org.bson.Document</code> in does indeed contain the <code>LocalDateCodec</code> via a </p>
  
  <pre><code>System.out.println(collection.getCodecRegistry().get(LocalDate.class));
  </code></pre>
  
  <p>Can anyone can provide some insight on why this is happening?</p>