public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { if (value == null) bsonWriter.WriteInt32(0); else bsonWriter.WriteInt32(((ContentItem)value).ID); }
public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { var dateTimeOffset = (DateTimeOffset)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Array: bsonWriter.WriteStartArray(); bsonWriter.WriteInt64(dateTimeOffset.UtcTicks); 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.UtcTicks); bsonWriter.WriteInt32("Offset", (int)dateTimeOffset.Offset.TotalMinutes); bsonWriter.WriteEndDocument(); break; default: var message = string.Format("'{0}' is not a valid DateTimeOffset representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } }
public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { var relation = value as ContentRelation; if (relation == null || !relation.ID.HasValue || relation.ID.Value == 0) bsonWriter.WriteInt32(0); else bsonWriter.WriteInt32(relation.ID.Value); }
public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { var timeOfDay = (TimeOfDay)value; bsonWriter.WriteStartDocument(); bsonWriter.WriteInt32("Hour", timeOfDay.Hour); bsonWriter.WriteInt32("Minute", timeOfDay.Minute); bsonWriter.WriteInt32("Second", timeOfDay.Second); bsonWriter.WriteEndDocument(); }
public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { int intValue; if (value is string && int.TryParse((string)value, out intValue)) bsonWriter.WriteInt32(intValue); else throw new InvalidOperationException(); }
public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { var c = (C)value; bsonWriter.WriteStartDocument(); bsonWriter.WriteString("nominalType", nominalType.Name); bsonWriter.WriteInt32("X", c.X); 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 (_trace) pb.Trace.WriteLine("ZIntSerializer.Serialize()"); if (value == null) { throw new ArgumentNullException("value"); } var zint = (ZInt)value; bsonWriter.WriteInt32(zint.Value); }
public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { var underlyingValue = value.GetType().GetProperty("Value").GetValue(value, null); var underlyingValueType = nominalType.GetConceptValueType(); if (underlyingValueType == typeof(Guid)) { var guid = (Guid)underlyingValue; var guidAsBytes = guid.ToByteArray (); bsonWriter.WriteBinaryData (guidAsBytes, BsonBinarySubType.UuidLegacy, GuidRepresentation.CSharpLegacy); } else if (underlyingValueType == typeof(double)) bsonWriter.WriteDouble ((double)underlyingValue); else if (underlyingValueType == typeof(float)) bsonWriter.WriteDouble ((double)underlyingValue); else if (underlyingValueType == typeof(Int32)) bsonWriter.WriteInt32 ((Int32)underlyingValue); else if (underlyingValueType == typeof(Int64)) bsonWriter.WriteInt64 ((Int64)underlyingValue); else if (underlyingValueType == typeof(bool)) bsonWriter.WriteBoolean ((bool)underlyingValue); else if (underlyingValueType == typeof(string)) bsonWriter.WriteString ((string)(underlyingValue ?? string.Empty)); else if (underlyingValueType == typeof(decimal)) bsonWriter.WriteString (underlyingValue.ToString()); }
/// <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); switch (timeSpanSerializationOptions.Representation) { case BsonType.Double: bsonWriter.WriteDouble(ToDouble(timeSpan, timeSpanSerializationOptions.Units)); break; case BsonType.Int32: bsonWriter.WriteInt32(ToInt32(timeSpan, timeSpanSerializationOptions.Units)); break; case BsonType.Int64: bsonWriter.WriteInt64(ToInt64(timeSpan, timeSpanSerializationOptions.Units)); break; case BsonType.String: bsonWriter.WriteString(timeSpan.ToString()); // not XmlConvert.ToString (we're using .NET's format for TimeSpan) 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 int16Value = (short)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Double: bsonWriter.WriteDouble(representationSerializationOptions.ToDouble(int16Value)); break; case BsonType.Int32: bsonWriter.WriteInt32(representationSerializationOptions.ToInt32(int16Value)); break; case BsonType.Int64: bsonWriter.WriteInt64(representationSerializationOptions.ToInt64(int16Value)); break; case BsonType.String: bsonWriter.WriteString(XmlConvert.ToString(int16Value)); break; default: var message = string.Format("'{0}' is not a valid Int16 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 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); } } }
public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options ) { var actualType = value.GetType(); VerifySerializeTypes(nominalType, actualType); var representation = (options == null) ? 0 : ((RepresentationSerializationOptions) options).Representation; switch (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"); } }
public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { Category category = value as Category; bsonWriter.WriteInt32(category.Id); }
public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, bool serializeIdFirst ) { bsonWriter.WriteInt32((int) 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) { 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) { if (value == null) { bsonWriter.WriteNull(); } else { var profileInfo = (SystemProfileInfo)value; bsonWriter.WriteStartDocument(); bsonWriter.WriteDateTime("ts", BsonUtils.ToMillisecondsSinceEpoch(profileInfo.Timestamp)); if (profileInfo.Info != null) { bsonWriter.WriteString("info", profileInfo.Info); } if (profileInfo.Op != null) { bsonWriter.WriteString("op", profileInfo.Op); } if (profileInfo.Namespace != null) { bsonWriter.WriteString("ns", profileInfo.Namespace); } if (profileInfo.Command != null) { bsonWriter.WriteName("command"); profileInfo.Command.WriteTo(bsonWriter); } if (profileInfo.Query != null) { bsonWriter.WriteName("query"); profileInfo.Query.WriteTo(bsonWriter); } if (profileInfo.UpdateObject != null) { bsonWriter.WriteName("updateobj"); profileInfo.UpdateObject.WriteTo(bsonWriter); } if (profileInfo.CursorId != 0) { bsonWriter.WriteInt64("cursorid", profileInfo.CursorId); } if (profileInfo.NumberToReturn != 0) { bsonWriter.WriteInt32("ntoreturn", profileInfo.NumberToReturn); } if (profileInfo.NumberToSkip != 0) { bsonWriter.WriteInt32("ntoskip", profileInfo.NumberToSkip); } if (profileInfo.Exhaust) { bsonWriter.WriteBoolean("exhaust", profileInfo.Exhaust); } if (profileInfo.NumberScanned != 0) { bsonWriter.WriteInt32("nscanned", profileInfo.NumberScanned); } if (profileInfo.IdHack) { bsonWriter.WriteBoolean("idhack", profileInfo.IdHack); } if (profileInfo.ScanAndOrder) { bsonWriter.WriteBoolean("scanAndOrder", profileInfo.ScanAndOrder); } if (profileInfo.Moved) { bsonWriter.WriteBoolean("moved", profileInfo.Moved); } if (profileInfo.FastMod) { bsonWriter.WriteBoolean("fastmod", profileInfo.FastMod); } if (profileInfo.FastModInsert) { bsonWriter.WriteBoolean("fastmodinsert", profileInfo.FastModInsert); } if (profileInfo.Upsert) { bsonWriter.WriteBoolean("upsert", profileInfo.Upsert); } if (profileInfo.KeyUpdates != 0) { bsonWriter.WriteInt32("keyUpdates", profileInfo.KeyUpdates); } if (profileInfo.Exception != null) { bsonWriter.WriteString("exception", profileInfo.Exception); } if (profileInfo.ExceptionCode != 0) { bsonWriter.WriteInt32("exceptionCode", profileInfo.ExceptionCode); } if (profileInfo.NumberReturned != 0) { bsonWriter.WriteInt32("nreturned", profileInfo.NumberReturned); } if (profileInfo.ResponseLength != 0) { bsonWriter.WriteInt32("responseLength", profileInfo.ResponseLength); } bsonWriter.WriteDouble("millis", profileInfo.Duration.TotalMilliseconds); if (profileInfo.Client != null) { bsonWriter.WriteString("client", profileInfo.Client); } if (profileInfo.User != null) { bsonWriter.WriteString("user", profileInfo.User); } if (profileInfo.Error != null) { bsonWriter.WriteString("err", profileInfo.Error); } if (profileInfo.Abbreviated != null) { bsonWriter.WriteString("abbreviated", profileInfo.Abbreviated); } 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 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); } }
public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options ) { var uint16Value = (ushort) value; var representation = (options == null) ? BsonType.Int32 : ((RepresentationSerializationOptions) options).Representation; switch (representation) { case BsonType.Double: bsonWriter.WriteDouble(uint16Value); break; case BsonType.Int32: bsonWriter.WriteInt32(uint16Value); break; case BsonType.Int64: bsonWriter.WriteInt64(uint16Value); break; case BsonType.String: bsonWriter.WriteString(XmlConvert.ToString(uint16Value)); break; default: var message = string.Format("'{0}' is not a valid representation for type 'UInt16'", 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 byteValue = (byte)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Binary: bsonWriter.WriteBytes(new byte[] { byteValue }); break; case BsonType.Int32: bsonWriter.WriteInt32(byteValue); break; case BsonType.Int64: bsonWriter.WriteInt64(byteValue); break; case BsonType.String: bsonWriter.WriteString(string.Format("{0:x2}", byteValue)); break; default: var message = string.Format("'{0}' is not a valid Byte representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } }
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 representation = (options == null) ? BsonType.Array : ((RepresentationSerializationOptions) options).Representation; switch (representation) { case BsonType.Array: bsonWriter.WriteStartArray(); bsonWriter.WriteInt64("0", dateTimeOffset.Ticks); bsonWriter.WriteInt32("1", (int) dateTimeOffset.Offset.TotalMinutes); bsonWriter.WriteEndArray(); break; case BsonType.Document: bsonWriter.WriteStartDocument(); bsonWriter.WriteDateTime("DateTime", 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 representation for type 'DateTimeOffset'", representation); throw new BsonSerializationException(message); } }
void IBsonSerializable.Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options) { bsonWriter.WriteStartDocument(); bsonWriter.WriteDateTime("ts", BsonUtils.ToMillisecondsSinceEpoch(_timestamp)); if (_info != null) { bsonWriter.WriteString("info", _info); } if (_op != null) { bsonWriter.WriteString("op", _op); } if (_namespace != null) { bsonWriter.WriteString("ns", _namespace); } if (_command != null) { bsonWriter.WriteName("command"); _command.WriteTo(bsonWriter); } if (_query != null) { bsonWriter.WriteName("query"); _query.WriteTo(bsonWriter); } if (_updateObject != null) { bsonWriter.WriteName("updateobj"); _updateObject.WriteTo(bsonWriter); } if (_cursorId != 0) { bsonWriter.WriteInt64("cursorid", _cursorId); } if (_numberToReturn != 0) { bsonWriter.WriteInt32("ntoreturn", _numberToReturn); } if (_numberToSkip != 0) { bsonWriter.WriteInt32("ntoskip", _numberToSkip); } if (_exhaust) { bsonWriter.WriteBoolean("exhaust", _exhaust); } if (_numberScanned != 0) { bsonWriter.WriteInt32("nscanned", _numberScanned); } if (_idHack) { bsonWriter.WriteBoolean("idhack", _idHack); } if (_scanAndOrder) { bsonWriter.WriteBoolean("scanAndOrder", _scanAndOrder); } if (_moved) { bsonWriter.WriteBoolean("moved", _moved); } if (_fastMod) { bsonWriter.WriteBoolean("fastmod", _fastMod); } if (_fastModInsert) { bsonWriter.WriteBoolean("fastmodinsert", _fastModInsert); } if (_upsert) { bsonWriter.WriteBoolean("upsert", _upsert); } if (_keyUpdates != 0) { bsonWriter.WriteInt32("keyUpdates", _keyUpdates); } if (_exception != null) { bsonWriter.WriteString("exception", _exception); } if (_exceptionCode != 0) { bsonWriter.WriteInt32("exceptionCode", _exceptionCode); } if (_numberReturned != 0) { bsonWriter.WriteInt32("nreturned", _numberReturned); } if (_responseLength != 0) { bsonWriter.WriteInt32("responseLength", _responseLength); } bsonWriter.WriteDouble("millis", _duration.TotalMilliseconds); if (_client != null) { bsonWriter.WriteString("client", _client); } if (_user != null) { bsonWriter.WriteString("user", _user); } if (_error != null) { bsonWriter.WriteString("err", _error); } if (_abbreviated != null) { bsonWriter.WriteString("abbreviated", _abbreviated); } bsonWriter.WriteEndDocument(); }
public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options ) { var decimalValue = (Decimal) value; var representation = (options == null) ? BsonType.String : ((RepresentationSerializationOptions) options).Representation; switch (representation) { case BsonType.Array: bsonWriter.WriteStartArray(); var bits = Decimal.GetBits(decimalValue); bsonWriter.WriteInt32("0", bits[0]); bsonWriter.WriteInt32("1", bits[1]); bsonWriter.WriteInt32("2", bits[2]); bsonWriter.WriteInt32("3", bits[3]); bsonWriter.WriteEndArray(); break; case BsonType.String: bsonWriter.WriteString(XmlConvert.ToString(decimalValue)); break; default: var message = string.Format("'{0}' is not a valid representation for type 'Decimal'", 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 boolValue = (bool)value; var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); switch (representationSerializationOptions.Representation) { case BsonType.Boolean: bsonWriter.WriteBoolean(boolValue); break; case BsonType.Double: bsonWriter.WriteDouble(boolValue ? 1.0 : 0.0); break; case BsonType.Int32: bsonWriter.WriteInt32(boolValue ? 1 : 0); break; case BsonType.Int64: bsonWriter.WriteInt64(boolValue ? 1 : 0); break; case BsonType.String: bsonWriter.WriteString(XmlConvert.ToString(boolValue)); break; default: var message = string.Format("'{0}' is not a valid Boolean representation.", representationSerializationOptions.Representation); throw new BsonSerializationException(message); } }
public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options ) { var sbyteValue = (sbyte) value; var representation = (options == null) ? BsonType.Int32 : ((RepresentationSerializationOptions) options).Representation; switch (representation) { case BsonType.Binary: bsonWriter.WriteBinaryData(new byte[] { (byte) sbyteValue }, BsonBinarySubType.Binary); break; case BsonType.Int32: bsonWriter.WriteInt32(sbyteValue); break; case BsonType.Int64: bsonWriter.WriteInt64(sbyteValue); break; case BsonType.String: bsonWriter.WriteString(string.Format("{0:x2}", sbyteValue)); break; default: var message = string.Format("'{0}' is not a valid representation for type 'Byte'", 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); } }
public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options ) { var floatValue = (float) value; var doubleValue = (floatValue == float.MinValue) ? double.MinValue : (floatValue == float.MaxValue) ? double.MaxValue : floatValue; var representation = (options == null) ? BsonType.Double : ((RepresentationSerializationOptions) options).Representation; switch (representation) { case BsonType.Double: bsonWriter.WriteDouble(doubleValue); break; case BsonType.Int32: bsonWriter.WriteInt32((int) doubleValue); break; case BsonType.Int64: bsonWriter.WriteInt64((long) doubleValue); break; case BsonType.String: bsonWriter.WriteString(XmlConvert.ToString(doubleValue)); break; default: var message = string.Format("'{0}' is not a valid representation for type 'Single'", 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) { if (value == null) { throw new ArgumentNullException("value"); } var bsonInt32 = (BsonInt32)value; bsonWriter.WriteInt32(bsonInt32.Value); }