public void Serialize(
            BsonWriter bsonWriter,
            object obj,
            bool serializeIdFirst,
            bool serializeDiscriminator
        )
        {
            var objType = obj.GetType();
            var classMap = BsonClassMap.LookupClassMap(objType);

            bsonWriter.WriteStartDocument();
            BsonPropertyMap idPropertyMap = null;
            if (serializeIdFirst) {
                idPropertyMap = classMap.IdPropertyMap;
                if (idPropertyMap != null) {
                    idPropertyMap.PropertySerializer.SerializeProperty(bsonWriter, obj, idPropertyMap);
                }
            }

            if (serializeDiscriminator) {
                bsonWriter.WriteString("_t", classMap.Discriminator);
            }

            foreach (var propertyMap in classMap.PropertyMaps) {
                // note: if serializeIdFirst is false then idPropertyMap will be null (so no property will be skipped)
                if (propertyMap != idPropertyMap) {
                    if (obj == null && propertyMap.IgnoreIfNull) {
                        continue; // don't serialize null value
                    }
                    if (propertyMap.HasDefaultValue && !propertyMap.SerializeDefaultValue && obj.Equals(propertyMap.DefaultValue)) {
                        continue; // don't serialize default value
                    }
                    propertyMap.PropertySerializer.SerializeProperty(bsonWriter, obj, propertyMap);
                }
            }
            bsonWriter.WriteEndDocument();
        }
 public void SerializeProperty(
     BsonWriter bsonWriter,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var value = (ulong) propertyMap.Getter(obj);
     if (propertyMap.UseCompactRepresentation) {
         bsonWriter.WriteInt64(propertyMap.ElementName, (long) value);
     } else {
         bsonWriter.WriteDocumentName(propertyMap.ElementName);
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteString("_t", typeof(ulong).FullName);
         bsonWriter.WriteInt64("v", (long) value);
         bsonWriter.WriteEndDocument();
     }
 }
 public void SerializeProperty(
     BsonWriter bsonWriter,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var value = (BsonNull) propertyMap.Getter(obj);
     if (value == null) {
         bsonWriter.WriteDocumentName(propertyMap.ElementName);
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteString("$null", "c#null");
         bsonWriter.WriteEndDocument();
     } else {
         bsonWriter.WriteNull(propertyMap.ElementName);
     }
 }
 public void SerializeProperty(
     BsonWriter bsonWriter,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var value = (float) propertyMap.Getter(obj);
     var doubleValue = value == float.MinValue ? double.MinValue : value == float.MaxValue ? double.MaxValue : value;
     if (propertyMap.UseCompactRepresentation) {
         bsonWriter.WriteDouble(propertyMap.ElementName, doubleValue);
     } else {
         bsonWriter.WriteDocumentName(propertyMap.ElementName);
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteString("_t", typeof(float).FullName);
         bsonWriter.WriteDouble("v", doubleValue);
         bsonWriter.WriteEndDocument();
     }
 }
 public void SerializeProperty(
     BsonWriter bsonWriter,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var value = (Decimal) propertyMap.Getter(obj);
     bsonWriter.WriteDocumentName(propertyMap.ElementName);
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteString("_t", typeof(Decimal).FullName);
     bsonWriter.WriteString("v", XmlConvert.ToString(value));
     bsonWriter.WriteEndDocument();
 }
 public void SerializeProperty(
     BsonWriter bsonWriter,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     // note: the DateTime portion cannot be serialized as a BsonType.DateTime because it is NOT in UTC
     var value = (DateTimeOffset) propertyMap.Getter(obj);
     if (propertyMap.UseCompactRepresentation) {
         bsonWriter.WriteArrayName(propertyMap.ElementName);
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteInt64("0", value.DateTime.Ticks);
         bsonWriter.WriteInt64("1", value.Offset.Ticks);
         bsonWriter.WriteEndDocument();
     } else {
         bsonWriter.WriteDocumentName(propertyMap.ElementName);
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteString("_t", typeof(DateTimeOffset).FullName);
         bsonWriter.WriteString("dt", value.DateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFF")); // omit trailing zeros
         bsonWriter.WriteString("o", Regex.Replace(value.Offset.ToString(), ":00$", "")); // omit trailing zeros
         bsonWriter.WriteEndDocument();
     }
 }
 public void SerializeProperty(
     BsonWriter bsonWriter,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var value = (CultureInfo) propertyMap.Getter(obj);
     bsonWriter.WriteDocumentName(propertyMap.ElementName);
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteString("_t", typeof(CultureInfo).FullName);
     bsonWriter.WriteString("v", value.ToString());
     bsonWriter.WriteEndDocument();
 }
 public void SerializeProperty(
     BsonWriter bsonWriter,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var value = (char) propertyMap.Getter(obj);
     if (propertyMap.UseCompactRepresentation) {
         bsonWriter.WriteString(propertyMap.ElementName, new string(new char[] { value }));
     } else {
         bsonWriter.WriteDocumentName(propertyMap.ElementName);
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteString("_t", typeof(char).FullName);
         bsonWriter.WriteString("v", new string(new char[] { value }));
         bsonWriter.WriteEndDocument();
     }
 }