/// <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 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 { if (nominalType == typeof(object)) { var actualType = value.GetType(); bsonWriter.WriteStartDocument(); bsonWriter.WriteString("_t", TypeNameDiscriminator.GetDiscriminator(actualType)); bsonWriter.WriteName("_v"); Serialize(bsonWriter, actualType, value, options); bsonWriter.WriteEndDocument(); return; } var items = ((Stack)value).ToArray(); // convert to array to allow efficient access in reverse order var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options); var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions; // serialize first pushed item first (reverse of enumeration order) bsonWriter.WriteStartArray(); for (var i = items.Length - 1; i >= 0; i--) { BsonSerializer.Serialize(bsonWriter, typeof(object), items[i], itemSerializationOptions); } bsonWriter.WriteEndArray(); } }
/// <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); } }
#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 bytes = (byte[])value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Binary: bsonWriter.WriteBytes(bytes); break; case BsonType.String: var sb = new StringBuilder(bytes.Length * 2); for (int i = 0; i < bytes.Length; i++) { sb.Append(string.Format("{0:x2}", bytes[i])); } bsonWriter.WriteString(sb.ToString()); break; default: var message = string.Format("'{0}' is not a valid Byte[] 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) { // 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) { if (value == null) { bsonWriter.WriteNull(); } else { if (nominalType == typeof(object)) { var actualType = value.GetType(); bsonWriter.WriteStartDocument(); bsonWriter.WriteString("_t", TypeNameDiscriminator.GetDiscriminator(actualType)); bsonWriter.WriteName("_v"); Serialize(bsonWriter, actualType, value, options); // recursive call replacing nominalType with actualType bsonWriter.WriteEndDocument(); return; } var dictionary = (IDictionary)value; var dictionarySerializationOptions = EnsureSerializationOptions(options); var dictionaryRepresentation = dictionarySerializationOptions.Representation; var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions; if (dictionaryRepresentation == DictionaryRepresentation.Dynamic) { dictionaryRepresentation = DictionaryRepresentation.Document; foreach (object key in dictionary.Keys) { var name = key as string; // check for null and type string at the same time if (name == null || name[0] == '$' || name.IndexOf('.') != -1) { dictionaryRepresentation = DictionaryRepresentation.ArrayOfArrays; break; } } } switch (dictionaryRepresentation) { case DictionaryRepresentation.Document: bsonWriter.WriteStartDocument(); foreach (DictionaryEntry dictionaryEntry in dictionary) { bsonWriter.WriteName((string)dictionaryEntry.Key); BsonSerializer.Serialize(bsonWriter, typeof(object), dictionaryEntry.Value, keyValuePairSerializationOptions.ValueSerializationOptions); } bsonWriter.WriteEndDocument(); break; case DictionaryRepresentation.ArrayOfArrays: case DictionaryRepresentation.ArrayOfDocuments: // override KeyValuePair representation if necessary var keyValuePairRepresentation = (dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays) ? BsonType.Array : BsonType.Document; if (keyValuePairSerializationOptions.Representation != keyValuePairRepresentation) { keyValuePairSerializationOptions = new KeyValuePairSerializationOptions( keyValuePairRepresentation, keyValuePairSerializationOptions.KeySerializationOptions, keyValuePairSerializationOptions.ValueSerializationOptions); } bsonWriter.WriteStartArray(); foreach (DictionaryEntry dictionaryEntry in dictionary) { var keyValuePair = new KeyValuePair<object, object>(dictionaryEntry.Key, dictionaryEntry.Value); _keyValuePairSerializer.Serialize( bsonWriter, typeof(KeyValuePair<object, object>), keyValuePair, keyValuePairSerializationOptions); } bsonWriter.WriteEndArray(); break; default: var message = string.Format("'{0}' is not a valid IDictionary representation.", dictionaryRepresentation); throw new BsonSerializationException(message); } } }
/// <summary> /// Serializes a Bitmap to a BsonWriter. /// </summary> /// <param name="bsonWriter">The BsonWriter.</param> /// <param name="nominalType">The nominal type.</param> /// <param name="value">The Bitmap.</param> /// <param name="options">The serialization options.</param> public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { if (nominalType != typeof(Image) && nominalType != typeof(Bitmap)) { var message = string.Format("Nominal type must be Image or Bitmap, not {0}.", nominalType.FullName); throw new ArgumentException(message, "nominalType"); } if (value == null) { bsonWriter.WriteNull(); } else { var actualType = value.GetType(); if (actualType != typeof(Bitmap)) { var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName); throw new ArgumentException(message, "actualType"); } var bitmap = (Bitmap)value; var stream = new MemoryStream(); bitmap.Save(stream, ImageFormat.Bmp); var bytes = stream.ToArray(); if (nominalType == typeof(Image)) { bsonWriter.WriteStartDocument(); bsonWriter.WriteString("_t", "Bitmap"); bsonWriter.WriteBinaryData("bitmap", bytes, BsonBinarySubType.Binary); bsonWriter.WriteEndDocument(); } else { bsonWriter.WriteBinaryData(bytes, BsonBinarySubType.Binary); } } }
/// <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."); } }
/// <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 address = (IPAddress)value; string stringValue; if (address.AddressFamily == AddressFamily.InterNetwork) { stringValue = address.ToString(); } else { stringValue = string.Format("[{0}]", address); } bsonWriter.WriteString(stringValue); } }
/// <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) { throw new ArgumentNullException("value"); } var bsonString = (BsonString)value; bsonWriter.WriteString(bsonString.Value); }
/// <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 actualType = value.GetType(); var discriminator = GetDiscriminator(nominalType, actualType); if (discriminator != null) { bsonWriter.WriteStartDocument(); bsonWriter.WriteString("_t", discriminator); bsonWriter.WriteName("_v"); Serialize(bsonWriter, actualType, value, options); bsonWriter.WriteEndDocument(); return; } var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options); var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions; Type lastItemType = null; IBsonSerializer lastItemSerializer = null; bsonWriter.WriteStartArray(); foreach (var item in EnumerateItemsInSerializationOrder(value)) { var itemType = (item == null) ? typeof(object) : item.GetType(); IBsonSerializer itemSerializer; if (itemType == lastItemType) { itemSerializer = lastItemSerializer; } else { itemSerializer = BsonSerializer.LookupSerializer(itemType); lastItemType = itemType; lastItemSerializer = itemSerializer; } itemSerializer.Serialize(bsonWriter, typeof(object), item, itemSerializationOptions); } bsonWriter.WriteEndArray(); } }
/// <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 dateTime = (DateTime)value; var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options); DateTime utcDateTime; if (dateTimeSerializationOptions.DateOnly) { if (dateTime.TimeOfDay != TimeSpan.Zero) { throw new BsonSerializationException("TimeOfDay component is not zero."); } utcDateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); // not ToLocalTime } else { utcDateTime = BsonUtils.ToUniversalTime(dateTime); } var millisecondsSinceEpoch = BsonUtils.ToMillisecondsSinceEpoch(utcDateTime); switch (dateTimeSerializationOptions.Representation) { case BsonType.DateTime: bsonWriter.WriteDateTime(millisecondsSinceEpoch); break; case BsonType.Document: bsonWriter.WriteStartDocument(); bsonWriter.WriteDateTime("DateTime", millisecondsSinceEpoch); bsonWriter.WriteInt64("Ticks", utcDateTime.Ticks); bsonWriter.WriteEndDocument(); break; case BsonType.Int64: bsonWriter.WriteInt64(utcDateTime.Ticks); break; case BsonType.String: if (dateTimeSerializationOptions.DateOnly) { bsonWriter.WriteString(dateTime.ToString("yyyy-MM-dd")); } else { // we're not using XmlConvert.ToString because of bugs in Mono if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue) { // serialize MinValue and MaxValue as Unspecified so we do NOT get the time zone offset dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified); } else if (dateTime.Kind == DateTimeKind.Unspecified) { // serialize Unspecified as Local se we get the time zone offset dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Local); } bsonWriter.WriteString(dateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK")); } break; default: var message = string.Format("'{0}' is not a valid DateTime representation.", dateTimeSerializationOptions.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) { if (value == null) { bsonWriter.WriteNull(); } else { var stringValue = (string)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.ObjectId: var id = ObjectId.Parse(stringValue); bsonWriter.WriteObjectId(id.Timestamp, id.Machine, id.Pid, id.Increment); break; case BsonType.String: bsonWriter.WriteString(stringValue); break; case BsonType.Symbol: bsonWriter.WriteSymbol(stringValue); break; default: var message = string.Format("'{0}' is not a valid String 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) { if (value == null) { bsonWriter.WriteNull(); } else { var symbol = (BsonSymbol)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.String: bsonWriter.WriteString(symbol.Name); break; case BsonType.Symbol: bsonWriter.WriteSymbol(symbol.Name); break; default: var message = string.Format("'{0}' is not a valid BsonSymbol 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 guid = (Guid)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Binary: var writerGuidRepresentation = bsonWriter.Settings.GuidRepresentation; if (writerGuidRepresentation == GuidRepresentation.Unspecified) { throw new BsonSerializationException("GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified."); } var bytes = GuidConverter.ToBytes(guid, writerGuidRepresentation); var subType = (writerGuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy; bsonWriter.WriteBinaryData(new BsonBinaryData(bytes, subType, writerGuidRepresentation)); break; case BsonType.String: bsonWriter.WriteString(guid.ToString()); break; default: var message = string.Format("'{0}' is not a valid Guid representation.", representationSerializationOptions.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 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 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); } } }
/// <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) { if (value == null) { bsonWriter.WriteNull(); } else { bsonWriter.WriteString(((Uri)value).OriginalString); } }
/// <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 endPoint = (IPEndPoint)value; string stringValue; if (endPoint.AddressFamily == AddressFamily.InterNetwork) { stringValue = string.Format("{0}:{1}", endPoint.Address, endPoint.Port); // IPv4 } else { stringValue = string.Format("[{0}]:{1}", endPoint.Address, endPoint.Port); // IPv6 } bsonWriter.WriteString(stringValue); } }
/// <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 objectId = (ObjectId)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.ObjectId: bsonWriter.WriteObjectId(objectId); break; case BsonType.String: bsonWriter.WriteString(objectId.ToString()); break; default: var message = string.Format("'{0}' is not a valid ObjectId 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) { if (value == null) { bsonWriter.WriteNull(); } else { var cultureInfo = (CultureInfo)value; if (cultureInfo.UseUserOverride) { // the default for UseUserOverride is true so we don't need to serialize it bsonWriter.WriteString(cultureInfo.Name); } else { bsonWriter.WriteStartDocument(); bsonWriter.WriteString("Name", cultureInfo.Name); bsonWriter.WriteBoolean("UseUserOverride", cultureInfo.UseUserOverride); 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) { 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) { if (value == null) { bsonWriter.WriteNull(); } else { if (nominalType == typeof(object)) { var actualType = value.GetType(); bsonWriter.WriteStartDocument(); bsonWriter.WriteString("_t", TypeNameDiscriminator.GetDiscriminator(actualType)); bsonWriter.WriteName("_v"); Serialize(bsonWriter, actualType, value, options); bsonWriter.WriteEndDocument(); return; } var items = (Queue)value; var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options); var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions; bsonWriter.WriteStartArray(); foreach (var item in items) { BsonSerializer.Serialize(bsonWriter, typeof(object), item, itemSerializationOptions); } bsonWriter.WriteEndArray(); } }