예제 #1
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 actualType = value.GetType();
                VerifyTypes(nominalType, actualType, typeof(T[]));

                if (nominalType != typeof(object))
                {
                    bsonWriter.WriteStartArray();
                    var array = (T[])value;
                    for (int index = 0; index < array.Length; index++)
                    {
                        BsonSerializer.Serialize(bsonWriter, typeof(T), array[index]);
                    }
                    bsonWriter.WriteEndArray();
                }
                else
                {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", BsonClassMap.GetTypeNameDiscriminator(actualType));
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options);
                    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();
                VerifyTypes(nominalType, actualType, typeof(T[, , ]));

                if (nominalType != typeof(object))
                {
                    bsonWriter.WriteStartArray();
                    var array   = (T[, , ])value;
                    var length1 = array.GetLength(0);
                    var length2 = array.GetLength(1);
                    var length3 = array.GetLength(2);
                    for (int i = 0; i < length1; i++)
                    {
                        bsonWriter.WriteStartArray();
                        for (int j = 0; j < length2; j++)
                        {
                            bsonWriter.WriteStartArray();
                            for (int k = 0; k < length3; k++)
                            {
                                BsonSerializer.Serialize(bsonWriter, typeof(T), array[i, j, k]);
                            }
                            bsonWriter.WriteEndArray();
                        }
                        bsonWriter.WriteEndArray();
                    }
                    bsonWriter.WriteEndArray();
                }
                else
                {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", BsonClassMap.GetTypeNameDiscriminator(actualType));
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options);
                    bsonWriter.WriteEndDocument();
                }
            }
        }
예제 #3
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 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);
                }
            }
        }