WriteStartArray() public method

Writes the start of a BSON array to the writer.
public WriteStartArray ( ) : void
return void
        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();
        }
        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);
            }
        }
        /// <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();
            }
        }
 public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
 {
     bsonWriter.WriteStartArray();
     if (value != null)
     {
         var array = value as IEnumerable;
         if (array != null)
         {
             foreach (var id in from object arrayItem in array select IdentifierFinder.GetId(arrayItem))
             {
                 BsonSerializer.Serialize(bsonWriter, id.GetType(), id);
             }
         }
     }
     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 coordinates = (GeoJson3DProjectedCoordinates)value;

                bsonWriter.WriteStartArray();
                bsonWriter.WriteDouble(coordinates.Easting);
                bsonWriter.WriteDouble(coordinates.Northing);
                bsonWriter.WriteDouble(coordinates.Altitude);
                bsonWriter.WriteEndArray();
            }
        }
Example #6
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();
            }
        }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var actualType = value.GetType();
                var discriminator = GetDiscriminator(nominalType, actualType);
                if (discriminator != null)
                {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", discriminator);
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options);
                    bsonWriter.WriteEndDocument();
                    return;
                }

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

                bsonWriter.WriteStartArray();
                foreach (var item in EnumerateItemsInSerializationOrder(value))
                {
                    var itemType = (item == null) ? typeof(object) : item.GetType();
                    IBsonSerializer itemSerializer;
                    if (itemType == lastItemType)
                    {
                        itemSerializer = lastItemSerializer;
                    }
                    else
                    {
                        itemSerializer = BsonSerializer.LookupSerializer(itemType);
                        lastItemType = itemType;
                        lastItemSerializer = itemSerializer;
                    }
                    itemSerializer.Serialize(bsonWriter, typeof(object), item, itemSerializationOptions);
                }
                bsonWriter.WriteEndArray();
            }
        }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var dictionary = (IDictionary)value;

                if (nominalType == typeof(object))
                {
                    var actualType = value.GetType();
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", BsonClassMap.GetTypeNameDiscriminator(actualType));
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options); // recursive call replacing nominalType with actualType
                    bsonWriter.WriteEndDocument();
                    return;
                }

                var dictionaryOptions = options as DictionarySerializationOptions;
                if (dictionaryOptions == null)
                {
                    // support RepresentationSerializationOptions for backward compatibility
                    var representationOptions = options as RepresentationSerializationOptions;
                    if (representationOptions != null)
                    {
                        switch (representationOptions.Representation)
                        {
                            case BsonType.Array:
                                dictionaryOptions = DictionarySerializationOptions.ArrayOfArrays;
                                break;
                            case BsonType.Document:
                                dictionaryOptions = DictionarySerializationOptions.Document;
                                break;
                            default:
                                var message = string.Format("BsonType {0} is not a valid representation for a Dictionary.", representationOptions.Representation);
                                throw new BsonSerializationException(message);
                        }
                    }

                    if (dictionaryOptions == null)
                    {
                        dictionaryOptions = DictionarySerializationOptions.Defaults;
                    }
                }

                var representation = dictionaryOptions.Representation;
                if (representation == DictionaryRepresentation.Dynamic)
                {
                    representation = DictionaryRepresentation.Document;
                    foreach (object key in dictionary.Keys)
                    {
                        var name = key as string; // check for null and type string at the same time
                        if (name == null || name.StartsWith("$") || name.Contains("."))
                        {
                            representation = DictionaryRepresentation.ArrayOfArrays;
                            break;
                        }
                    }
                }

                switch (representation)
                {
                    case DictionaryRepresentation.Document:
                        bsonWriter.WriteStartDocument();
                        foreach (DictionaryEntry entry in dictionary)
                        {
                            bsonWriter.WriteName((string)entry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                        }
                        bsonWriter.WriteEndDocument();
                        break;
                    case DictionaryRepresentation.ArrayOfArrays:
                        bsonWriter.WriteStartArray();
                        foreach (DictionaryEntry entry in dictionary)
                        {
                            bsonWriter.WriteStartArray();
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                            bsonWriter.WriteEndArray();
                        }
                        bsonWriter.WriteEndArray();
                        break;
                    case DictionaryRepresentation.ArrayOfDocuments:
                        bsonWriter.WriteStartArray();
                        foreach (DictionaryEntry entry in dictionary)
                        {
                            bsonWriter.WriteStartDocument();
                            bsonWriter.WriteName("k");
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Key);
                            bsonWriter.WriteName("v");
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                            bsonWriter.WriteEndDocument();
                        }
                        bsonWriter.WriteEndArray();
                        break;
                    default:
                        var message = string.Format("'{0}' is not a valid representation for type IDictionary.", representation);
                        throw new BsonSerializationException(message);
                }
            }
        }
 private static void WriteStyleParagraphProperties(BsonWriter bsonWriter, OXmlStyleParagraphProperties element)
 {
     if (element.AdjustRightIndent != null)
         bsonWriter.WriteBoolean("AdjustRightIndent", (bool)element.AdjustRightIndent);
     if (element.AutoSpaceDE != null)
         bsonWriter.WriteBoolean("AutoSpaceDE", (bool)element.AutoSpaceDE);
     if (element.AutoSpaceDN != null)
         bsonWriter.WriteBoolean("AutoSpaceDN", (bool)element.AutoSpaceDN);
     if (element.BiDi != null)
         bsonWriter.WriteBoolean("BiDi", (bool)element.BiDi);
     if (element.ContextualSpacing != null)
         bsonWriter.WriteBoolean("ContextualSpacing", (bool)element.ContextualSpacing);
     if (element.Justification != null)
         bsonWriter.WriteString("Justification", element.Justification.ToString());
     if (element.KeepLines != null)
         bsonWriter.WriteBoolean("KeepLines", (bool)element.KeepLines);
     if (element.KeepNext != null)
         bsonWriter.WriteBoolean("KeepNext", (bool)element.KeepNext);
     if (element.Kinsoku != null)
         bsonWriter.WriteBoolean("Kinsoku", (bool)element.Kinsoku);
     if (element.MirrorIndents != null)
         bsonWriter.WriteBoolean("MirrorIndents", (bool)element.MirrorIndents);
     if (element.OutlineLevel != null)
         bsonWriter.WriteInt32("OutlineLevel", (int)element.OutlineLevel);
     if (element.OverflowPunctuation != null)
         bsonWriter.WriteBoolean("OverflowPunctuation", (bool)element.OverflowPunctuation);
     if (element.PageBreakBefore != null)
         bsonWriter.WriteBoolean("PageBreakBefore", (bool)element.PageBreakBefore);
     if (element.SnapToGrid != null)
         bsonWriter.WriteBoolean("SnapToGrid", (bool)element.SnapToGrid);
     if (element.SpacingBetweenLines != null)
     {
         bsonWriter.WriteStartDocument("SpacingBetweenLines");
         WriteSpacingBetweenLines(bsonWriter, element.SpacingBetweenLines);
         bsonWriter.WriteEndDocument();
     }
     if (element.SuppressAutoHyphens != null)
         bsonWriter.WriteBoolean("SuppressAutoHyphens", (bool)element.SuppressAutoHyphens);
     if (element.SuppressLineNumbers != null)
         bsonWriter.WriteBoolean("SuppressLineNumbers", (bool)element.SuppressLineNumbers);
     if (element.SuppressOverlap != null)
         bsonWriter.WriteBoolean("SuppressOverlap", (bool)element.SuppressOverlap);
     if (element.Tabs != null)
     {
         bsonWriter.WriteStartArray("Tabs");
         WriteTabs(bsonWriter, element.Tabs);
         bsonWriter.WriteEndArray();
     }
     if (element.TextAlignment != null)
         bsonWriter.WriteString("TextAlignment", element.TextAlignment.ToString());
     if (element.TextBoxTightWrap != null)
         bsonWriter.WriteString("TextBoxTightWrap", element.TextBoxTightWrap.ToString());
     if (element.TextDirection != null)
         bsonWriter.WriteString("TextDirection", element.TextDirection.ToString());
     if (element.TopLinePunctuation != null)
         bsonWriter.WriteBoolean("TopLinePunctuation", (bool)element.TopLinePunctuation);
     if (element.WidowControl != null)
         bsonWriter.WriteBoolean("WidowControl", (bool)element.WidowControl);
     if (element.WordWrap != null)
         bsonWriter.WriteBoolean("WordWrap", (bool)element.WordWrap);
 }
        //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 = ((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();
            }
        }
 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");
     }
 }
        /// <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 array = (BsonArray)value;
            bsonWriter.WriteStartArray();
            for (int i = 0; i < array.Count; i++)
            {
                BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), array[i], null);
            }
            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 = (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();
            }
        }
Example #15
0
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (_trace)
                pb.Trace.WriteLine("WebHeaderSerializer.Serialize()");

            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 headers = (WebHeaderCollection)value;
                bsonWriter.WriteStartDocument();
                for (int i = 0; i < headers.Count; i++)
                {
                    bsonWriter.WriteName(headers.GetKey(i));
                    string[] headerValues = headers.GetValues(i);
                    if (headerValues.Length == 1)
                        bsonWriter.WriteString(headerValues[0]);
                    else
                    {
                        bsonWriter.WriteStartArray();
                        foreach (string headerValue in headerValues)
                        {
                            bsonWriter.WriteString(headerValue);
                        }
                        bsonWriter.WriteEndArray();
                    }
                }
                bsonWriter.WriteEndDocument();
            }
        }
 private static void SerializePolygon(BsonWriter bsonWriter, OXmlPolygon polygon)
 {
     bsonWriter.WriteStartDocument("Polygon");
     if (polygon.StartPoint != null)
     {
         bsonWriter.WriteInt64("StartPointX", polygon.StartPoint.X);
         bsonWriter.WriteInt64("StartPointY", polygon.StartPoint.Y);
     }
     bsonWriter.WriteStartArray("LinesTo");
     foreach (OXmlPoint2DType lineTo in polygon.LinesTo)
     {
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteInt64("X", lineTo.X);
         bsonWriter.WriteInt64("Y", lineTo.Y);
         bsonWriter.WriteEndDocument();
     }
     bsonWriter.WriteEndArray();
     if (polygon.Edited != null)
         bsonWriter.WriteBoolean("Edited", (bool)polygon.Edited);
     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
     {
         bsonWriter.WriteStartArray();
         foreach (var element in (Queue)value)
         {
             BsonSerializer.Serialize(bsonWriter, typeof(object), element);
         }
         bsonWriter.WriteEndArray();
     }
 }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 )
 {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         bsonWriter.WriteStartArray();
         int index = 0;
         foreach (var element in (IEnumerable) value) {
             bsonWriter.WriteName(index.ToString());
             BsonSerializer.Serialize(bsonWriter, typeof(object), element);
             index++;
         }
         bsonWriter.WriteEndArray();
     }
 }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 )
 {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         bsonWriter.WriteStartArray();
         var outputOrder = new ArrayList((Stack) value); // serialize first pushed item first (reverse of enumerator order)
         outputOrder.Reverse();
         int index = 0;
         foreach (var element in outputOrder) {
             bsonWriter.WriteName(index.ToString());
             BsonSerializer.Serialize(bsonWriter, typeof(object),  element);
             index++;
         }
         bsonWriter.WriteEndArray();
     }
 }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 ) {
     var @decimal = (Decimal) value;
     switch (representation) {
         case BsonType.Array:
             bsonWriter.WriteStartArray();
             var bits = Decimal.GetBits(@decimal);
             bsonWriter.WriteInt32("0", bits[0]);
             bsonWriter.WriteInt32("1", bits[1]);
             bsonWriter.WriteInt32("2", bits[2]);
             bsonWriter.WriteInt32("3", bits[3]);
             bsonWriter.WriteEndArray();
             break;
         case BsonType.String:
             bsonWriter.WriteString(XmlConvert.ToString(@decimal));
             break;
         default:
             throw new BsonInternalException("Unexpected representation");
     }
 }
        /// <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);
                }
            }
        }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     IBsonSerializationOptions options
 )
 {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         var dictionary = (IDictionary) value;
         if (dictionary.Keys.Cast<object>().All(o => o.GetType() == typeof(string))) {
             bsonWriter.WriteStartDocument();
             int index = 0;
             foreach (DictionaryEntry entry in dictionary) {
                 bsonWriter.WriteName((string) entry.Key);
                 BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                 index++;
             }
             bsonWriter.WriteEndDocument();
         } else {
             bsonWriter.WriteStartArray();
             foreach (DictionaryEntry entry in dictionary) {
                 bsonWriter.WriteStartArray();
                 BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Key);
                 BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                 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 dictionary = (IDictionary) value;

                var representationOptions = options as RepresentationSerializationOptions;
                BsonType representation;
                if (representationOptions == null) {
                    representation = BsonType.Document;
                    foreach (object key in dictionary.Keys) {
                        var name = key as string; // check for null and type string at the same time
                        if (name == null || name.StartsWith("$") || name.Contains(".")) {
                            representation = BsonType.Array;
                            break;
                        }
                    }
                } else {
                    representation = representationOptions.Representation;
                }

                switch (representation) {
                    case BsonType.Document:
                        bsonWriter.WriteStartDocument();
                        foreach (DictionaryEntry entry in dictionary) {
                            bsonWriter.WriteName((string) entry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                        }
                        bsonWriter.WriteEndDocument();
                        break;
                    case BsonType.Array:
                        bsonWriter.WriteStartArray();
                        foreach (DictionaryEntry entry in dictionary) {
                            bsonWriter.WriteStartArray();
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                            bsonWriter.WriteEndArray();
                        }
                        bsonWriter.WriteEndArray();
                        break;
                    default:
                        var message = string.Format("'{0}' is not a valid representation for type IDictionary.", representation);
                        throw new BsonSerializationException(message);
                }
            }
        }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                if (nominalType == typeof(object))
                {
                    var actualType = value.GetType();
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", TypeNameDiscriminator.GetDiscriminator(actualType));
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options); // recursive call replacing nominalType with actualType
                    bsonWriter.WriteEndDocument();
                    return;
                }

                var dictionary = (IDictionary)value;
                var dictionarySerializationOptions = EnsureSerializationOptions(options);
                var representation = dictionarySerializationOptions.Representation;
                var itemSerializationOptions = dictionarySerializationOptions.ItemSerializationOptions;

                if (representation == DictionaryRepresentation.Dynamic)
                {
                    representation = DictionaryRepresentation.Document;
                    foreach (object key in dictionary.Keys)
                    {
                        var name = key as string; // check for null and type string at the same time
                        if (name == null || name[0] == '$' || name.IndexOf('.') != -1)
                        {
                            representation = DictionaryRepresentation.ArrayOfArrays;
                            break;
                        }
                    }
                }

                switch (representation)
                {
                    case DictionaryRepresentation.Document:
                        bsonWriter.WriteStartDocument();
                        foreach (DictionaryEntry entry in dictionary)
                        {
                            bsonWriter.WriteName((string)entry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value, itemSerializationOptions);
                        }
                        bsonWriter.WriteEndDocument();
                        break;
                    case DictionaryRepresentation.ArrayOfArrays:
                        bsonWriter.WriteStartArray();
                        foreach (DictionaryEntry entry in dictionary)
                        {
                            bsonWriter.WriteStartArray();
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Key, null); // no serialization options for key
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value, itemSerializationOptions);
                            bsonWriter.WriteEndArray();
                        }
                        bsonWriter.WriteEndArray();
                        break;
                    case DictionaryRepresentation.ArrayOfDocuments:
                        bsonWriter.WriteStartArray();
                        foreach (DictionaryEntry entry in dictionary)
                        {
                            bsonWriter.WriteStartDocument();
                            bsonWriter.WriteName("k");
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Key, null);  // no serialization options for key
                            bsonWriter.WriteName("v");
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value, itemSerializationOptions);
                            bsonWriter.WriteEndDocument();
                        }
                        bsonWriter.WriteEndArray();
                        break;
                    default:
                        var message = string.Format("'{0}' is not a valid IDictionary representation.", representation);
                        throw new BsonSerializationException(message);
                }
            }
        }
 /// <summary>
 /// Serializes an object to a BsonWriter.
 /// </summary>
 /// <param name="bsonWriter">The BsonWriter.</param>
 /// <param name="nominalType">The nominal type.</param>
 /// <param name="value">The object.</param>
 /// <param name="options">The serialization options.</param>
 public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
 {
     if (value == null)
     {
         bsonWriter.WriteNull();
     }
     else
     {
         bsonWriter.WriteStartArray();
         var outputOrder = new ArrayList((Stack)value); // serialize first pushed item first (reverse of enumerator order)
         outputOrder.Reverse();
         foreach (var element in outputOrder)
         {
             BsonSerializer.Serialize(bsonWriter, typeof(object), element);
         }
         bsonWriter.WriteEndArray();
     }
 }
 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 decimalValue = (Decimal) value;
     var representation = (options == null) ? BsonType.String : ((RepresentationSerializationOptions) options).Representation;
     switch (representation) {
         case BsonType.Array:
             bsonWriter.WriteStartArray();
             var bits = Decimal.GetBits(decimalValue);
             bsonWriter.WriteInt32("0", bits[0]);
             bsonWriter.WriteInt32("1", bits[1]);
             bsonWriter.WriteInt32("2", bits[2]);
             bsonWriter.WriteInt32("3", bits[3]);
             bsonWriter.WriteEndArray();
             break;
         case BsonType.String:
             bsonWriter.WriteString(XmlConvert.ToString(decimalValue));
             break;
         default:
             var message = string.Format("'{0}' is not a valid representation for type 'Decimal'", representation);
             throw new BsonSerializationException(message);
     }
 }