public void Serialize(
BsonWriter bsonWriter,
Type nominalType,
object value,
IBsonSerializationOptions options)
{
if (value == null)
{
bsonWriter.WriteNull();
}
else
{
// Nullable types are weird because they get boxed as their underlying value type
// we can best handle that by switching the nominalType to the underlying value type
// (so VerifyNominalType doesn't fail and we don't get an unnecessary discriminator)
if (nominalType.IsGenericType && nominalType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
nominalType = nominalType.GetGenericArguments()[0];
}
VerifyNominalType(nominalType);
var actualType = (value == null) ? nominalType : value.GetType();
if (actualType != _classMap.ClassType)
{
var message = string.Format("BsonClassMapSerializer.Serialize for type {0} was called with actualType {1}.",
BsonUtils.GetFriendlyTypeName(_classMap.ClassType), BsonUtils.GetFriendlyTypeName(actualType));
throw new BsonSerializationException(message);
}
var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
if (documentSerializationOptions == null)
{
var message = string.Format(
"Serializer BsonClassMapSerializer expected serialization options of type {0}, not {1}.",
BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
BsonUtils.GetFriendlyTypeName(options.GetType()));
throw new BsonSerializationException(message);
}
bsonWriter.WriteStartDocument();
BsonMemberMap idMemberMap = null;
if (documentSerializationOptions.SerializeIdFirst)
{
idMemberMap = _classMap.IdMemberMap;
if (idMemberMap != null)
{
SerializeMember(bsonWriter, value, idMemberMap);
}
}
if (actualType != nominalType || _classMap.DiscriminatorIsRequired || _classMap.HasRootClass)
{
// never write out a discriminator for an anonymous class
if (!_classMap.IsAnonymous)
{
var discriminatorConvention = _classMap.GetDiscriminatorConvention();
var discriminator = discriminatorConvention.GetDiscriminator(nominalType, actualType);
if (discriminator != null)
{
bsonWriter.WriteName(discriminatorConvention.ElementName);
BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), discriminator, null);
}
}
}
var allMemberMaps = _classMap.AllMemberMaps;
var extraElementsMemberMapIndex = _classMap.ExtraElementsMemberMapIndex;
for (var memberMapIndex = 0; memberMapIndex < allMemberMaps.Count; ++memberMapIndex)
{
var memberMap = allMemberMaps[memberMapIndex];
// note: if serializeIdFirst is false then idMemberMap will be null (so no property will be skipped)
if (memberMap != idMemberMap)
{
if (memberMapIndex != extraElementsMemberMapIndex)
{
SerializeMember(bsonWriter, value, memberMap);
}
else
{
SerializeExtraElements(bsonWriter, value, memberMap);
}
}
}
bsonWriter.WriteEndDocument();
}
}