/// <summary> /// Adds multiple elements to the array. /// </summary> /// <param name="values">A list of values to add to the array.</param> /// <returns>The array (so method calls can be chained).</returns> public BsonArray AddRange(IEnumerable <bool> values) { if (values != null) { foreach (var value in values) { this.values.Add(BsonBoolean.Create(value)); } } return(this); }
// private static methods private static BsonValue Convert(object value, Conversion conversion) { // note: the ToBoolean conversions use the JavaScript definition of truthiness switch (conversion) { // note: I expect this switch statement to be compiled using a jump table and therefore to be very efficient case Conversion.None: return((BsonValue)value); case Conversion.BsonJavaScriptToBsonJavaScriptWithScope: return(BsonJavaScriptWithScope.Create(((BsonJavaScript)value).Code, new BsonDocument())); case Conversion.BsonMaxKeyToBsonBoolean: return(BsonBoolean.True); case Conversion.BsonMinKeyToBsonBoolean: return(BsonBoolean.True); case Conversion.BsonNullToBsonBoolean: return(BsonBoolean.False); case Conversion.BsonUndefinedToBsonBoolean: return(BsonBoolean.False); case Conversion.ByteArrayToBsonBinary: return(new BsonBinaryData((byte[])value)); case Conversion.ByteArrayToBsonObjectId: return(BsonObjectId.Create((byte[])value)); case Conversion.ByteToBsonBoolean: return(BsonBoolean.Create((byte)value != 0)); case Conversion.ByteToBsonDouble: return(new BsonDouble((double)(byte)value)); case Conversion.ByteToBsonInt32: return(BsonInt32.Create((int)(byte)value)); case Conversion.ByteToBsonInt64: return(new BsonInt64((long)(byte)value)); case Conversion.CharToBsonBoolean: return(BsonBoolean.Create((char)value != 0)); case Conversion.CharToBsonDouble: return(new BsonDouble((double)(char)value)); case Conversion.CharToBsonInt32: return(BsonInt32.Create((int)(char)value)); case Conversion.CharToBsonInt64: return(new BsonInt64((long)(char)value)); case Conversion.DateTimeOffsetToBsonDateTime: return(new BsonDateTime(((DateTimeOffset)value).UtcDateTime)); case Conversion.DateTimeToBsonDateTime: return(new BsonDateTime((DateTime)value)); case Conversion.DoubleToBsonBoolean: var d = (double)value; return(BsonBoolean.Create(!(double.IsNaN(d) || d == 0.0))); case Conversion.GuidToBsonBinary: return(new BsonBinaryData((Guid)value)); case Conversion.Int16ToBsonBoolean: return(BsonBoolean.Create((short)value != 0)); case Conversion.Int16ToBsonDouble: return(new BsonDouble((double)(short)value)); case Conversion.Int16ToBsonInt32: return(BsonInt32.Create((int)(short)value)); case Conversion.Int16ToBsonInt64: return(new BsonInt64((long)(short)value)); case Conversion.Int32ToBsonBoolean: return(BsonBoolean.Create((int)value != 0)); case Conversion.Int32ToBsonDouble: return(new BsonDouble((double)(int)value)); case Conversion.Int32ToBsonInt64: return(new BsonInt64((long)(int)value)); case Conversion.Int64ToBsonBoolean: return(BsonBoolean.Create((long)value != 0)); case Conversion.Int64ToBsonDouble: return(new BsonDouble((double)(long)value)); case Conversion.Int64ToBsonTimestamp: return(new BsonTimestamp((long)value)); case Conversion.NewBsonBoolean: return(BsonBoolean.Create((bool)value)); case Conversion.NewBsonDouble: return(new BsonDouble((double)value)); case Conversion.NewBsonInt32: return(BsonInt32.Create((int)value)); case Conversion.NewBsonInt64: return(new BsonInt64((long)value)); case Conversion.NewBsonObjectId: return(new BsonObjectId((ObjectId)value)); case Conversion.NewBsonString: return(new BsonString((string)value)); case Conversion.RegexToBsonRegularExpression: return(new BsonRegularExpression((Regex)value)); case Conversion.SByteToBsonBoolean: return(BsonBoolean.Create((sbyte)value != 0)); case Conversion.SByteToBsonDouble: return(new BsonDouble((double)(sbyte)value)); case Conversion.SByteToBsonInt32: return(BsonInt32.Create((int)(sbyte)value)); case Conversion.SByteToBsonInt64: return(new BsonInt64((long)(sbyte)value)); case Conversion.SingleToBsonBoolean: var f = (float)value; return(BsonBoolean.Create(!(float.IsNaN(f) || f == 0.0f))); case Conversion.SingleToBsonDouble: return(new BsonDouble((double)(float)value)); case Conversion.StringToBsonBoolean: return(BsonBoolean.Create((string)value != "")); case Conversion.StringToBsonDateTime: var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" }; var dt = DateTime.ParseExact((string)value, formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); return(new BsonDateTime(dt)); case Conversion.StringToBsonDouble: return(new BsonDouble(XmlConvert.ToDouble((string)value))); case Conversion.StringToBsonInt32: return(BsonInt32.Create(XmlConvert.ToInt32((string)value))); case Conversion.StringToBsonInt64: return(new BsonInt64(XmlConvert.ToInt64((string)value))); case Conversion.StringToBsonJavaScript: return(new BsonJavaScript((string)value)); case Conversion.StringToBsonJavaScriptWithScope: return(new BsonJavaScriptWithScope((string)value, new BsonDocument())); case Conversion.StringToBsonObjectId: return(BsonObjectId.Create((string)value)); case Conversion.StringToBsonRegularExpression: return(new BsonRegularExpression((string)value)); case Conversion.StringToBsonSymbol: return(BsonSymbol.Create((string)value)); case Conversion.StringToBsonTimestamp: return(new BsonTimestamp(XmlConvert.ToInt64((string)value))); case Conversion.UInt16ToBsonBoolean: return(BsonBoolean.Create((ushort)value != 0)); case Conversion.UInt16ToBsonDouble: return(new BsonDouble((double)(ushort)value)); case Conversion.UInt16ToBsonInt32: return(BsonInt32.Create((int)(ushort)value)); case Conversion.UInt16ToBsonInt64: return(new BsonInt64((long)(ushort)value)); case Conversion.UInt32ToBsonBoolean: return(BsonBoolean.Create((uint)value != 0)); case Conversion.UInt32ToBsonDouble: return(new BsonDouble((double)(uint)value)); case Conversion.UInt32ToBsonInt32: return(BsonInt32.Create((int)(uint)value)); case Conversion.UInt32ToBsonInt64: return(new BsonInt64((long)(uint)value)); case Conversion.UInt64ToBsonBoolean: return(BsonBoolean.Create((ulong)value != 0)); case Conversion.UInt64ToBsonDouble: return(new BsonDouble((double)(ulong)value)); case Conversion.UInt64ToBsonInt64: return(new BsonInt64((long)(ulong)value)); case Conversion.UInt64ToBsonTimestamp: return(new BsonTimestamp((long)(ulong)value)); } throw new BsonInternalException("Unexpected Conversion."); }
public static dynamic ToDynamic(this BsonBoolean bsonBoolean) => bsonBoolean.Value;