@Override
 public void serialize(Symbol value, JsonGenerator gen, SerializerProvider serializerProvider)
     throws IOException {
   if (value == null) {
     serializerProvider.defaultSerializeNull(gen);
   } else if (gen instanceof BsonGenerator) {
     BsonGenerator bgen = (BsonGenerator) gen;
     bgen.writeSymbol(value);
   } else {
     gen.writeString(value.getSymbol());
   }
 }
  public static void marshal(JsonGenerator generator, TimeInstant value) throws IOException {

    if (generator instanceof BsonGenerator) {
      ((BsonGenerator) generator).writeDateTime(new Date(value.value()));
    } else {
      generator.writeNumber(value.value());
    }
  }
 public static void marshal(JsonGenerator generator, Id value) throws IOException {
   if (generator instanceof BsonGenerator) {
     ((BsonGenerator) generator)
         .writeObjectId(new ObjectId(value.time(), value.machine(), value.inc()));
   } else {
     generator.writeString(value.toString());
   }
 }
 @Override
 public void serialize(UUID value, JsonGenerator jgen, SerializerProvider provider)
     throws IOException, JsonProcessingException {
   if (!(jgen instanceof BsonGenerator)) {
     throw new JsonGenerationException("BsonUuidSerializer can only be used with BsonGenerator");
   }
   ((BsonGenerator) jgen)
       .writeBinary(null, BsonConstants.SUBTYPE_UUID, uuidToLittleEndianBytes(value), 0, 16);
 }
 @Override
 public void serialize(
     ObjectId objectId, BsonGenerator bsonGenerator, SerializerProvider serializerProvider)
     throws IOException {
   if (objectId == null) {
     serializerProvider.defaultSerializeNull(bsonGenerator);
   } else {
     bsonGenerator.writeObjectId(objectId);
   }
 }
 @Override
 public void serialize(
     Timestamp timestamp, BsonGenerator bsonGenerator, SerializerProvider serializerProvider)
     throws IOException {
   if (timestamp == null) {
     serializerProvider.defaultSerializeNull(bsonGenerator);
   } else {
     bsonGenerator.writeTimestamp(timestamp);
   }
 }
 public int writeObject(OutputBuffer buf, BSONObject object) {
   if (object instanceof JacksonDBObject) {
     Object actualObject = ((JacksonDBObject) object).getObject();
     OutputBufferOutputStream stream = new OutputBufferOutputStream(buf);
     BsonGenerator generator =
         new DBEncoderBsonGenerator(JsonGenerator.Feature.collectDefaults(), stream);
     try {
       objectMapper.writeValue(generator, actualObject);
       // The generator buffers everything so that it can write the number of bytes to the stream
       generator.close();
     } catch (JsonMappingException e) {
       throw new MongoJsonMappingException(e);
     } catch (IOException e) {
       throw new MongoException("Error writing object out", e);
     }
     return stream.getCount();
   } else {
     return defaultDBEncoder.writeObject(buf, object);
   }
 }
 @Override
 public void serialize(UUID value, BsonGenerator bgen, SerializerProvider provider)
     throws IOException {
   bgen.writeBinary(null, BsonConstants.SUBTYPE_UUID, uuidToLittleEndianBytes(value), 0, 16);
 }