예제 #1
0
 internal void WriteTo(
     BsonWriter bsonWriter
     )
 {
     bsonWriter.WriteName(name);
     value.WriteTo(bsonWriter);
 }
 public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
 {
     if (value == null)
     {
         bsonWriter.WriteNull();
     }
     else
     {
         if (nominalType.IsGenericType && nominalType.GetGenericTypeDefinition() == typeof(Nullable <>))
         {
             nominalType = nominalType.GetGenericArguments()[0];
         }
         this.VerifyNominalType(nominalType);
         // Added
         Type type = value == null ? nominalType : ProxyManager.GetUnderlyingType(value);
         // Added
         BsonClassMap bsonClassMap = BsonClassMap.LookupClassMap(type);
         bsonWriter.WriteStartDocument();
         DocumentSerializationOptions serializationOptions = options == null ? DocumentSerializationOptions.Defaults : (DocumentSerializationOptions)options;
         BsonMemberMap memberMap = (BsonMemberMap)null;
         if (serializationOptions.SerializeIdFirst)
         {
             memberMap = bsonClassMap.IdMemberMap;
             if (memberMap != null)
             {
                 this.SerializeMember(bsonWriter, value, memberMap);
             }
         }
         if ((type != nominalType || bsonClassMap.DiscriminatorIsRequired || bsonClassMap.HasRootClass) && !bsonClassMap.IsAnonymous)
         {
             IDiscriminatorConvention discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType);
             BsonValue discriminator = discriminatorConvention.GetDiscriminator(nominalType, type);
             if (discriminator != (BsonValue)null)
             {
                 bsonWriter.WriteName(discriminatorConvention.ElementName);
                 discriminator.WriteTo(bsonWriter);
             }
         }
         foreach (BsonMemberMap bsonMemberMap in bsonClassMap.MemberMaps)
         {
             if (bsonMemberMap != memberMap)
             {
                 if (bsonMemberMap == bsonClassMap.ExtraElementsMemberMap)
                 {
                     this.SerializeExtraElements(bsonWriter, value, bsonMemberMap);
                 }
                 else
                 {
                     this.SerializeMember(bsonWriter, value, bsonMemberMap);
                 }
             }
         }
         bsonWriter.WriteEndDocument();
     }
 }
예제 #3
0
 void IBsonSerializable.Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
 {
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteString("$ref", collectionName);
     bsonWriter.WriteName("$id");
     id.WriteTo(bsonWriter);
     if (databaseName != null)
     {
         bsonWriter.WriteString("$db", databaseName);
     }
     bsonWriter.WriteEndDocument();
 }