Ejemplo n.º 1
0
        public void SetValue(string name, BsonValue value)
        {
            object dotNetObject;

            switch (value.BsonType)
            {
            case BsonType.Int32: dotNetObject = value.AsInt32; break;

            case BsonType.Int64: dotNetObject = value.AsInt64; break;

            case BsonType.String: dotNetObject = value.AsString; break;

            case BsonType.Double: dotNetObject = value.AsDouble; break;

            case BsonType.Boolean: dotNetObject = value.AsBoolean; break;

            case BsonType.DateTime: dotNetObject = value.ToUniversalTime(); break;

            case BsonType.Null: dotNetObject = null; break;

            default: throw new NotSupportedException(string.Format("the bson type [{0}] is not supported", value.BsonType));
            }

            _values.Add(name, dotNetObject);
        }
        /// <summary>
        ///     BsonValue转展示用字符
        /// </summary>
        /// <param name="bsonValue"></param>
        /// <returns></returns>
        public static String ConvertToString(BsonValue bsonValue)
        {
            //二进制数据
            if (bsonValue.IsBsonBinaryData)
            {
                return("[Binary]");
            }
            //空值
            if (bsonValue.IsBsonNull)
            {
                return("[Empty]");
            }
            //文档
            if (bsonValue.IsBsonDocument)
            {
                return(bsonValue + "[Contains" + bsonValue.ToBsonDocument().ElementCount + "Documents]");
            }
            //时间
            if (bsonValue.IsBsonDateTime)
            {
                DateTime bsonData = bsonValue.ToUniversalTime();
                //@flydreamer提出的本地化时间要求
                return(bsonData.ToLocalTime().ToString());
            }

            //字符
            if (bsonValue.IsString)
            {
                //只有在字符的时候加上""
                return("\"" + bsonValue + "\"");
            }

            //其他
            return(bsonValue.ToString());
        }
Ejemplo n.º 3
0
 /// <summary>
 ///     初始化
 /// </summary>
 /// <param name="value"></param>
 public BsonValueEx(BsonValue value)
 {
     if (value.IsString)
     {
         MBsonType = "BsonString";
         MBsonString = value.ToString();
     }
     if (value.IsInt32)
     {
         MBsonType = "BsonInt32";
         MBsonInt32 = value.AsInt32;
     }
     if (value.IsValidDateTime)
     {
         MBsonType = "BsonDateTime";
         MBsonDateTime = value.ToUniversalTime();
     }
     if (value.IsBoolean)
     {
         MBsonType = "BsonBoolean";
         MBsonBoolean = value.AsBoolean;
     }
     if (value.IsDouble)
     {
         MBsonType = "BsonDouble";
         MBsonDouble = value.AsDouble;
     }
 }
Ejemplo n.º 4
0
        public override void UpdateDocument(MongoCollection <BsonDocument> collection, BsonDocument document)
        {
            if (!document.Contains(ErrorRepository.FieldNames.ExceptionlessClientInfo))
            {
                return;
            }

            BsonDocument clientInfo = document.GetElement(ErrorRepository.FieldNames.ExceptionlessClientInfo).Value.AsBsonDocument;

            if (clientInfo.Contains("SubmissionMethod"))
            {
                clientInfo.ChangeName("SubmissionMethod", ErrorRepository.FieldNames.SubmissionMethod);
            }

            if (clientInfo.Contains(ErrorRepository.FieldNames.InstallDate))
            {
                BsonValue installDateValue = clientInfo.GetElement(ErrorRepository.FieldNames.InstallDate).Value;
                if (installDateValue.IsBsonArray)
                {
                    return;
                }

                DateTime installDate = installDateValue.ToUniversalTime();
                clientInfo.AsBsonDocument.Set(ErrorRepository.FieldNames.InstallDate, new BsonArray(new BsonValue[] { new BsonInt64(installDate.Ticks), new BsonInt32(-360) }));
            }

            collection.Save(document);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     初始化
        /// </summary>
        /// <param name="value"></param>
        public BsonValueEx(BsonValue value)
        {
            if (value.IsString)
            {
                mBsonType   = BasicType.BsonString;
                mBsonString = value.ToString();
            }

            if (value.IsInt32)
            {
                mBsonType  = BasicType.BsonInt32;
                mBsonInt32 = value.AsInt32;
            }
            if (value.IsInt64)
            {
                mBsonType  = BasicType.BsonInt64;
                mBsonInt64 = value.AsInt64;
            }

            if (value.IsDecimal128)
            {
                mBsonType       = BasicType.BsonDecimal128;
                mBSonDecimal128 = value.AsDecimal;
            }
            if (value.IsDouble)
            {
                mBsonType   = BasicType.BsonDouble;
                mBsonDouble = value.AsDouble;
            }

            if (value.IsValidDateTime)
            {
                mBsonType     = BasicType.BsonDateTime;
                mBsonDateTime = value.ToUniversalTime();
            }
            if (value.IsBoolean)
            {
                mBsonType    = BasicType.BsonBoolean;
                mBsonBoolean = value.AsBoolean;
            }

            if (value.IsBsonMaxKey)
            {
                mBsonType = BasicType.BsonMaxKey;
            }
            if (value.IsBsonMinKey)
            {
                mBsonType = BasicType.BsonMinKey;
            }

            if (value.IsBsonBinaryData)
            {
                mBsonType   = BasicType.BsonBinary;
                mBsonBinary = value.AsBsonBinaryData.Bytes;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Converts a certain "BsonValue" to String.
        /// </summary>
        /// <param name="bsonValue">BsonValue field</param>
        /// <returns>String value of the "BsonValue" value</returns>
        private static string BsonValueToString(BsonValue bsonValue)
        {
            if (bsonValue.IsBsonNull)            // Type : Null
            {
                return(String.Empty);
            }
            else if (bsonValue.IsBsonArray)      // Type : Array
            {
                // Reading "Inner Array"
                BsonArray bsonArray = bsonValue.AsBsonArray;

                // String "Concatenator"
                List <String> docsList = new List <String> ();

                // Iterating over elements of the array - They will all be treated as a String
                foreach (var bsonDoc in bsonArray)
                {
                    docsList.Add(bsonDoc.AsString);
                }

                return(String.Join(_configuration.ListDelimiter, docsList));
            }
            else if (bsonValue.IsObjectId)      // Type : ObjectId
            {
                return((bsonValue.AsObjectId).ToString());
            }
            else if (bsonValue.IsValidDateTime) // Type : DateTime
            {
                return(bsonValue.ToUniversalTime().ToString("yyyy-MM-dd"));
            }
            else if (bsonValue.IsDouble)        // Type : Double
            {
                return(Convert.ToString(bsonValue.AsDouble));
            }
            else if (bsonValue.IsInt32)         // Type : Int32
            {
                return(Convert.ToString(bsonValue.AsInt32));
            }
            else if (bsonValue.IsInt64)         // Type : Int64
            {
                return(Convert.ToString(bsonValue.AsInt64));
            }
            else if (bsonValue.IsNumeric)       // Type : Numeric
            {
                return(Convert.ToString(bsonValue.AsDouble));
            }
            else if (bsonValue.IsBoolean)       // Type : Boolean
            {
                return(Convert.ToString(bsonValue.AsBoolean));
            }
            else                                // Type : String
            {
                return(bsonValue.AsString);
            }
        }
Ejemplo n.º 7
0
        public void TestAsDateTime()
        {
            var       utcNow          = DateTime.UtcNow;
            var       utcNowTruncated = utcNow.AddTicks(-(utcNow.Ticks % 10000));
            BsonValue v  = utcNow;
            BsonValue s  = "";
            var       dt = v.ToUniversalTime();

            Assert.AreEqual(utcNowTruncated, dt);
#pragma warning disable 618
            Assert.Throws <InvalidCastException>(() => { var x = s.AsDateTime; });
#pragma warning restore
            Assert.Throws <NotSupportedException>(() => { var x = s.ToUniversalTime(); });
        }
Ejemplo n.º 8
0
        internal static object ToNative(this BsonValue bsonValue)
        {
            if (bsonValue.IsString)
            {
                return(bsonValue.AsString);
            }

            if (bsonValue.IsBoolean)
            {
                return(bsonValue.AsBoolean);
            }

            if (bsonValue.IsGuid)
            {
                return(bsonValue.AsGuid);
            }

            if (bsonValue.IsInt32)
            {
                return(bsonValue.AsInt32);
            }

            if (bsonValue.IsInt64)
            {
                return(bsonValue.AsInt64);
            }

            if (bsonValue.IsDouble)
            {
                return(bsonValue.AsDouble);
            }

            if (bsonValue.IsGuid)
            {
                return(bsonValue.AsGuid);
            }

            if (bsonValue.IsObjectId)
            {
                return(bsonValue.AsObjectId);
            }

            if (bsonValue.IsValidDateTime)
            {
                return(bsonValue.ToUniversalTime());
            }

            return(bsonValue);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 转换BsonDocument为C#内置类型
 /// </summary>
 /// <param name="bsonValue"></param>
 /// <returns></returns>
 public static object AsObject(this BsonValue bsonValue)
 {
     if (bsonValue.IsBsonDocument)
     {
         return(bsonValue.AsBsonDocument.ToJsonEx());
     }
     else if (bsonValue.IsBsonArray)
     {
         return(bsonValue.AsBsonArray);
     }
     else if (bsonValue.IsBoolean)
     {
         return(bsonValue.AsBoolean);
     }
     else if (bsonValue.IsValidDateTime)
     {
         return(bsonValue.ToUniversalTime());
     }
     else if (bsonValue.IsBsonNull)
     {
         return(null);
     }
     else if (bsonValue.IsObjectId)
     {
         return(bsonValue.AsObjectId);
     }
     else if (bsonValue.IsDouble)
     {
         return(bsonValue.AsDouble);
     }
     else if (bsonValue.IsInt32)
     {
         return(bsonValue.AsInt32);
     }
     else if (bsonValue.IsInt64)
     {
         return(bsonValue.AsInt64);
     }
     else if (bsonValue.IsString)
     {
         return(bsonValue.AsString);
     }
     else
     {
         return(bsonValue);
     }
 }
Ejemplo n.º 10
0
        public void SetValue(string name, BsonValue value)
        {
            object dotNetObject;

            switch(value.BsonType)
            {
                case BsonType.Int32: dotNetObject = value.AsInt32; break;
                case BsonType.Int64: dotNetObject = value.AsInt64; break;
                case BsonType.String: dotNetObject = value.AsString; break;
                case BsonType.Double: dotNetObject = value.AsDouble; break;
                case BsonType.Boolean: dotNetObject = value.AsBoolean; break;
                case BsonType.DateTime: dotNetObject = value.ToUniversalTime(); break;
                case BsonType.Null: dotNetObject = null; break;
                default: throw new NotSupportedException(string.Format("the bson type [{0}] is not supported", value.BsonType));
            }

            _values.Add(name, dotNetObject);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// get object value from BsonValue
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static object GetValue(this BsonValue value, Type totype = null)
 {
     return(value.BsonType switch
     {
         BsonType.Array => value.AsBsonArray.ToArray().Select(x => x.GetValue()),
         BsonType.Boolean => value.AsBoolean,
         BsonType.DateTime => value.ToUniversalTime(),
         BsonType.Decimal128 => value.AsDecimal,
         BsonType.Document => totype is null?value.ToJson() : BsonSerializer.Deserialize(value.ToBsonDocument(), totype),
             BsonType.Double => value.AsDouble,
             BsonType.Int32 => value.AsInt32,
             BsonType.Int64 => value.AsInt64,
             BsonType.Null => null,
             BsonType.ObjectId => value.AsString,
             BsonType.String => value.AsString,
             BsonType.Timestamp => value.AsString,
             BsonType.Undefined => null,
             _ => null
     });
Ejemplo n.º 12
0
        public static JToken ToJson(this BsonValue source)
        {
            switch (source.BsonType)
            {
            case BsonType.Document:
                return(source.AsBsonDocument.ToJson());

            case BsonType.Array:
                return(source.AsBsonArray.ToJson());

            case BsonType.Double:
                return(new JValue(source.AsDouble));

            case BsonType.String:
                return(new JValue(source.AsString));

            case BsonType.Boolean:
                return(new JValue(source.AsBoolean));

            case BsonType.DateTime:
                return(new JValue(source.ToUniversalTime()));

            case BsonType.Int32:
                return(new JValue(source.AsInt32));

            case BsonType.Int64:
                return(new JValue(source.AsInt64));

            case BsonType.Decimal128:
                return(new JValue(source.AsDecimal));

            case BsonType.Binary:
                return(new JValue(source.AsBsonBinaryData.Bytes));

            case BsonType.Null:
                return(JValue.CreateNull());

            case BsonType.Undefined:
                return(JValue.CreateUndefined());
            }

            throw new NotSupportedException($"Cannot convert {source.GetType()} to Json.");
        }
Ejemplo n.º 13
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="value"></param>
 public BsonValueEx(BsonValue value)
 {
     if (value.IsString)
     {
         mBsonType   = "BsonString";
         mBsonString = value.ToString();
     }
     if (value.IsInt32)
     {
         mBsonType  = "BsonInt32";
         mBsonInt32 = value.AsInt32;
     }
     if (value.IsValidDateTime)
     {
         mBsonType     = "BsonDateTime";
         mBsonDateTime = value.ToUniversalTime();
     }
     if (value.IsBoolean)
     {
         mBsonType    = "BsonBoolean";
         mBsonBoolean = value.AsBoolean;
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        ///     初始化
        /// </summary>
        /// <param name="value"></param>
        public BsonValueEx(BsonValue value)
        {
            if (value.IsString)
            {
                MBsonType   = "BsonString";
                MBsonString = value.ToString();
            }
            if (value.IsInt32)
            {
                MBsonType  = "BsonInt32";
                MBsonInt32 = value.AsInt32;
            }

            if (value.IsInt64)
            {
                MBsonType       = "BsonInt64";
                MBSonDecimal128 = value.AsDecimal;
            }

            if (value.IsDecimal128)
            {
                MBsonType       = "BsonDecimal128";
                MBSonDecimal128 = value.AsDecimal;
            }

            if (value.IsValidDateTime)
            {
                MBsonType     = "BsonDateTime";
                MBsonDateTime = value.ToUniversalTime();
            }
            if (value.IsBoolean)
            {
                MBsonType    = "BsonBoolean";
                MBsonBoolean = value.AsBoolean;
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Maps a BsonValue to a .NET value.
        /// </summary>
        /// <param name="bsonValue">The BsonValue.</param>
        /// <param name="options">The BsonTypeMapperOptions.</param>
        /// <returns>The mapped .NET value.</returns>
        public static object MapToDotNetValue(BsonValue bsonValue, BsonTypeMapperOptions options)
        {
            switch (bsonValue.BsonType)
            {
                case BsonType.Array:
                    var bsonArray = (BsonArray)bsonValue;
                    if (options.MapBsonArrayTo == typeof(BsonArray))
                    {
                        return bsonArray;
                    }
                    else if (options.MapBsonArrayTo == typeof(object[]))
                    {
                        var array = new object[bsonArray.Count];
                        for (int i = 0; i < bsonArray.Count; i++)
                        {
                            array[i] = MapToDotNetValue(bsonArray[i], options);
                        }
                        return array;
                    }
                    else if (typeof(IList<object>).IsAssignableFrom(options.MapBsonArrayTo))
                    {
                        var list = (IList<object>)Activator.CreateInstance(options.MapBsonArrayTo);
                        for (int i = 0; i < bsonArray.Count; i++)
                        {
                            list.Add(MapToDotNetValue(bsonArray[i], options));
                        }
                        return list;
                    }
                    else if (typeof(IList).IsAssignableFrom(options.MapBsonArrayTo))
                    {
                        var list = (IList)Activator.CreateInstance(options.MapBsonArrayTo);
                        for (int i = 0; i < bsonArray.Count; i++)
                        {
                            list.Add(MapToDotNetValue(bsonArray[i], options));
                        }
                        return list;
                    }
                    else
                    {
                        var message = string.Format("A BsonArray can't be mapped to a {0}.", BsonUtils.GetFriendlyTypeName(options.MapBsonArrayTo));
                        throw new NotSupportedException(message);
                    }
                case BsonType.Binary:
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
                    var bsonBinaryData = (BsonBinaryData)bsonValue;
                    if (bsonBinaryData.SubType == BsonBinarySubType.Binary ||
                        bsonBinaryData.SubType == BsonBinarySubType.OldBinary && options.MapOldBinaryToByteArray)
                    {
                        return bsonBinaryData.Bytes;
                    }
                    else if (bsonBinaryData.SubType == BsonBinarySubType.UuidLegacy || bsonBinaryData.SubType == BsonBinarySubType.UuidStandard)
                    {
                        return bsonBinaryData.ToGuid();
                    }
                    else
                    {
                        return bsonBinaryData; // unmapped
                    }
#pragma warning restore 618
                case BsonType.Boolean:
                    return bsonValue.AsBoolean;
                case BsonType.DateTime:
                    return bsonValue.ToUniversalTime();
                case BsonType.Document:
                    var bsonDocument = (BsonDocument)bsonValue;
                    if (options.MapBsonDocumentTo == typeof(BsonDocument))
                    {
                        return bsonDocument;
                    }
                    else if (typeof(IDictionary<string, object>).IsAssignableFrom(options.MapBsonDocumentTo))
                    {
                        var dictionary = (IDictionary<string, object>)Activator.CreateInstance(options.MapBsonDocumentTo);
                        foreach (var element in bsonDocument.Elements)
                        {
                            var mappedValue = MapToDotNetValue(element.Value, options);
                            if (dictionary.ContainsKey(element.Name))
                            {
                                switch (options.DuplicateNameHandling)
                                {
                                    case DuplicateNameHandling.Ignore:
                                        break;
                                    case DuplicateNameHandling.Overwrite:
                                    default:
                                        dictionary[element.Name] = mappedValue;
                                        break;
                                    case DuplicateNameHandling.ThrowException:
                                        var message = string.Format("Duplicate element name '{0}'.", element.Name);
                                        throw new ArgumentOutOfRangeException(message);
                                }
                            }
                            else
                            {
                                dictionary.Add(element.Name, mappedValue);
                            }
                        }
                        return dictionary;
                    }
                    else if (typeof(IDictionary).IsAssignableFrom(options.MapBsonDocumentTo))
                    {
                        var dictionary = (IDictionary)Activator.CreateInstance(options.MapBsonDocumentTo);
                        foreach (var element in bsonDocument.Elements)
                        {
                            var mappedValue = MapToDotNetValue(element.Value, options);
                            if (dictionary.Contains(element.Name))
                            {
                                switch (options.DuplicateNameHandling)
                                {
                                    case DuplicateNameHandling.Ignore:
                                        break;
                                    case DuplicateNameHandling.Overwrite:
                                    default:
                                        dictionary[element.Name] = mappedValue;
                                        break;
                                    case DuplicateNameHandling.ThrowException:
                                        var message = string.Format("Duplicate element name '{0}'.", element.Name);
                                        throw new ArgumentOutOfRangeException(message);
                                }
                            }
                            else
                            {
                                dictionary.Add(element.Name, mappedValue);
                            }
                        }
                        return dictionary;
                    }
                    else
                    {
                        var message = string.Format("A BsonDocument can't be mapped to a {0}.", BsonUtils.GetFriendlyTypeName(options.MapBsonArrayTo));
                        throw new NotSupportedException(message);
                    }
                case BsonType.Double:
                    return bsonValue.AsDouble;
                case BsonType.Int32:
                    return bsonValue.AsInt32;
                case BsonType.Int64:
                    return bsonValue.AsInt64;
                case BsonType.Null:
                    return null; // BsonValue.Null maps to C# null
                case BsonType.ObjectId:
                    return bsonValue.AsObjectId;
                case BsonType.String:
                    return bsonValue.AsString;

                case BsonType.JavaScript:
                case BsonType.JavaScriptWithScope:
                case BsonType.MaxKey:
                case BsonType.MinKey:
                case BsonType.RegularExpression:
                case BsonType.Symbol:
                case BsonType.Timestamp:
                case BsonType.Undefined:
                default:
                    return bsonValue; // unmapped
            }
        }
Ejemplo n.º 16
0
 /// <summary>
 ///     使用属性会发生一些MONO上的移植问题
 /// </summary>
 /// <returns></returns>
 public void SetValue(BsonValue value)
 {
     txtBsonValue.Visible   = false;
     txtBsonValue.Text      = string.Empty;
     txtBsonValue.ReadOnly  = false;
     radTrue.Visible        = false;
     radFalse.Visible       = false;
     radFalse.Checked       = true;
     dateTimePicker.Visible = false;
     NumberPick.Visible     = false;
     if (value.IsString)
     {
         cmbDataType.SelectedIndex = 0;
         txtBsonValue.Visible      = true;
         txtBsonValue.Text         = value.ToString();
     }
     if (value.IsInt32)
     {
         cmbDataType.SelectedIndex = 1;
         NumberPick.Visible        = true;
         NumberPick.Value          = value.AsInt32;
     }
     if (value.IsValidDateTime)
     {
         dateTimePicker.Visible    = true;
         dateTimePicker.Value      = value.ToUniversalTime();
         cmbDataType.SelectedIndex = 2;
     }
     if (value.IsBoolean)
     {
         radTrue.Visible  = true;
         radFalse.Visible = true;
         if (value.AsBoolean)
         {
             radTrue.Checked = true;
         }
         else
         {
             radFalse.Checked = true;
         }
         cmbDataType.SelectedIndex = 3;
     }
     if (value.IsBsonArray)
     {
         var t = GetArray();
         if (t != null)
         {
             _mBsonArray               = t;
             txtBsonValue.Visible      = true;
             txtBsonValue.Text         = _mBsonArray.ToString();
             txtBsonValue.ReadOnly     = true;
             cmbDataType.SelectedIndex = 4;
         }
     }
     if (value.IsBsonDocument)
     {
         var t = GetDocument();
         if (t != null)
         {
             _mBsonDocument            = t;
             txtBsonValue.Visible      = true;
             txtBsonValue.Text         = _mBsonDocument.ToString();
             txtBsonValue.ReadOnly     = true;
             cmbDataType.SelectedIndex = 5;
         }
     }
 }
Ejemplo n.º 17
0
 /// <summary>
 ///     使用属性会发生一些MONO上的移植问题
 /// </summary>
 /// <returns></returns>
 public void setValue(BsonValue value)
 {
     txtBsonValue.Visible   = false;
     txtBsonValue.Text      = String.Empty;
     txtBsonValue.ReadOnly  = false;
     radTrue.Visible        = false;
     radFalse.Visible       = false;
     radFalse.Checked       = true;
     dateTimePicker.Visible = false;
     NumberPick.Visible     = false;
     if (value.IsString)
     {
         cmbDataType.SelectedIndex = 0;
         txtBsonValue.Visible      = true;
         txtBsonValue.Text         = value.ToString();
     }
     if (value.IsInt32)
     {
         cmbDataType.SelectedIndex = 1;
         NumberPick.Visible        = true;
         NumberPick.Value          = value.AsInt32;
     }
     if (value.IsValidDateTime)
     {
         dateTimePicker.Visible    = true;
         dateTimePicker.Value      = value.ToUniversalTime();
         cmbDataType.SelectedIndex = 2;
     }
     if (value.IsBoolean)
     {
         radTrue.Visible  = true;
         radFalse.Visible = true;
         if (value.AsBoolean)
         {
             radTrue.Checked = true;
         }
         else
         {
             radFalse.Checked = true;
         }
         cmbDataType.SelectedIndex = 3;
     }
     if (value.IsBsonArray)
     {
         var frmInsertArray = new frmArrayCreator();
         SystemManager.OpenForm(frmInsertArray, false, true);
         if (frmInsertArray.mBsonArray != null)
         {
             mBsonArray                = frmInsertArray.mBsonArray;
             txtBsonValue.Visible      = true;
             txtBsonValue.Text         = mBsonArray.ToString();
             txtBsonValue.ReadOnly     = true;
             cmbDataType.SelectedIndex = 4;
         }
     }
     if (value.IsBsonDocument)
     {
         var frmInsertDoc = new frmNewDocument();
         SystemManager.OpenForm(frmInsertDoc, false, true);
         if (frmInsertDoc.mBsonDocument != null)
         {
             mBsonDocument             = frmInsertDoc.mBsonDocument;
             txtBsonValue.Visible      = true;
             txtBsonValue.Text         = mBsonDocument.ToString();
             txtBsonValue.ReadOnly     = true;
             cmbDataType.SelectedIndex = 5;
         }
     }
 }
Ejemplo n.º 18
0
        /// <summary>
        ///     使用属性会发生一些MONO上的移植问题
        /// </summary>
        /// <returns></returns>
        public void SetValue(BsonValue value, BsonValueEx.BasicType DataType = BsonValueEx.BasicType.BsonUndefined)
        {
            txtBsonValue.Visible   = false;
            txtBsonValue.Text      = string.Empty;
            txtBsonValue.ReadOnly  = false;
            radTrue.Visible        = false;
            radFalse.Visible       = false;
            radFalse.Checked       = true;
            dateTimePicker.Visible = false;
            NumberPick.Visible     = false;

            if (value.IsString)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Text    = value.ToString();
            }

            if (value.IsInt32)
            {
                NumberPick.Visible = true;
                NumberPick.Value   = value.AsInt32;
            }

            if (value.IsInt64)
            {
                NumberPick.Visible = true;
                NumberPick.Value   = value.AsInt64;
            }

            if (value.IsDecimal128)
            {
                NumberPick.Visible = true;
                NumberPick.Value   = value.AsDecimal;
            }

            if (value.IsDouble)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Text    = value.AsDouble.ToString();
            }

            if (value.IsValidDateTime)
            {
                dateTimePicker.Visible = true;
                dateTimePicker.Value   = value.ToUniversalTime();
            }

            if (value.IsBsonMaxKey || value.IsBsonMinKey)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Enabled = false;
                txtBsonValue.Text    = value.ToString();
            }

            if (value.IsBoolean)
            {
                radTrue.Visible  = true;
                radFalse.Visible = true;
                if (value.AsBoolean)
                {
                    radTrue.Checked = true;
                }
                else
                {
                    radFalse.Checked = true;
                }
            }
            if (value.IsBsonArray)
            {
                if (GetArray == null)
                {
                    MessageBox.Show("GetArray委托不存在!");
                    return;
                }
                if (DataType == BsonValueEx.BasicType.BsonLegacyPoint)
                {
                    //地理
                    var t = GetGeoPoint();
                    if (t != null)
                    {
                        _mBsonArray           = t;
                        txtBsonValue.Visible  = true;
                        txtBsonValue.Text     = _mBsonArray.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }
                else
                {
                    //普通数组
                    var t = GetArray();
                    if (t != null)
                    {
                        _mBsonArray           = t;
                        txtBsonValue.Visible  = true;
                        txtBsonValue.Text     = _mBsonArray.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }
            }
            if (value.IsBsonDocument)
            {
                if (GetDocument == null)
                {
                    MessageBox.Show("GetDocument委托不存在!");
                    return;
                }

                if (DataType == BsonValueEx.BasicType.BsonGeoJSON)
                {
                    //地理
                    var t = GetGeoPoint();
                    if (t != null)
                    {
                        _mBsonDocument = new BsonDocument("type", "Point")
                        {
                            { "coordinates", t }
                        };
                        txtBsonValue.Visible  = true;
                        txtBsonValue.Text     = _mBsonDocument.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }
                else
                {
                    var t = GetDocument();
                    if (t != null)
                    {
                        _mBsonDocument        = t;
                        txtBsonValue.Visible  = true;
                        txtBsonValue.Text     = _mBsonDocument.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }
            }
            if (value.IsBsonBinaryData)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Text    = Encoding.Default.GetString(value.AsBsonBinaryData.Bytes);
            }
        }
Ejemplo n.º 19
0
        /*
         * Parses an element's value and returns to caller in elNode
         */
        private static void ParseElementValue(
            ApplicationContext context,
            Node elNode,
            BsonValue value)
        {
            // Figuring out type of element
            switch (value.BsonType)
            {
            case BsonType.Array:
                foreach (var idxValue in value.AsBsonArray)
                {
                    var childNode = elNode.Add("").LastChild;
                    ParseElementValue(context, childNode, idxValue);
                }
                break;

            case BsonType.Binary:
                elNode.Value = value.AsByteArray;
                break;

            case BsonType.Boolean:
                elNode.Value = value.AsBoolean;
                break;

            case BsonType.DateTime:
                elNode.Value = value.ToUniversalTime();
                break;

            case BsonType.Document:
                ParseDocument(context, elNode, value.AsBsonDocument, null);
                break;

            case BsonType.Double:
                elNode.Value = value.AsDouble;
                break;

            case BsonType.Int32:
                elNode.Value = value.AsInt32;
                break;

            case BsonType.Int64:
                elNode.Value = value.AsInt64;
                break;

            case BsonType.JavaScript:
                elNode.Value = value.ToString();
                break;

            case BsonType.JavaScriptWithScope:
                elNode.Value = value.ToString();
                break;

            case BsonType.Null:
                elNode.Value = null;
                break;

            case BsonType.ObjectId:
                elNode.Value = value.ToString();
                break;

            case BsonType.RegularExpression:
                elNode.Value = value.ToString();
                break;

            case BsonType.String:
                elNode.Value = value.AsString;
                break;

            case BsonType.Symbol:
                elNode.Value = value.ToString();
                break;

            case BsonType.Timestamp:
                elNode.Value = value.ToString();
                break;

            case BsonType.Undefined:
                elNode.Value = value.ToString();
                break;
            }
        }
Ejemplo n.º 20
0
 /// <summary>
 ///     使用属性会发生一些MONO上的移植问题
 /// </summary>
 /// <returns></returns>
 public void SetValue(BsonValue value)
 {
     txtBsonValue.Visible = false;
     txtBsonValue.Text = string.Empty;
     txtBsonValue.ReadOnly = false;
     radTrue.Visible = false;
     radFalse.Visible = false;
     radFalse.Checked = true;
     dateTimePicker.Visible = false;
     NumberPick.Visible = false;
     if (value.IsString)
     {
         cmbDataType.SelectedIndex = 0;
         txtBsonValue.Visible = true;
         txtBsonValue.Text = value.ToString();
     }
     if (value.IsInt32)
     {
         cmbDataType.SelectedIndex = 1;
         NumberPick.Visible = true;
         NumberPick.Value = value.AsInt32;
     }
     if (value.IsValidDateTime)
     {
         dateTimePicker.Visible = true;
         dateTimePicker.Value = value.ToUniversalTime();
         cmbDataType.SelectedIndex = 2;
     }
     if (value.IsBoolean)
     {
         radTrue.Visible = true;
         radFalse.Visible = true;
         if (value.AsBoolean)
         {
             radTrue.Checked = true;
         }
         else
         {
             radFalse.Checked = true;
         }
         cmbDataType.SelectedIndex = 3;
     }
     if (value.IsBsonArray)
     {
         var t = GetArray();
         if (t != null)
         {
             _mBsonArray = t;
             txtBsonValue.Visible = true;
             txtBsonValue.Text = _mBsonArray.ToString();
             txtBsonValue.ReadOnly = true;
             cmbDataType.SelectedIndex = 4;
         }
     }
     if (value.IsBsonDocument)
     {
         var t = GetDocument();
         if (t != null)
         {
             _mBsonDocument = t;
             txtBsonValue.Visible = true;
             txtBsonValue.Text = _mBsonDocument.ToString();
             txtBsonValue.ReadOnly = true;
             cmbDataType.SelectedIndex = 5;
         }
     }
 }
Ejemplo n.º 21
0
        /// <summary>
        ///     使用属性会发生一些MONO上的移植问题
        /// </summary>
        /// <returns></returns>
        public void SetValue(BsonValue value, BsonValueEx.BasicType DataType = BsonValueEx.BasicType.BsonUndefined)
        {
            txtBsonValue.Visible = false;
            txtBsonValue.Text = string.Empty;
            txtBsonValue.ReadOnly = false;
            radTrue.Visible = false;
            radFalse.Visible = false;
            radFalse.Checked = true;
            dateTimePicker.Visible = false;
            NumberPick.Visible = false;

            if (value.IsString)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Text = value.ToString();
            }

            if (value.IsInt32)
            {
                NumberPick.Visible = true;
                NumberPick.Value = value.AsInt32;
            }

            if (value.IsInt64)
            {
                NumberPick.Visible = true;
                NumberPick.Value = value.AsInt64;
            }

            if (value.IsDecimal128)
            {
                NumberPick.Visible = true;
                NumberPick.Value = value.AsDecimal;
            }

            if (value.IsDouble)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Text = value.AsDouble.ToString();
            }

            if (value.IsValidDateTime)
            {
                dateTimePicker.Visible = true;
                dateTimePicker.Value = value.ToUniversalTime();
            }

            if (value.IsBsonMaxKey || value.IsBsonMinKey)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Enabled = false;
                txtBsonValue.Text = value.ToString();
            }

            if (value.IsBoolean)
            {
                radTrue.Visible = true;
                radFalse.Visible = true;
                if (value.AsBoolean)
                {
                    radTrue.Checked = true;
                }
                else
                {
                    radFalse.Checked = true;
                }
            }
            if (value.IsBsonArray)
            {
                if (GetArray == null)
                {
                    MessageBox.Show("GetArray委托不存在!");
                    return;
                }
                if (DataType == BsonValueEx.BasicType.BsonLegacyPoint)
                {
                    //地理
                    var t = GetGeoPoint();
                    if (t != null)
                    {
                        _mBsonArray = t;
                        txtBsonValue.Visible = true;
                        txtBsonValue.Text = _mBsonArray.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }
                else
                {
                    //普通数组
                    var t = GetArray();
                    if (t != null)
                    {
                        _mBsonArray = t;
                        txtBsonValue.Visible = true;
                        txtBsonValue.Text = _mBsonArray.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }

            }
            if (value.IsBsonDocument)
            {
                if (GetDocument == null)
                {
                    MessageBox.Show("GetDocument委托不存在!");
                    return;
                }

                if (DataType == BsonValueEx.BasicType.BsonGeoJSON)
                {
                    //地理
                    var t = GetGeoPoint();
                    if (t != null)
                    {
                        _mBsonDocument = new BsonDocument("type", "Point");
                        _mBsonDocument.Add("coordinates", t);
                        txtBsonValue.Visible = true;
                        txtBsonValue.Text = _mBsonDocument.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }
                else
                {
                    var t = GetDocument();
                    if (t != null)
                    {
                        _mBsonDocument = t;
                        txtBsonValue.Visible = true;
                        txtBsonValue.Text = _mBsonDocument.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }
            }
            if (value.IsBsonBinaryData)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Text = Encoding.Default.GetString(value.AsBsonBinaryData.Bytes);
            }
        }
Ejemplo n.º 22
0
        public static DateTime GetVersionDate(BsonDocument document)
        {
            BsonValue value = document[Field.WHEN];

            return(value.ToUniversalTime());
        }
        /// <summary>
        /// BsonValue转展示用字符
        /// </summary>
        /// <param name="bsonValue"></param>
        /// <returns></returns>
        public static String ConvertToString(BsonValue bsonValue)
        {
            //二进制数据
            if (bsonValue.IsBsonBinaryData)
            {
                return "[Binary]";
            }
            //空值
            if (bsonValue.IsBsonNull)
            {
                return "[Empty]";
            }
            //文档
            if (bsonValue.IsBsonDocument)
            {
                return bsonValue.ToString() + "[Contains" + bsonValue.ToBsonDocument().ElementCount + "Documents]";
            }
            //时间
            if (bsonValue.IsBsonDateTime)
            {
                DateTime bsonData = bsonValue.ToUniversalTime();
                //@flydreamer提出的本地化时间要求
                return bsonData.ToLocalTime().ToString();
            }

            //字符
            if (bsonValue.IsString)
            {
                //只有在字符的时候加上""
                return "\"" + bsonValue.ToString() + "\"";
            }

            //其他
            return bsonValue.ToString();
        }
Ejemplo n.º 24
0
 /// <summary>
 /// 使用属性会发生一些MONO上的移植问题
 /// </summary>
 /// <returns></returns>
 public void setValue(BsonValue value)
 {
     txtBsonValue.Visible = false;
     txtBsonValue.Text = String.Empty;
     txtBsonValue.ReadOnly = false;
     radTrue.Visible = false;
     radFalse.Visible = false;
     radFalse.Checked = true;
     dateTimePicker.Visible = false;
     NumberPick.Visible = false;
     if (value.IsString)
     {
         cmbDataType.SelectedIndex = 0;
         txtBsonValue.Visible = true;
         txtBsonValue.Text = value.ToString();
     }
     if (value.IsInt32)
     {
         cmbDataType.SelectedIndex = 1;
         NumberPick.Visible = true;
         NumberPick.Value = value.AsInt32;
     }
     if (value.IsValidDateTime)
     {
         dateTimePicker.Visible = true;
         dateTimePicker.Value = value.ToUniversalTime();
         cmbDataType.SelectedIndex = 2;
     }
     if (value.IsBoolean)
     {
         radTrue.Visible = true;
         radFalse.Visible = true;
         if (value.AsBoolean)
         {
             radTrue.Checked = true;
         }
         else
         {
             radFalse.Checked = true;
         }
         cmbDataType.SelectedIndex = 3;
     }
     if (value.IsBsonArray)
     {
         frmArrayCreator frmInsertArray = new frmArrayCreator();
         SystemManager.OpenForm(frmInsertArray, false, true);
         if (frmInsertArray.mBsonArray != null)
         {
             mBsonArray = frmInsertArray.mBsonArray;
             txtBsonValue.Visible = true;
             txtBsonValue.Text = mBsonArray.ToString();
             txtBsonValue.ReadOnly = true;
             cmbDataType.SelectedIndex = 4;
         }
     }
     if (value.IsBsonDocument)
     {
         frmNewDocument frmInsertDoc = new frmNewDocument();
         SystemManager.OpenForm(frmInsertDoc, false, true);
         if (frmInsertDoc.mBsonDocument != null)
         {
             mBsonDocument = frmInsertDoc.mBsonDocument;
             txtBsonValue.Visible = true;
             txtBsonValue.Text = mBsonDocument.ToString();
             txtBsonValue.ReadOnly = true;
             cmbDataType.SelectedIndex = 5;
         }
     }
 }
Ejemplo n.º 25
0
        /// <summary>
        ///     初始化
        /// </summary>
        /// <param name="value"></param>
        public BsonValueEx(BsonValue value)
        {
            if (value.IsString)
            {
                mBsonType = BasicType.BsonString;
                mBsonString = value.ToString();
            }

            if (value.IsInt32)
            {
                mBsonType = BasicType.BsonInt32;
                mBsonInt32 = value.AsInt32;
            }
            if (value.IsInt64)
            {
                mBsonType = BasicType.BsonInt64;
                mBsonInt64 = value.AsInt64;
            }

            if (value.IsDecimal128)
            {
                mBsonType = BasicType.BsonDecimal128;
                mBSonDecimal128 = value.AsDecimal;
            }
            if (value.IsDouble)
            {
                mBsonType = BasicType.BsonDouble;
                mBsonDouble = value.AsDouble;
            }

            if (value.IsValidDateTime)
            {
                mBsonType = BasicType.BsonDateTime;
                mBsonDateTime = value.ToUniversalTime();
            }
            if (value.IsBoolean)
            {
                mBsonType = BasicType.BsonBoolean;
                mBsonBoolean = value.AsBoolean;
            }

            if (value.IsBsonMaxKey)
            {
                mBsonType = BasicType.BsonMaxKey;
            }
            if (value.IsBsonMinKey)
            {
                mBsonType = BasicType.BsonMinKey;
            }

            if (value.IsBsonBinaryData)
            {
                mBsonType = BasicType.BsonBinary;
                mBsonBinary = value.AsBsonBinaryData.Bytes;
            }
        }
Ejemplo n.º 26
0
        private static DateTime GetVersionDate(BsonDocument document)
        {
            BsonValue value = document[Field.VERSIONDATE];

            return(value.ToUniversalTime());
        }
Ejemplo n.º 27
0
        AsyncReply Parse(BsonValue value)
        {
            if (value.BsonType == BsonType.Document)
            {
                var doc = value.AsBsonDocument;
                if (doc["type"] == 0)
                {
                    return(Warehouse.Get(doc["link"].AsString));
                } // structure
                else if (doc["type"] == 1)
                {
                    var bag = new AsyncBag <object>();
                    var rt  = new AsyncReply <Structure>();

                    var bs = (BsonDocument)doc["values"].AsBsonDocument;
                    var s  = new Structure();

                    foreach (var v in bs)
                    {
                        bag.Add(Parse(v.Value));
                    }

                    bag.Seal();
                    bag.Then((x) =>
                    {
                        for (var i = 0; i < x.Length; i++)
                        {
                            s[bs.GetElement(i).Name] = x[i];
                        }

                        rt.Trigger(s);
                    });

                    return(rt);
                }
                else
                {
                    return(new AsyncReply(null));
                }
            }
            else if (value.BsonType == BsonType.Array)
            {
                var array = value.AsBsonArray;
                var bag   = new AsyncBag <object>();

                foreach (var v in array)
                {
                    bag.Add(Parse(v));
                }

                bag.Seal();

                return(bag);
            }
            else if (value.BsonType == BsonType.DateTime)
            {
                return(new AsyncReply(value.ToUniversalTime()));
            }
            else
            {
                return(new AsyncReply(value.RawValue));
            }
        }
Ejemplo n.º 28
0
        /// <summary>
        ///     使用属性会发生一些MONO上的移植问题
        /// </summary>
        /// <returns></returns>
        public void SetValue(BsonValue value, BsonValueEx.BasicType DataType = BsonValueEx.BasicType.BsonUndefined)
        {
            txtBsonValue.Visible   = false;
            txtBsonValue.Text      = string.Empty;
            txtBsonValue.ReadOnly  = false;
            radTrue.Visible        = false;
            radFalse.Visible       = false;
            radFalse.Checked       = true;
            dateTimePicker.Visible = false;
            NumberPick.Visible     = false;

            if (value.IsString)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Text    = value.ToString();
            }

            if (value.IsInt32)
            {
                NumberPick.Visible = true;
                NumberPick.Value   = value.AsInt32;
            }

            if (value.IsInt64)
            {
                NumberPick.Visible = true;
                NumberPick.Value   = value.AsInt64;
            }

            if (value.IsDecimal128)
            {
                NumberPick.Visible = true;
                NumberPick.Value   = value.AsDecimal;
            }

            if (value.IsDouble)
            {
                txtBsonValue.Visible = true;
                txtBsonValue.Text    = value.AsDouble.ToString();
            }

            if (value.IsValidDateTime)
            {
                dateTimePicker.Visible = true;
                dateTimePicker.Value   = value.ToUniversalTime();
            }

            if (value.IsBoolean)
            {
                radTrue.Visible  = true;
                radFalse.Visible = true;
                if (value.AsBoolean)
                {
                    radTrue.Checked = true;
                }
                else
                {
                    radFalse.Checked = true;
                }
            }
            if (value.IsBsonArray)
            {
                if (GetArray == null)
                {
                    MessageBox.Show("GetArray委托不存在!");
                    return;
                }
                if (DataType == BsonValueEx.BasicType.BsonGeo)
                {
                    //地理
                    var t = GetGeo();
                    if (t != null)
                    {
                        _mBsonArray           = t;
                        txtBsonValue.Visible  = true;
                        txtBsonValue.Text     = _mBsonArray.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }
                else
                {
                    //普通数组
                    var t = GetArray();
                    if (t != null)
                    {
                        _mBsonArray           = t;
                        txtBsonValue.Visible  = true;
                        txtBsonValue.Text     = _mBsonArray.ToString();
                        txtBsonValue.ReadOnly = true;
                    }
                }
            }
            if (value.IsBsonDocument)
            {
                if (GetDocument == null)
                {
                    MessageBox.Show("GetDocument委托不存在!");
                    return;
                }
                var t = GetDocument();
                if (t != null)
                {
                    _mBsonDocument        = t;
                    txtBsonValue.Visible  = true;
                    txtBsonValue.Text     = _mBsonDocument.ToString();
                    txtBsonValue.ReadOnly = true;
                }
            }
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Maps a BsonValue to a .NET value.
        /// </summary>
        /// <param name="bsonValue">The BsonValue.</param>
        /// <param name="options">The BsonTypeMapperOptions.</param>
        /// <returns>The mapped .NET value.</returns>
        public static object MapToDotNetValue(BsonValue bsonValue, BsonTypeMapperOptions options)
        {
            switch (bsonValue.BsonType)
            {
            case BsonType.Array:
                var bsonArray = (BsonArray)bsonValue;
                if (options.MapBsonArrayTo == typeof(BsonArray))
                {
                    return(bsonArray);
                }
                else if (options.MapBsonArrayTo == typeof(object[]))
                {
                    var array = new object[bsonArray.Count];
                    for (int i = 0; i < bsonArray.Count; i++)
                    {
                        array[i] = MapToDotNetValue(bsonArray[i], options);
                    }
                    return(array);
                }
                else if (typeof(IList <object>).IsAssignableFrom(options.MapBsonArrayTo))
                {
                    var list = (IList <object>)Activator.CreateInstance(options.MapBsonArrayTo);
                    for (int i = 0; i < bsonArray.Count; i++)
                    {
                        list.Add(MapToDotNetValue(bsonArray[i], options));
                    }
                    return(list);
                }
                else if (typeof(IList).IsAssignableFrom(options.MapBsonArrayTo))
                {
                    var list = (IList)Activator.CreateInstance(options.MapBsonArrayTo);
                    for (int i = 0; i < bsonArray.Count; i++)
                    {
                        list.Add(MapToDotNetValue(bsonArray[i], options));
                    }
                    return(list);
                }
                else
                {
                    var message = string.Format("A BsonArray can't be mapped to a {0}.", BsonUtils.GetFriendlyTypeName(options.MapBsonArrayTo));
                    throw new NotSupportedException(message);
                }

            case BsonType.Binary:
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
                var bsonBinaryData = (BsonBinaryData)bsonValue;
                if (bsonBinaryData.SubType == BsonBinarySubType.Binary ||
                    bsonBinaryData.SubType == BsonBinarySubType.OldBinary && options.MapOldBinaryToByteArray)
                {
                    return(bsonBinaryData.Bytes);
                }
                else if (bsonBinaryData.SubType == BsonBinarySubType.UuidLegacy || bsonBinaryData.SubType == BsonBinarySubType.UuidStandard)
                {
                    return(bsonBinaryData.ToGuid());
                }
                else
                {
                    return(bsonBinaryData);    // unmapped
                }

#pragma warning restore 618
            case BsonType.Boolean:
                return(bsonValue.AsBoolean);

            case BsonType.DateTime:
                return(bsonValue.ToUniversalTime());

            case BsonType.Document:
                var bsonDocument = (BsonDocument)bsonValue;
                if (options.MapBsonDocumentTo == typeof(BsonDocument))
                {
                    return(bsonDocument);
                }
                else if (typeof(IDictionary <string, object>).IsAssignableFrom(options.MapBsonDocumentTo))
                {
                    var dictionary = (IDictionary <string, object>)Activator.CreateInstance(options.MapBsonDocumentTo);
                    foreach (var element in bsonDocument.Elements)
                    {
                        var mappedValue = MapToDotNetValue(element.Value, options);
                        if (dictionary.ContainsKey(element.Name))
                        {
                            switch (options.DuplicateNameHandling)
                            {
                            case DuplicateNameHandling.Ignore:
                                break;

                            case DuplicateNameHandling.Overwrite:
                            default:
                                dictionary[element.Name] = mappedValue;
                                break;

                            case DuplicateNameHandling.ThrowException:
                                var message = string.Format("Duplicate element name '{0}'.", element.Name);
                                throw new ArgumentOutOfRangeException("bsonValue", message);
                            }
                        }
                        else
                        {
                            dictionary.Add(element.Name, mappedValue);
                        }
                    }
                    return(dictionary);
                }
                else if (typeof(IDictionary).IsAssignableFrom(options.MapBsonDocumentTo))
                {
                    var dictionary = (IDictionary)Activator.CreateInstance(options.MapBsonDocumentTo);
                    foreach (var element in bsonDocument.Elements)
                    {
                        var mappedValue = MapToDotNetValue(element.Value, options);
                        if (dictionary.Contains(element.Name))
                        {
                            switch (options.DuplicateNameHandling)
                            {
                            case DuplicateNameHandling.Ignore:
                                break;

                            case DuplicateNameHandling.Overwrite:
                            default:
                                dictionary[element.Name] = mappedValue;
                                break;

                            case DuplicateNameHandling.ThrowException:
                                var message = string.Format("Duplicate element name '{0}'.", element.Name);
                                throw new ArgumentOutOfRangeException("bsonValue", message);
                            }
                        }
                        else
                        {
                            dictionary.Add(element.Name, mappedValue);
                        }
                    }
                    return(dictionary);
                }
                else
                {
                    var message = string.Format("A BsonDocument can't be mapped to a {0}.", BsonUtils.GetFriendlyTypeName(options.MapBsonArrayTo));
                    throw new NotSupportedException(message);
                }

            case BsonType.Double:
                return(bsonValue.AsDouble);

            case BsonType.Int32:
                return(bsonValue.AsInt32);

            case BsonType.Int64:
                return(bsonValue.AsInt64);

            case BsonType.Null:
                return(null);    // BsonValue.Null maps to C# null

            case BsonType.ObjectId:
                return(bsonValue.AsObjectId);

            case BsonType.String:
                return(bsonValue.AsString);

            case BsonType.JavaScript:
            case BsonType.JavaScriptWithScope:
            case BsonType.MaxKey:
            case BsonType.MinKey:
            case BsonType.RegularExpression:
            case BsonType.Symbol:
            case BsonType.Timestamp:
            case BsonType.Undefined:
            default:
                return(bsonValue);    // unmapped
            }
        }