/// <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) { // note: the DateTime portion cannot be serialized as a BsonType.DateTime because it is NOT in UTC var dateTimeOffset = (DateTimeOffset)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Array: bsonWriter.WriteStartArray(); bsonWriter.WriteInt64(dateTimeOffset.Ticks); bsonWriter.WriteInt32((int)dateTimeOffset.Offset.TotalMinutes); bsonWriter.WriteEndArray(); break; case BsonType.Document: bsonWriter.WriteStartDocument(); bsonWriter.WriteDateTime("DateTime", BsonUtils.ToMillisecondsSinceEpoch(dateTimeOffset.UtcDateTime)); bsonWriter.WriteInt64("Ticks", dateTimeOffset.Ticks); bsonWriter.WriteInt32("Offset", (int)dateTimeOffset.Offset.TotalMinutes); bsonWriter.WriteEndDocument(); break; case BsonType.String: bsonWriter.WriteString(XmlConvert.ToString(dateTimeOffset)); break; default: var message = string.Format("'{0}' is not a valid DateTimeOffset representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } }
/// <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) { var doubleValue = (double)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Double: bsonWriter.WriteDouble(doubleValue); break; case BsonType.Int32: bsonWriter.WriteInt32(representationSerializationOptions.ToInt32(doubleValue)); break; case BsonType.Int64: bsonWriter.WriteInt64(representationSerializationOptions.ToInt64(doubleValue)); break; case BsonType.String: bsonWriter.WriteString(doubleValue.ToString("R", NumberFormatInfo.InvariantInfo)); break; default: var message = string.Format("'{0}' is not a valid Double representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } }
/// <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) { var decimalValue = (Decimal)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Array: bsonWriter.WriteStartArray(); var bits = Decimal.GetBits(decimalValue); bsonWriter.WriteInt32(bits[0]); bsonWriter.WriteInt32(bits[1]); bsonWriter.WriteInt32(bits[2]); bsonWriter.WriteInt32(bits[3]); bsonWriter.WriteEndArray(); break; case BsonType.Double: bsonWriter.WriteDouble(representationSerializationOptions.ToDouble(decimalValue)); break; case BsonType.Int32: bsonWriter.WriteInt32(representationSerializationOptions.ToInt32(decimalValue)); break; case BsonType.Int64: bsonWriter.WriteInt64(representationSerializationOptions.ToInt64(decimalValue)); break; case BsonType.String: bsonWriter.WriteString(XmlConvert.ToString(decimalValue)); break; default: var message = string.Format("'{0}' is not a valid Decimal representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } }
/// <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) { var int64Value = (long)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Double: bsonWriter.WriteDouble(representationSerializationOptions.ToDouble(int64Value)); break; case BsonType.Int32: bsonWriter.WriteInt32(representationSerializationOptions.ToInt32(int64Value)); break; case BsonType.Int64: bsonWriter.WriteInt64(int64Value); break; case BsonType.String: bsonWriter.WriteString(XmlConvert.ToString(int64Value)); break; default: var message = string.Format("'{0}' is not a valid Int64 representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } }
/// <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) { var sbyteValue = (sbyte)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Binary: bsonWriter.WriteBytes(new byte[] { (byte)sbyteValue }); break; case BsonType.Int32: bsonWriter.WriteInt32(sbyteValue); break; case BsonType.Int64: bsonWriter.WriteInt64(sbyteValue); break; case BsonType.String: bsonWriter.WriteString(string.Format("{0:x2}", (byte)sbyteValue)); break; default: var message = string.Format("'{0}' is not a valid SByte representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } }
/// <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) { var charValue = (char)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Int32: bsonWriter.WriteInt32((int)charValue); break; case BsonType.String: bsonWriter.WriteString(new string(new[] { charValue })); break; default: var message = string.Format("'{0}' is not a valid Char representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } }
/// <summary> /// Serializes an object of type System.Drawing.Size 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) { var size = (System.Drawing.Size)value; bsonWriter.WriteStartDocument(); bsonWriter.WriteInt32("Width", size.Width); bsonWriter.WriteInt32("Height", size.Height); bsonWriter.WriteEndDocument(); }
/// <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 version = (Version)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Document: bsonWriter.WriteStartDocument(); bsonWriter.WriteInt32("Major", version.Major); bsonWriter.WriteInt32("Minor", version.Minor); if (version.Build != -1) { bsonWriter.WriteInt32("Build", version.Build); if (version.Revision != -1) { bsonWriter.WriteInt32("Revision", version.Revision); } } bsonWriter.WriteEndDocument(); break; case BsonType.String: bsonWriter.WriteString(version.ToString()); break; default: var message = string.Format("'{0}' is not a valid Version representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } } }
/// <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) { var timeSpan = (TimeSpan)value; // support RepresentationSerializationOptions for backward compatibility var representationSerializationOptions = options as RepresentationSerializationOptions; if (representationSerializationOptions != null) { options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation); } var timeSpanSerializationOptions = EnsureSerializationOptions<TimeSpanSerializationOptions>(options); if (timeSpanSerializationOptions.Representation == BsonType.String) { bsonWriter.WriteString(timeSpan.ToString()); // for TimeSpan use .NET's format instead of XmlConvert.ToString } else if (timeSpanSerializationOptions.Units == TimeSpanUnits.Ticks) { var ticks = timeSpan.Ticks; switch (timeSpanSerializationOptions.Representation) { case BsonType.Double: bsonWriter.WriteDouble((double)ticks); break; case BsonType.Int32: bsonWriter.WriteInt32((int)ticks); break; case BsonType.Int64: bsonWriter.WriteInt64(ticks); break; default: var message = string.Format("'{0}' is not a valid TimeSpan representation.", timeSpanSerializationOptions.Representation); throw new BsonSerializationException(message); } } else { double interval; switch (timeSpanSerializationOptions.Units) { case TimeSpanUnits.Days: interval = timeSpan.TotalDays; break; case TimeSpanUnits.Hours: interval = timeSpan.TotalHours; break; case TimeSpanUnits.Minutes: interval = timeSpan.TotalMinutes; break; case TimeSpanUnits.Seconds: interval = timeSpan.TotalSeconds; break; case TimeSpanUnits.Milliseconds: interval = timeSpan.TotalMilliseconds; break; case TimeSpanUnits.Nanoseconds: interval = timeSpan.TotalMilliseconds * 1000.0; break; default: var message = string.Format("'{0}' is not a valid TimeSpanUnits value.", timeSpanSerializationOptions.Units); throw new BsonSerializationException(message); } switch (timeSpanSerializationOptions.Representation) { case BsonType.Double: bsonWriter.WriteDouble(interval); break; case BsonType.Int32: bsonWriter.WriteInt32((int)interval); break; case BsonType.Int64: bsonWriter.WriteInt64((long)interval); break; default: var message = string.Format("'{0}' is not a valid TimeSpan representation.", timeSpanSerializationOptions.Representation); throw new BsonSerializationException(message); } } }
#pragma warning restore 618 /// <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 bitArray = (BitArray)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Binary: if ((bitArray.Length % 8) == 0) { bsonWriter.WriteBinaryData(GetBytes(bitArray), BsonBinarySubType.Binary); } else { bsonWriter.WriteStartDocument(); bsonWriter.WriteInt32("Length", bitArray.Length); bsonWriter.WriteBinaryData("Bytes", GetBytes(bitArray), BsonBinarySubType.Binary); bsonWriter.WriteEndDocument(); } break; case BsonType.String: var sb = new StringBuilder(bitArray.Length); for (int i = 0; i < bitArray.Length; i++) { sb.Append(bitArray[i] ? '1' : '0'); } bsonWriter.WriteString(sb.ToString()); break; default: var message = string.Format("'{0}' is not a valid BitArray representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } } }
/// <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) { var actualType = value.GetType(); VerifySerializeTypes(nominalType, actualType); var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case 0: var underlyingTypeCode = Type.GetTypeCode(Enum.GetUnderlyingType(actualType)); if (underlyingTypeCode == TypeCode.Int64 || underlyingTypeCode == TypeCode.UInt64) { goto case BsonType.Int64; } else { goto case BsonType.Int32; } case BsonType.Int32: bsonWriter.WriteInt32(Convert.ToInt32(value)); break; case BsonType.Int64: bsonWriter.WriteInt64(Convert.ToInt64(value)); break; case BsonType.String: bsonWriter.WriteString(value.ToString()); break; default: throw new BsonInternalException("Unexpected EnumRepresentation."); } }