WriteEndDocument() публичный Метод

Writes the end of a BSON document to the writer.
public WriteEndDocument ( ) : void
Результат 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)
        {
            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)
 {
     var c = (C)value;
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteString("nominalType", nominalType.Name);
     bsonWriter.WriteInt32("X", c.X);
     bsonWriter.WriteEndDocument();
 }
 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)
        {
            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();
            }
        }
        public override void Serialize(BsonWriter bsonWriter, System.Type nominalType, object value, IBsonSerializationOptions options)
        {
            var rectangle = (Rectangle) value;

            bsonWriter.WriteStartDocument();
            WriteVector(bsonWriter,"Min",rectangle.Min);
            WriteVector(bsonWriter,"Max",rectangle.Max);
            
            bsonWriter.WriteDouble("Width",rectangle.Width);
            bsonWriter.WriteDouble("Height",rectangle.Height);
            bsonWriter.WriteEndDocument();
        }
Пример #7
0
        public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            var method = (MethodInfo)value;

            bsonWriter.WriteStartDocument();
            bsonWriter.WriteName("Type");
            bsonWriter.WriteString(method.DeclaringType.AssemblyQualifiedName);
            bsonWriter.WriteName("Method");
            bsonWriter.WriteString(GetMethodSignature(method));

            bsonWriter.WriteEndDocument();

        }
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
                throw new PBException("serialize OXmlSimpleFieldElement value is null");
            if (_trace)
                pb.Trace.WriteLine("OXmlSimpleFieldElementSerializer.Serialize()");

            OXmlSimpleFieldElement element = (OXmlSimpleFieldElement)value;
            bsonWriter.WriteStartDocument();
            bsonWriter.WriteString("Type", "SimpleField");
            bsonWriter.WriteString("Instruction", element.Instruction);
            bsonWriter.WriteEndDocument();
        }
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
                throw new PBException("serialize OXmlOpenHeader value is null");
            if (_trace)
                pb.Trace.WriteLine("OXmlOpenHeaderElementSerializer.Serialize()");

            OXmlOpenHeaderElement element = (OXmlOpenHeaderElement)value;
            bsonWriter.WriteStartDocument();
            bsonWriter.WriteString("Type", "OpenHeader");
            bsonWriter.WriteString("HeaderType", element.HeaderType.ToString());
            bsonWriter.WriteEndDocument();
        }
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
                throw new PBException("serialize OXmlParagraphElement value is null");
            if (_trace)
                pb.Trace.WriteLine("OXmlParagraphElementSerializer.Serialize()");

            OXmlParagraphElement paragraph = (OXmlParagraphElement)value;
            bsonWriter.WriteStartDocument();
            bsonWriter.WriteString("Type", "Paragraph");
            if (paragraph.Style != null)
                bsonWriter.WriteString("Style", paragraph.Style);
            bsonWriter.WriteEndDocument();
        }
        public override void Serialize(
			BsonWriter bsonWriter,
			Type nominalType,
			object value,
			IBsonSerializationOptions options
			)
        {
            var nvc = (NameValueCollection)value;
            bsonWriter.WriteStartDocument();
            if (nvc != null && nvc.Count > 0)
            {
                foreach (var key in nvc.AllKeys)
                {
                    bsonWriter.WriteString(key.Replace(".", "__period__"), nvc[key]);
                }
            }
            bsonWriter.WriteEndDocument();
        }
        public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options )
        {
            var dictionary = (DynamicDictionary)value;

            bsonWriter.WriteStartDocument();

            bsonWriter.WriteString( "_t", "DynamicDictionary" );

            if( dictionary != null )
            {
                foreach( var entry in dictionary )
                {
                    bsonWriter.WriteName( entry.Key.Replace( '.', '\x03' ) );
                    BsonSerializer.Serialize( bsonWriter, typeof( object ), entry.Value );
                }
            }

            bsonWriter.WriteEndDocument();
        }
Пример #13
0
 /// <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();
 }
Пример #14
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 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);
                }
            }
        }
Пример #15
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 cultureInfo = (CultureInfo)value;
         if (cultureInfo.UseUserOverride)
         {
             // the default for UseUserOverride is true so we don't need to serialize it
             bsonWriter.WriteString(cultureInfo.Name);
         }
         else
         {
             bsonWriter.WriteStartDocument();
             bsonWriter.WriteString("Name", cultureInfo.Name);
             bsonWriter.WriteBoolean("UseUserOverride", cultureInfo.UseUserOverride);
             bsonWriter.WriteEndDocument();
         }
     }
 }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var actualType = value.GetType();
                var discriminator = GetDiscriminator(nominalType, actualType);
                if (discriminator != null)
                {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", discriminator);
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options);
                    bsonWriter.WriteEndDocument();
                    return;
                }

                var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options);
                var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions;
                Type lastItemType = null;
                IBsonSerializer lastItemSerializer = null;

                bsonWriter.WriteStartArray();
                foreach (var item in EnumerateItemsInSerializationOrder(value))
                {
                    var itemType = (item == null) ? typeof(object) : item.GetType();
                    IBsonSerializer itemSerializer;
                    if (itemType == lastItemType)
                    {
                        itemSerializer = lastItemSerializer;
                    }
                    else
                    {
                        itemSerializer = BsonSerializer.LookupSerializer(itemType);
                        lastItemType = itemType;
                        lastItemSerializer = itemSerializer;
                    }
                    itemSerializer.Serialize(bsonWriter, typeof(object), item, itemSerializationOptions);
                }
                bsonWriter.WriteEndArray();
            }
        }
        /// <summary>
        /// Serializes a Bitmap to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The Bitmap.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (nominalType != typeof(Image) && nominalType != typeof(Bitmap))
            {
                var message = string.Format("Nominal type must be Image or Bitmap, not {0}.", nominalType.FullName);
                throw new ArgumentException(message, "nominalType");
            }

            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var actualType = value.GetType();
                if (actualType != typeof(Bitmap))
                {
                    var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName);
                    throw new ArgumentException(message, "actualType");
                }

                var bitmap = (Bitmap)value;
                var stream = new MemoryStream();
                bitmap.Save(stream, ImageFormat.Bmp);
                var bytes = stream.ToArray();

                if (nominalType == typeof(Image))
                {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", "Bitmap");
                    bsonWriter.WriteBinaryData("bitmap", bytes, BsonBinarySubType.Binary);
                    bsonWriter.WriteEndDocument();
                }
                else
                {
                    bsonWriter.WriteBinaryData(bytes, BsonBinarySubType.Binary);
                }
            }
        }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public 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();
            }
        }
#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);
                }
            }
        }
 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();
 }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                if (nominalType == typeof(object))
                {
                    var actualType = value.GetType();
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", TypeNameDiscriminator.GetDiscriminator(actualType));
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options); // recursive call replacing nominalType with actualType
                    bsonWriter.WriteEndDocument();
                    return;
                }

                var dictionary = (IDictionary)value;
                var dictionarySerializationOptions = EnsureSerializationOptions(options);
                var dictionaryRepresentation = dictionarySerializationOptions.Representation;
                var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions;

                if (dictionaryRepresentation == DictionaryRepresentation.Dynamic)
                {
                    dictionaryRepresentation = DictionaryRepresentation.Document;
                    foreach (object key in dictionary.Keys)
                    {
                        var name = key as string; // key might not be a string
                        if (name == null || (name.Length > 0 && name[0] == '$') || name.IndexOf('.') != -1)
                        {
                            dictionaryRepresentation = DictionaryRepresentation.ArrayOfArrays;
                            break;
                        }
                    }
                }

                switch (dictionaryRepresentation)
                {
                    case DictionaryRepresentation.Document:
                        bsonWriter.WriteStartDocument();
                        foreach (DictionaryEntry dictionaryEntry in dictionary)
                        {
                            bsonWriter.WriteName((string)dictionaryEntry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), dictionaryEntry.Value, keyValuePairSerializationOptions.ValueSerializationOptions);
                        }
                        bsonWriter.WriteEndDocument();
                        break;

                    case DictionaryRepresentation.ArrayOfArrays:
                    case DictionaryRepresentation.ArrayOfDocuments:
                        // override KeyValuePair representation if necessary
                        var keyValuePairRepresentation = (dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays) ? BsonType.Array : BsonType.Document;
                        if (keyValuePairSerializationOptions.Representation != keyValuePairRepresentation)
                        {
                            keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
                                keyValuePairRepresentation,
                                keyValuePairSerializationOptions.KeySerializationOptions,
                                keyValuePairSerializationOptions.ValueSerializationOptions);
                        }

                        bsonWriter.WriteStartArray();
                        foreach (DictionaryEntry dictionaryEntry in dictionary)
                        {
                            var keyValuePair = new KeyValuePair<object, object>(dictionaryEntry.Key, dictionaryEntry.Value);
                            _keyValuePairSerializer.Serialize(
                                bsonWriter,
                                typeof(KeyValuePair<object, object>),
                                keyValuePair,
                                keyValuePairSerializationOptions);
                        }
                        bsonWriter.WriteEndArray();
                        break;
                    default:
                        var message = string.Format("'{0}' is not a valid IDictionary representation.", dictionaryRepresentation);
                        throw new BsonSerializationException(message);
                }
            }
        }
        private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap)
        {
            var value = memberMap.Getter(obj);

            if (!memberMap.ShouldSerialize(obj, value))
            {
                return; // don't serialize member
            }

            bsonWriter.WriteName(memberMap.ElementName);
            var nominalType = memberMap.MemberType;
            if (value == null && nominalType.IsInterface)
            {
                bsonWriter.WriteNull();
            }
            else if (value == null && memberMap.MemberTypeIsBsonValue)
            {
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteBoolean("_csharpnull", true);
                bsonWriter.WriteEndDocument();
            }
            else
            {
                var actualType = (value == null) ? nominalType : value.GetType();
                var serializer = memberMap.GetSerializer(actualType);
                serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
            }
        }
 /// <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.WriteStartDocument();
         bsonWriter.WriteBoolean("$csharpnull", true);
         bsonWriter.WriteEndDocument();
     } else {
         bsonWriter.WriteNull();
     }
 }
        //private KeyValuePair<string, string> DeserializeNameValue(BsonReader bsonReader)
        //{
        //    string key = null;
        //    string value = null;
        //    var bsonType = bsonReader.GetCurrentBsonType();
        //    if (bsonType == BsonType.Array)
        //    {
        //        // [["toto1", "tata1"], ["toto2", "tata2"]]

        //        bsonReader.ReadStartArray();
        //        //bsonReader.ReadBsonType();
        //        //var keyType = keyDiscriminatorConvention.GetActualType(bsonReader, typeof(TKey));
        //        //var keySerializer = GetKeySerializer(keyType);
        //        //key = (TKey)keySerializer.Deserialize(bsonReader, typeof(TKey), keyType, keyValuePairSerializationOptions.KeySerializationOptions);
        //        //bsonReader.ReadBsonType();
        //        //var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue));
        //        //var valueSerializer = GetValueSerializer(valueType);
        //        //value = (TValue)valueSerializer.Deserialize(bsonReader, typeof(TValue), valueType, keyValuePairSerializationOptions.ValueSerializationOptions);
        //        key = bsonReader.ReadString();
        //        value = bsonReader.ReadString();
        //        bsonReader.ReadEndArray();
        //    }
        //    else if (bsonType == BsonType.Document)
        //    {
        //        // [{ "k" : "toto1", "v" : "tata1" }, { "k" : "toto2", "v" : "tata2" }]

        //        bsonReader.ReadStartDocument();

        //        var bsonTrie = new BsonTrie<bool>();
        //        bsonTrie.Add("k", true); // is key
        //        bsonTrie.Add("v", false);

        //        bool keyFound = false, valueFound = false;
        //        bool elementFound;
        //        bool elementIsKey;
        //        while (bsonReader.ReadBsonType(bsonTrie, out elementFound, out elementIsKey) != BsonType.EndOfDocument)
        //        {
        //            var name = bsonReader.ReadName();
        //            if (elementFound)
        //            {
        //                if (elementIsKey)
        //                {
        //                    //var keyType = keyDiscriminatorConvention.GetActualType(bsonReader, typeof(TKey));
        //                    //var keySerializer = GetValueSerializer(keyType);
        //                    //key = (TKey)keySerializer.Deserialize(bsonReader, typeof(TKey), keyType, keyValuePairSerializationOptions.KeySerializationOptions);
        //                    key = bsonReader.ReadString();
        //                    keyFound = true;
        //                }
        //                else
        //                {
        //                    //var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue));
        //                    //var valueSerializer = GetValueSerializer(valueType);
        //                    //value = (TValue)valueSerializer.Deserialize(bsonReader, typeof(TValue), valueType, keyValuePairSerializationOptions.ValueSerializationOptions);
        //                    value = bsonReader.ReadString();
        //                    valueFound = true;
        //                }
        //            }
        //            else
        //            {
        //                var message = string.Format("Element '{0}' is not valid for KeyValuePairs (expecting 'k' or 'v').", name);
        //                throw new BsonSerializationException(message);
        //            }
        //        }
        //        bsonReader.ReadEndDocument();

        //        if (!keyFound)
        //        {
        //            //throw new FileFormatException("KeyValuePair item was missing the 'k' element.");
        //            throw new PBException("KeyValuePair item was missing the 'k' element.");
        //        }
        //        if (!valueFound)
        //        {
        //            //throw new FileFormatException("KeyValuePair item was missing the 'v' element.");
        //            throw new PBException("KeyValuePair item was missing the 'v' element.");
        //        }
        //    }
        //    else
        //    {
        //        //var message = string.Format("Cannot deserialize '{0}' from BsonType {1}.", BsonUtils.GetFriendlyTypeName(typeof(KeyValuePair<string, string>)), bsonType);
        //        //throw new FileFormatException(message);
        //        throw new PBException("Cannot deserialize '{0}' from BsonType {1}.", BsonUtils.GetFriendlyTypeName(typeof(KeyValuePair<string, string>)), bsonType);
        //    }
        //    return new KeyValuePair<string, string>(key, 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 (_trace)
                pb.Trace.WriteLine("NameValueCollectionSerializer.Serialize()");

            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                // dont know why nominalType can be an object
                if (nominalType == typeof(object))
                {
                    var actualType = value.GetType();
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", TypeNameDiscriminator.GetDiscriminator(actualType));
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options); // recursive call replacing nominalType with actualType
                    bsonWriter.WriteEndDocument();
                    return;
                }

                // json Dictionary
                // { "toto1" : "tata1", "toto2" : "tata2" }

                //var dictionary = (IDictionary)value;
                NameValueCollection nameValueCollection = (NameValueCollection)value;
                var dictionarySerializationOptions = EnsureSerializationOptions(options);
                var dictionaryRepresentation = dictionarySerializationOptions.Representation;
                var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions;

                if (dictionaryRepresentation == DictionaryRepresentation.Dynamic)
                {
                    // if some keys contain '$', '.' or '\0' serialize as ArrayOfArrays otherwise serialize as Document
                    dictionaryRepresentation = DictionaryRepresentation.Document;
                    foreach (string key in nameValueCollection.Keys)
                    {
                        //var name = key as string; // key might not be a string
                        if (string.IsNullOrEmpty(key) || key[0] == '$' || key.IndexOf('.') != -1 || key.IndexOf('\0') != -1)
                        {
                            dictionaryRepresentation = DictionaryRepresentation.ArrayOfArrays;
                            break;
                        }
                    }
                }

                switch (dictionaryRepresentation)
                {
                    case DictionaryRepresentation.Document:
                        bsonWriter.WriteStartDocument();
                        //foreach (DictionaryEntry dictionaryEntry in dictionary)
                        //{
                        //    bsonWriter.WriteName((string)dictionaryEntry.Key);
                        //    BsonSerializer.Serialize(bsonWriter, typeof(object), dictionaryEntry.Value, keyValuePairSerializationOptions.ValueSerializationOptions);
                        //}
                        for (int i = 0; i < nameValueCollection.Count; i++)
                        {
                            bsonWriter.WriteString(nameValueCollection.GetKey(i), nameValueCollection.Get(i));
                        }
                        bsonWriter.WriteEndDocument();
                        break;

                    case DictionaryRepresentation.ArrayOfArrays:
                    case DictionaryRepresentation.ArrayOfDocuments:
                        // override KeyValuePair representation if necessary
                        var keyValuePairRepresentation = (dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays) ? BsonType.Array : BsonType.Document;
                        if (keyValuePairSerializationOptions.Representation != keyValuePairRepresentation)
                        {
                            keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(keyValuePairRepresentation, keyValuePairSerializationOptions.KeySerializationOptions,
                                keyValuePairSerializationOptions.ValueSerializationOptions);
                        }

                        bsonWriter.WriteStartArray();
                        //foreach (DictionaryEntry dictionaryEntry in dictionary)
                        for (int i = 0; i < nameValueCollection.Count; i++)
                        {
                            //var keyValuePair = new KeyValuePair<object, object>(dictionaryEntry.Key, dictionaryEntry.Value);
                            var keyValuePair = new KeyValuePair<string, string>(nameValueCollection.GetKey(i), nameValueCollection.Get(i));
                            //_keyValuePairSerializer.Serialize(bsonWriter, typeof(KeyValuePair<object, object>), keyValuePair, keyValuePairSerializationOptions);
                            _keyValuePairSerializer.Serialize(bsonWriter, typeof(KeyValuePair<string, string>), keyValuePair, keyValuePairSerializationOptions);
                        }
                        bsonWriter.WriteEndArray();

                        //bsonWriter.WriteStartArray();
                        //for (int i = 0; i < nameValueCollection.Count; i++)
                        //{
                        //    bsonWriter.WriteStartArray();
                        //    bsonWriter.WriteString(nameValueCollection.GetKey(i), nameValueCollection.Get(i));
                        //    bsonWriter.WriteEndArray();
                        //}
                        //bsonWriter.WriteEndArray();
                        break;
                    //case DictionaryRepresentation.ArrayOfDocuments:
                    //    bsonWriter.WriteStartArray();
                    //    for (int i = 0; i < nameValueCollection.Count; i++)
                    //    {
                    //        bsonWriter.WriteStartDocument();
                    //        bsonWriter.WriteString(nameValueCollection.GetKey(i), nameValueCollection.Get(i));
                    //        bsonWriter.WriteEndDocument();
                    //    }
                    //    bsonWriter.WriteEndArray();
                    //    break;
                    default:
                        var message = string.Format("'{0}' is not a valid IDictionary representation.", dictionaryRepresentation);
                        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
            {
                if (nominalType == typeof(object))
                {
                    var actualType = value.GetType();
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", TypeNameDiscriminator.GetDiscriminator(actualType));
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options);
                    bsonWriter.WriteEndDocument();
                    return;
                }

                var items = ((Stack)value).ToArray(); // convert to array to allow efficient access in reverse order
                var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options);
                var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions;

                // serialize first pushed item first (reverse of enumeration order)
                bsonWriter.WriteStartArray();
                for (var i = items.Length - 1; i >= 0; i--)
                {
                    BsonSerializer.Serialize(bsonWriter, typeof(object), items[i], itemSerializationOptions);
                }
                bsonWriter.WriteEndArray();
            }
        }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            var dateTime = (DateTime)value;
            var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);

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

            switch (dateTimeSerializationOptions.Representation)
            {
                case BsonType.DateTime:
                    bsonWriter.WriteDateTime(millisecondsSinceEpoch);
                    break;
                case BsonType.Document:
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteDateTime("DateTime", millisecondsSinceEpoch);
                    bsonWriter.WriteInt64("Ticks", utcDateTime.Ticks);
                    bsonWriter.WriteEndDocument();
                    break;
                case BsonType.Int64:
                    bsonWriter.WriteInt64(utcDateTime.Ticks);
                    break;
                case BsonType.String:
                    if (dateTimeSerializationOptions.DateOnly)
                    {
                        bsonWriter.WriteString(dateTime.ToString("yyyy-MM-dd"));
                    }
                    else
                    {
                        // we're not using XmlConvert.ToString because of bugs in Mono
                        if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue)
                        {
                            // serialize MinValue and MaxValue as Unspecified so we do NOT get the time zone offset
                            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified);
                        }
                        else if (dateTime.Kind == DateTimeKind.Unspecified)
                        {
                            // serialize Unspecified as Local se we get the time zone offset
                            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Local);
                        }
                        bsonWriter.WriteString(dateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK"));
                    }
                    break;
                default:
                    var message = string.Format("'{0}' is not a valid DateTime representation.", dateTimeSerializationOptions.Representation);
                    throw new BsonSerializationException(message);
            }
        }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // could get here with a BsonDocumentWrapper from BsonValueSerializer switch statement
            var wrapper = value as BsonDocumentWrapper;
            if (wrapper != null)
            {
                BsonDocumentWrapperSerializer.Instance.Serialize(bsonWriter, nominalType, value, null);
                return;
            }

            var bsonDocument = (BsonDocument)value;
            var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
            if (documentSerializationOptions == null)
            {
                var message = string.Format(
                    "Serialize method of BsonDocument expected serialization options of type {0}, not {1}.",
                    BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
                    BsonUtils.GetFriendlyTypeName(options.GetType()));
                throw new BsonSerializationException(message);
            }

            bsonWriter.WriteStartDocument();
            BsonElement idElement = null;
            if (documentSerializationOptions.SerializeIdFirst && bsonDocument.TryGetElement("_id", out idElement))
            {
                bsonWriter.WriteName(idElement.Name);
                BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), idElement.Value, null);
            }

            foreach (var element in bsonDocument)
            {
                // if serializeIdFirst is false then idElement will be null and no elements will be skipped
                if (!object.ReferenceEquals(element, idElement))
                {
                    bsonWriter.WriteName(element.Name);
                    BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), element.Value, null);
                }
            }

            bsonWriter.WriteEndDocument();
        }
        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
            {
                // Nullable types are weird because they get boxed as their underlying value type
                // we can best handle that by switching the nominalType to the underlying value type
                // (so VerifyNominalType doesn't fail and we don't get an unnecessary discriminator)
                if (nominalType.IsGenericType && nominalType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    nominalType = nominalType.GetGenericArguments()[0];
                }

                VerifyNominalType(nominalType);
                var actualType = (value == null) ? nominalType : value.GetType();
                var classMap = BsonClassMap.LookupClassMap(actualType);

                bsonWriter.WriteStartDocument();
                var documentOptions = (options == null) ? DocumentSerializationOptions.Defaults : (DocumentSerializationOptions)options;
                BsonMemberMap idMemberMap = null;
                if (documentOptions.SerializeIdFirst)
                {
                    idMemberMap = classMap.IdMemberMap;
                    if (idMemberMap != null)
                    {
                        SerializeMember(bsonWriter, value, idMemberMap);
                    }
                }

                if (actualType != nominalType || classMap.DiscriminatorIsRequired || classMap.HasRootClass)
                {
                    // never write out a discriminator for an anonymous class
                    if (!classMap.IsAnonymous)
                    {
                        var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType);
                        var discriminator = discriminatorConvention.GetDiscriminator(nominalType, actualType);
                        if (discriminator != null)
                        {
                            bsonWriter.WriteName(discriminatorConvention.ElementName);
                            discriminator.WriteTo(bsonWriter);
                        }
                    }
                }

                foreach (var memberMap in classMap.MemberMaps)
                {
                    // note: if serializeIdFirst is false then idMemberMap will be null (so no property will be skipped)
                    if (memberMap != idMemberMap)
                    {
                        if (memberMap == classMap.ExtraElementsMemberMap)
                        {
                            SerializeExtraElements(bsonWriter, value, memberMap);
                        }
                        else
                        {
                            SerializeMember(bsonWriter, value, memberMap);
                        }
                    }
                }
                bsonWriter.WriteEndDocument();
            }
        }