WriteInt64() публичный абстрактный Метод

Writes a BSON Int64 to the writer.
public abstract WriteInt64 ( long value ) : void
value long The Int64 value.
Результат void
        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 versionValue = (Version) value;
            var representationSerializationOptions =
                EnsureSerializationOptions<RepresentationSerializationOptions>(options);

            switch (representationSerializationOptions.Representation)
            {
                case BsonType.Int64:
                    bsonWriter.WriteInt64(SerializeVersion(versionValue));
                    break;
                default:
                    throw new BsonSerializationException(string.Format("'{0}' is not a valid Version representation.", representationSerializationOptions.Representation));
            }
        }
Пример #3
0
        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 dateTime = (DateTime) value;
            var dateTimeOptions = (options == null) ? DateTimeSerializationOptions.Defaults : (DateTimeSerializationOptions) options;

            if (dateTimeOptions.DateOnly) {
                if (dateTime.TimeOfDay != TimeSpan.Zero) {
                    throw new BsonSerializationException("TimeOfDay component for DateOnly DateTime value is not zero");
                }
            }

            if (dateTimeOptions.Representation != BsonType.String) {
                if (dateTime.Kind != DateTimeKind.Utc) {
                    if (dateTimeOptions.DateOnly) {
                        dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); // not ToUniversalTime!
                    } else {
                        dateTime = ToUniversalTimeHelper(dateTime);
                    }
                }
            }

            switch (dateTimeOptions.Representation) {
                case BsonType.DateTime:
                    bsonWriter.WriteDateTime(dateTime);
                    break;
                case BsonType.Document:
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteDateTime("DateTime", dateTime);
                    bsonWriter.WriteInt64("Ticks", dateTime.Ticks);
                    bsonWriter.WriteEndDocument();
                    break;
                case BsonType.Int64:
                    bsonWriter.WriteInt64(dateTime.Ticks);
                    break;
                case BsonType.String:
                    if (dateTimeOptions.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 representation for type 'DateTime'", dateTimeOptions.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);

            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);
            }
        }
        /// <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);
            }
        }
        /// <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,
     bool serializeIdFirst
 )
 {
     bsonWriter.WriteInt64((long) value);
 }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 ) {
     // note: the DateTime portion cannot be serialized as a BsonType.DateTime because it is NOT in UTC
     var dateTimeOffset = (DateTimeOffset) value;
     switch (representation) {
         case BsonType.Array:
             bsonWriter.WriteStartArray();
             bsonWriter.WriteInt64("0", dateTimeOffset.DateTime.Ticks);
             bsonWriter.WriteInt32("1", (int) dateTimeOffset.Offset.TotalMinutes);
             bsonWriter.WriteEndArray();
             break;
         case BsonType.String:
             bsonWriter.WriteString(XmlConvert.ToString(dateTimeOffset));
             break;
         default:
             throw new BsonInternalException("Unexpected representation");
     }
 }
 public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
 {
     if (value is decimal) {
         bsonWriter.WriteInt64((long)((decimal)value * 10000));
     }
 }
Пример #12
0
 void IBsonSerializable.Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     IBsonSerializationOptions options
 )
 {
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteString("$ref", collectionName);
     if (id is ObjectId) {
         var objectId = (ObjectId) id;
         bsonWriter.WriteObjectId("$id", objectId.Timestamp, objectId.Machine, objectId.Pid, objectId.Increment);
     } else if (id is Guid) {
         var guid = (Guid) id;
         bsonWriter.WriteBinaryData("$id", guid.ToByteArray(), BsonBinarySubType.Uuid);
     } else if (id is int) {
         bsonWriter.WriteInt32("$id", (int) id);
     } else if (id is long) {
         bsonWriter.WriteInt64("$id", (long) id);
     } else if (id is string) {
         bsonWriter.WriteString("$id", (string) id);
     } else {
         var message = string.Format("Unexpected Id type: {0}", id.GetType().FullName);
         throw new BsonInternalException(message);
     }
     if (databaseName != null) {
         bsonWriter.WriteString("$db", databaseName);
     }
     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 floatValue = (float)value;
     var representationOptions = (RepresentationSerializationOptions)options ?? defaultRepresentationOptions;
     switch (representationOptions.Representation)
     {
         case BsonType.Double:
             bsonWriter.WriteDouble(representationOptions.ToDouble(floatValue));
             break;
         case BsonType.Int32:
             bsonWriter.WriteInt32(representationOptions.ToInt32(floatValue));
             break;
         case BsonType.Int64:
             bsonWriter.WriteInt64(representationOptions.ToInt64(floatValue));
             break;
         case BsonType.String:
             bsonWriter.WriteString(floatValue.ToString("R", NumberFormatInfo.InvariantInfo));
             break;
         default:
             var message = string.Format("'{0}' is not a valid representation for type Single.", representationOptions.Representation);
             throw new BsonSerializationException(message);
     }
 }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 )
 {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         bsonWriter.WriteInt64(((BsonInt64) value).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 doubleValue = (double) value;
     var representationOptions = (RepresentationSerializationOptions) options ?? defaultRepresentationOptions;
     switch (representationOptions.Representation) {
         case BsonType.Double:
             bsonWriter.WriteDouble(doubleValue);
             break;
         case BsonType.Int32:
             bsonWriter.WriteInt32(representationOptions.ToInt32(doubleValue));
             break;
         case BsonType.Int64:
             bsonWriter.WriteInt64(representationOptions.ToInt64(doubleValue));
             break;
         case BsonType.String:
             bsonWriter.WriteString(XmlConvert.ToString(doubleValue));
             break;
         default:
             var message = string.Format("'{0}' is not a valid representation for type 'Double'", representationOptions.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);
     }
 }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     IBsonSerializationOptions options
 ) {
     var timeSpan = (TimeSpan) value;
     var representation = (options == null) ? BsonType.String : ((RepresentationSerializationOptions) options).Representation;
     switch (representation) {
         case BsonType.Int64:
             bsonWriter.WriteInt64(timeSpan.Ticks);
             break;
         case BsonType.String:
             bsonWriter.WriteString(timeSpan.ToString());
             break;
         default:
             var message = string.Format("'{0}' is not a valid representation for type 'TimeSpan'", representation);
             throw new BsonSerializationException(message);
     }
 }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 ) {
     var timeSpan = (TimeSpan) value;
     switch (representation) {
         case BsonType.Int64:
             bsonWriter.WriteInt64(timeSpan.Ticks);
             break;
         case BsonType.String:
             bsonWriter.WriteString(timeSpan.ToString());
             break;
         default:
             throw new BsonInternalException("Unexpected representation");
     }
 }
Пример #19
0
        /// <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 bsonInt64 = (BsonInt64)value;
            bsonWriter.WriteInt64(bsonInt64.Value);
        }
Пример #20
0
 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");
     }
 }
Пример #21
0
        /// <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);
                }
            }
        }
Пример #22
0
        /// <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();
            }
        }
 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 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 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);
            }
        }
 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);
     }
 }
        /// <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 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 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);
            }
        }
Пример #30
0
 private static void SerializeSquare(BsonWriter bsonWriter, OXmlSquare square)
 {
     bsonWriter.WriteStartDocument("Square");
     if (square.StartPoint != null)
     {
         bsonWriter.WriteInt64("StartPointX", square.StartPoint.X);
         bsonWriter.WriteInt64("StartPointY", square.StartPoint.Y);
     }
     bsonWriter.WriteInt64("HorizontalSize", square.HorizontalSize);
     bsonWriter.WriteInt64("VerticalSize", square.VerticalSize);
     if (square.Edited != null)
         bsonWriter.WriteBoolean("Edited", (bool)square.Edited);
     bsonWriter.WriteEndDocument();
 }