示例#1
0
 /// <summary>
 /// Parses a string and creates a new BsonObjectId.
 /// </summary>
 /// <param name="s">The string value.</param>
 /// <returns>A BsonObjectId.</returns>
 public static BsonObjectId Parse(string s)
 {
     return(new BsonObjectId(ObjectId.Parse(s)));
 }
示例#2
0
        // 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.BoolToBsonBoolean: return((BsonBoolean)((bool)value));

            case Conversion.BsonJavaScriptToBsonJavaScriptWithScope: return(new BsonJavaScriptWithScope(((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(new BsonObjectId(new ObjectId((byte[])value)));

            case Conversion.ByteToBsonBoolean: return((BsonBoolean)((byte)value != 0));

            case Conversion.ByteToBsonDecimal128: return((BsonDecimal128)(Decimal128)(byte)value);

            case Conversion.ByteToBsonDouble: return((BsonDouble)(double)(byte)value);

            case Conversion.ByteToBsonInt32: return((BsonInt32)(int)(byte)value);

            case Conversion.ByteToBsonInt64: return((BsonInt64)(long)(byte)value);

            case Conversion.CharToBsonBoolean: return((BsonBoolean)((char)value != 0));

            case Conversion.CharToBsonDecimal128: return((BsonDecimal128)(Decimal128)(char)value);

            case Conversion.CharToBsonDouble: return((BsonDouble)(double)(char)value);

            case Conversion.CharToBsonInt32: return((BsonInt32)(int)(char)value);

            case Conversion.CharToBsonInt64: return((BsonInt64)(long)(char)value);

            case Conversion.DateTimeOffsetToBsonDateTime: return(new BsonDateTime(((DateTimeOffset)value).UtcDateTime));

            case Conversion.DateTimeToBsonDateTime: return(new BsonDateTime((DateTime)value));

            case Conversion.DecimalToBsonBoolean: return((BsonBoolean)((decimal)value != 0M));

            case Conversion.DecimalToBsonDecimal128: return((BsonDecimal128)(Decimal128)(decimal)value);

            case Conversion.Decimal128ToBsonBoolean: var d128 = (Decimal128)value; return((BsonBoolean)(!(Decimal128.IsNaN(d128) || d128 == Decimal128.Zero)));

            case Conversion.Decimal128ToBsonDecimal128: return((BsonDecimal128)(Decimal128)value);

            case Conversion.DoubleToBsonBoolean: var d = (double)value; return((BsonBoolean)(!(double.IsNaN(d) || d == 0.0)));

            case Conversion.DoubleToBsonDecimal128: return((BsonDecimal128)(Decimal128)(double)value);

            case Conversion.DoubleToBsonDouble: return((BsonDouble)(double)value);

            case Conversion.GuidToBsonBinary: return(new BsonBinaryData((Guid)value));

            case Conversion.Int16ToBsonBoolean: return((BsonBoolean)((short)value != 0));

            case Conversion.Int16ToBsonDecimal128: return((BsonDecimal128)(Decimal128)(short)value);

            case Conversion.Int16ToBsonDouble: return((BsonDouble)(double)(short)value);

            case Conversion.Int16ToBsonInt32: return((BsonInt32)(int)(short)value);

            case Conversion.Int16ToBsonInt64: return((BsonInt64)(long)(short)value);

            case Conversion.Int32ToBsonBoolean: return((BsonBoolean)((int)value != 0));

            case Conversion.Int32ToBsonDecimal128: return((BsonDecimal128)(Decimal128)(int)value);

            case Conversion.Int32ToBsonDouble: return((BsonDouble)(double)(int)value);

            case Conversion.Int32ToBsonInt32: return((BsonInt32)(int)value);

            case Conversion.Int32ToBsonInt64: return((BsonInt64)(long)(int)value);

            case Conversion.Int64ToBsonBoolean: return((BsonBoolean)((long)value != 0));

            case Conversion.Int64ToBsonDecimal128: return((BsonDecimal128)(Decimal128)(long)value);

            case Conversion.Int64ToBsonDouble: return((BsonDouble)(double)(long)value);

            case Conversion.Int64ToBsonTimestamp: return(new BsonTimestamp((long)value));

            case Conversion.Int64ToBsonInt64: return((BsonInt64)(long)value);

            case Conversion.ObjectIdToBsonObjectId: return(new BsonObjectId((ObjectId)value));

            case Conversion.RegexToBsonRegularExpression: return(new BsonRegularExpression((Regex)value));

            case Conversion.SByteToBsonBoolean: return((BsonBoolean)((sbyte)value != 0));

            case Conversion.SByteToBsonDecimal128: return((BsonDecimal128)(Decimal128)(sbyte)value);

            case Conversion.SByteToBsonDouble: return((BsonDouble)(double)(sbyte)value);

            case Conversion.SByteToBsonInt32: return((BsonInt32)(int)(sbyte)value);

            case Conversion.SByteToBsonInt64: return((BsonInt64)(long)(sbyte)value);

            case Conversion.SingleToBsonBoolean: var f = (float)value; return((BsonBoolean)(!(float.IsNaN(f) || f == 0.0f)));

            case Conversion.SingleToBsonDecimal128: return((BsonDecimal128)(Decimal128)(float)value);

            case Conversion.SingleToBsonDouble: return((BsonDouble)(double)(float)value);

            case Conversion.StringToBsonBoolean: return((BsonBoolean)((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.StringToBsonDecimal128: return((BsonDecimal128)JsonConvert.ToDecimal128((string)value));

            case Conversion.StringToBsonDouble: return((BsonDouble)JsonConvert.ToDouble((string)value));

            case Conversion.StringToBsonInt32: return((BsonInt32)JsonConvert.ToInt32((string)value));

            case Conversion.StringToBsonInt64: return((BsonInt64)JsonConvert.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(new BsonObjectId(ObjectId.Parse((string)value)));

            case Conversion.StringToBsonRegularExpression: return(new BsonRegularExpression((string)value));

            case Conversion.StringToBsonString: return((BsonString)(string)value);

            case Conversion.StringToBsonSymbol: return(BsonSymbolTable.Lookup((string)value));

            case Conversion.StringToBsonTimestamp: return(new BsonTimestamp(JsonConvert.ToInt64((string)value)));

            case Conversion.UInt16ToBsonBoolean: return((BsonValue)((ushort)value != 0));

            case Conversion.UInt16ToBsonDecimal128: return((BsonDecimal128)(Decimal128)(ushort)value);

            case Conversion.UInt16ToBsonDouble: return((BsonDouble)(double)(ushort)value);

            case Conversion.UInt16ToBsonInt32: return((BsonInt32)(int)(ushort)value);

            case Conversion.UInt16ToBsonInt64: return((BsonInt64)(long)(ushort)value);

            case Conversion.UInt32ToBsonBoolean: return((BsonBoolean)((uint)value != 0));

            case Conversion.UInt32ToBsonDecimal128: return((BsonDecimal128)(Decimal128)(uint)value);

            case Conversion.UInt32ToBsonDouble: return((BsonDouble)(double)(uint)value);

            case Conversion.UInt32ToBsonInt32: return((BsonInt32)(int)(uint)value);

            case Conversion.UInt32ToBsonInt64: return((BsonInt64)(long)(uint)value);

            case Conversion.UInt64ToBsonBoolean: return((BsonBoolean)((ulong)value != 0));

            case Conversion.UInt64ToBsonDecimal128: return((BsonDecimal128)(Decimal128)(ulong)value);

            case Conversion.UInt64ToBsonDouble: return((BsonDouble)(double)(ulong)value);

            case Conversion.UInt64ToBsonInt64: return((BsonInt64)(long)(ulong)value);

            case Conversion.UInt64ToBsonTimestamp: return(new BsonTimestamp((long)(ulong)value));
            }

            throw new BsonInternalException("Unexpected Conversion.");
        }