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

Writes a BSON null to the writer.
public abstract WriteNull ( ) : void
Результат void
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
                return;
            }
            var metaObject = ((IDynamicMetaObjectProvider)value).GetMetaObject(Expression.Constant(value));
            var memberNames = metaObject.GetDynamicMemberNames().ToList();
            if (memberNames.Count == 0)
            {
                bsonWriter.WriteNull();
                return;
            }

            bsonWriter.WriteStartDocument();
            foreach (var memberName in memberNames)
            {
                bsonWriter.WriteName(memberName);
                var memberValue = BinderHelper.GetMemberValue(value, memberName);
                if (memberValue == null)
                    bsonWriter.WriteNull();
                else
                {
                    var memberType = memberValue.GetType();
                    var serializer = BsonSerializer.LookupSerializer(memberType);
                    serializer.Serialize(bsonWriter, memberType, memberValue, options);
                }
            }
            bsonWriter.WriteEndDocument();
        }
        public override void Serialize(
			BsonWriter bsonWriter,
			Type nominalType,
			object value,
			IBsonSerializationOptions options
			)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
                return;
            }

            var nvc = (NameValueCollection)value;

            bsonWriter.WriteStartArray();
            foreach (var key in nvc.AllKeys)
            {
                foreach (var val in nvc.GetValues(key))
                {
                    bsonWriter.WriteStartArray();
                    StringSerializer.Instance.Serialize(bsonWriter, typeof(string), key, options);
                    StringSerializer.Instance.Serialize(bsonWriter, typeof(string), val, options);
                    bsonWriter.WriteEndArray();
                }
            }
            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)
 {
     if (value == null)
     {
         bsonWriter.WriteNull();
     }
     else
     {
         var lazyBsonArray = (LazyBsonArray)value;
         var slice = lazyBsonArray.Slice;
         if (slice == null)
         {
             BsonArraySerializer.Instance.Serialize(bsonWriter, typeof(BsonArray), lazyBsonArray, options);
         }
         else
         {
             using (var clonedSlice = slice.GetSlice(0, slice.Length))
             {
                 bsonWriter.WriteRawBsonArray(clonedSlice);
             }
         }
     }
 }
Пример #4
0
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            IDictionary<string, object> obj = value as IDictionary<string, object>;
            if (obj == null)
            {
                bsonWriter.WriteNull();
                return;
            }

            bsonWriter.WriteStartDocument();
            foreach (var member in obj)
            {
                bsonWriter.WriteName(member.Key);
                object memberValue = member.Value;

                if (memberValue == null)
                {
                    bsonWriter.WriteNull();
                }
                else
                {
                    nominalType = memberValue.GetType();
                    var serializer = BsonSerializer.LookupSerializer(nominalType);
                    serializer.Serialize(bsonWriter, nominalType, memberValue, options);
                }
            }
            bsonWriter.WriteEndDocument();
        }
 public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
 {
     if (value == null)
     {
         bsonWriter.WriteNull();
     }
     else
     {
         ObjectIdSerializer.Instance.Serialize(bsonWriter, nominalType, IdentifierFinder.GetId(value), options);
     }
 }
Пример #6
0
		public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
		{
			if (value == null)
			{
				bsonWriter.WriteNull();
			}
			else
			{
				var attachment = (Attachment)value;
				ObjectIdSerializer.Instance.Serialize(bsonWriter, nominalType, attachment.ID, options);
			}
		}
 public override void Serialize(BsonWriter bsonWriter, Type nominalType,
     object value, IBsonSerializationOptions options)
 {
     if (value != null)
     {
         bsonWriter.WriteString(value.ToString());
     }
     else
     {
         bsonWriter.WriteNull();
     }
 }
        #pragma warning restore 618

        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            bool serializeIdFirst
        ) {
            if (value == null) {
                bsonWriter.WriteNull();
            } else {
                bsonWriter.WriteBinaryData((byte[]) value, BsonBinarySubType.Binary);
            }
        }
 public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
 {
     if (value == null)
     {
         bsonWriter.WriteNull();
     }
     else
     {
         var actualType = value.GetType();
         var actualTypeSerializer = BsonSerializer.LookupSerializer(actualType);
         actualTypeSerializer.Serialize(bsonWriter, nominalType, value, options);
     }
 }
Пример #10
0
        protected void WriteNullableDouble(BsonWriter writer,string name,double? value)
        {
            writer.WriteName(name);

            if ( value != null )
            {
                writer.WriteDouble(value.Value);
            }
            else
            {
                writer.WriteNull();
            }
        }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 )
 {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         ((BsonArray) value).WriteTo(bsonWriter);
     }
 }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 )
 {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         Type underlyingType = Nullable.GetUnderlyingType(nominalType);
         BsonSerializer.Serialize(bsonWriter, underlyingType, value, serializeIdFirst);
     }
 }
Пример #13
0
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
            {
                throw new PBException("error serialize UrlImage value is null");
            }

            //bsonWriter.WriteString(((WebImage)value).Url);
            string url = ((WebImage)value).Url;
            if (url != null)
                bsonWriter.WriteString(url);
            else
                bsonWriter.WriteNull();
        }
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            // when entry is null, do not throw, just write null
            if (value == null)
            {
                bsonWriter.WriteNull();
                return;
            }

            if (_failOnSerialize)
                throw new InvalidOperationException();

            bsonWriter.WriteString((string)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 coordinates = (GeoJson2DCoordinates)value;

                bsonWriter.WriteStartArray();
                bsonWriter.WriteDouble(coordinates.X);
                bsonWriter.WriteDouble(coordinates.Y);
                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)
 {
     if (value == null)
     {
         bsonWriter.WriteNull();
     }
     else
     {
         var bsonDocumentWrapper = (BsonDocumentWrapper)value;
         ((IBsonSerializable)bsonDocumentWrapper).Serialize(bsonWriter, nominalType, options);
     }
 }
        /// <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 coordinates = (GeoJson3DProjectedCoordinates)value;

                bsonWriter.WriteStartArray();
                bsonWriter.WriteDouble(coordinates.Easting);
                bsonWriter.WriteDouble(coordinates.Northing);
                bsonWriter.WriteDouble(coordinates.Altitude);
                bsonWriter.WriteEndArray();
            }
        }
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var crs = (GeoJsonNamedCoordinateReferenceSystem)value;

                bsonWriter.WriteStartDocument();
                SerializeType(bsonWriter, crs.Type);
                bsonWriter.WriteStartDocument("properties");
                bsonWriter.WriteString("name", crs.Name);
                bsonWriter.WriteEndDocument();
                bsonWriter.WriteEndDocument();
            }
        }
Пример #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 (_trace)
                pb.Trace.WriteLine("ZStringSerializer.Serialize()");

            if (value == null)
            {
                //throw new ArgumentNullException("value");
                throw new PBException("error serialize ZString value is null");
            }

            string stringValue = ((ZString)value).Value;

            if (stringValue == null)
                bsonWriter.WriteNull();
            else
                bsonWriter.WriteString(stringValue);
        }
		public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
		{
			if (value == null)
			{
				bsonWriter.WriteNull();
				return;
			}

			var result = new List<ObjectId>();
			foreach (var item in (IEnumerable)value)
			{
				var idPropertyInfo = item.GetType().GetProperty("ID");
				var id = (ObjectId) idPropertyInfo.GetValue(item, null);
				if (id == ObjectId.Empty)
					throw new Exception("Relational associations must be saved before saving the parent relation");
				result.Add(id);
			}
			EnumerableSerializer.Instance.Serialize(bsonWriter, result.GetType(), result, options);
		}
 /// <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 rawBsonArray = (RawBsonArray)value;
         var slice = rawBsonArray.Slice;
         using (var clonedSlice = slice.GetSlice(0, slice.Length))
         {
             bsonWriter.WriteRawBsonArray(clonedSlice);
         }
     }
 }
Пример #22
0
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (_trace)
                pb.Trace.WriteLine("ZStringArraySerializer.Serialize()");

            if (value == null)
            {
                throw new PBException("error serialize ZStringArray value is null");
            }

            string[] stringValues = ((ZStringArray)value).Values;

            if (stringValues == null)
                bsonWriter.WriteNull();
            else
            {
                bsonWriter.WriteStartArray();
                foreach (string stringValue in stringValues)
                {
                    bsonWriter.WriteString(stringValue);
                }
                bsonWriter.WriteEndArray();
            }
        }
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options
        )
        {
            if (value == null) {
                bsonWriter.WriteNull();
            } else {
                var actualType = value.GetType();
                if (actualType != typeof(object)) {
                    var message = string.Format("ObjectSerializer called for type: {0}", actualType.FullName);
                    throw new InvalidOperationException(message);
                }

                bsonWriter.WriteStartDocument();
                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 void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var actualType = value.GetType();
                if (actualType != typeof(object))
                {
                    var message = string.Format("ObjectSerializer can only be used with type System.Object, not type {0}.", actualType.FullName);
                    throw new InvalidOperationException(message);
                }

                bsonWriter.WriteStartDocument();
                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 void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     IBsonSerializationOptions options)
 {
     if (value == null)
     {
         bsonWriter.WriteNull();
     }
     else
     {
         var serializable = (IBsonSerializable)value;
         serializable.Serialize(bsonWriter, nominalType, options);
     }
 }
        /// <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);
                }
            }
        }
#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)
        {
            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();
            }
        }
 /// <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 script = (BsonJavaScriptWithScope) value;
         bsonWriter.WriteJavaScriptWithScope(script.Code);
         script.Scope.WriteTo(bsonWriter);
     }
 }
        /// <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 bsonDateTime = (BsonDateTime)value;
                var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);

                DateTime utcDateTime = DateTime.MinValue;
                long millisecondsSinceEpoch;
                if (dateTimeSerializationOptions.DateOnly)
                {
                    if (bsonDateTime.Value.TimeOfDay != TimeSpan.Zero)
                    {
                        throw new BsonSerializationException("TimeOfDay component is not zero.");
                    }
                    utcDateTime = DateTime.SpecifyKind(bsonDateTime.Value, DateTimeKind.Utc); // not ToLocalTime
                    millisecondsSinceEpoch = BsonUtils.ToMillisecondsSinceEpoch(utcDateTime);
                }
                else
                {
                    if (bsonDateTime.IsValidDateTime)
                    {
                        utcDateTime = BsonUtils.ToUniversalTime(bsonDateTime.Value);
                    }
                    millisecondsSinceEpoch = bsonDateTime.MillisecondsSinceEpoch;
                }

                switch (dateTimeSerializationOptions.Representation)
                {
                    case BsonType.DateTime:
                        bsonWriter.WriteDateTime(millisecondsSinceEpoch);
                        break;
                    case BsonType.Document:
                        bsonWriter.WriteStartDocument();
                        bsonWriter.WriteDateTime("DateTime", millisecondsSinceEpoch);
                        if (bsonDateTime.IsValidDateTime)
                        {
                            bsonWriter.WriteInt64("Ticks", utcDateTime.Ticks);
                        }
                        else
                        {
                            bsonWriter.WriteUndefined("Ticks");
                        }
                        bsonWriter.WriteEndDocument();
                        break;
                    case BsonType.Int64:
                        if (bsonDateTime.IsValidDateTime)
                        {
                            bsonWriter.WriteInt64(utcDateTime.Ticks);
                        }
                        else
                        {
                            throw new BsonSerializationException("BsonDateTime is not a valid DateTime.");
                        }
                        break;
                    case BsonType.String:
                        if (dateTimeSerializationOptions.DateOnly)
                        {
                            bsonWriter.WriteString(bsonDateTime.Value.ToString("yyyy-MM-dd"));
                        }
                        else
                        {
                            // we're not using XmlConvert.ToString because of bugs in Mono
                            var dateTime = bsonDateTime.Value;
                            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);
                }
            }
        }