/// <summary> /// Serializes an object to a BsonWriter. /// </summary> /// <param name="bsonWriter">The BsonWriter.</param> /// <param name="nominalType">The nominal type.</param> /// <param name="value">The object.</param> /// <param name="options">The serialization options.</param> public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { if (value == null) { bsonWriter.WriteNull(); } else { var lazyBsonDocument = (LazyBsonDocument)value; var slice = lazyBsonDocument.Slice; if (slice == null) { BsonDocumentSerializer.Instance.Serialize(bsonWriter, typeof(BsonDocument), lazyBsonDocument, options); } else { using (var clonedSlice = slice.GetSlice(0, slice.Length)) { bsonWriter.WriteRawBsonDocument(clonedSlice); } } } }