// public methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <returns>An object.</returns>
        public override double Deserialize(BsonDeserializationContext context)
        {
            var bsonReader = context.Reader;

            var bsonType = bsonReader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Double:
                return(bsonReader.ReadDouble());

            case BsonType.Int32:
                return(_converter.ToDouble(bsonReader.ReadInt32()));

            case BsonType.Int64:
                return(_converter.ToDouble(bsonReader.ReadInt64()));

            case BsonType.String:
                return(JsonConvert.ToDouble(bsonReader.ReadString()));

            default:
                throw CreateCannotDeserializeFromBsonTypeException(bsonType);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Serializes a value.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="args">The serialization args.</param>
        /// <param name="value">The object.</param>
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, decimal value)
        {
            var bsonWriter = context.Writer;

            switch (_representation)
            {
            case BsonType.Array:
                bsonWriter.WriteStartArray();
                var bits = Decimal.GetBits(value);
                bsonWriter.WriteInt32(bits[0]);
                bsonWriter.WriteInt32(bits[1]);
                bsonWriter.WriteInt32(bits[2]);
                bsonWriter.WriteInt32(bits[3]);
                bsonWriter.WriteEndArray();
                break;

            case BsonType.Decimal128:
                bsonWriter.WriteDecimal128(_converter.ToDecimal128(value));
                break;

            case BsonType.Double:
                bsonWriter.WriteDouble(_converter.ToDouble(value));
                break;

            case BsonType.Int32:
                bsonWriter.WriteInt32(_converter.ToInt32(value));
                break;

            case BsonType.Int64:
                bsonWriter.WriteInt64(_converter.ToInt64(value));
                break;

            case BsonType.String:
                bsonWriter.WriteString(JsonConvert.ToString(value));
                break;

            default:
                var message = string.Format("'{0}' is not a valid Decimal representation.", _representation);
                throw new BsonSerializationException(message);
            }
        }
Esempio n. 3
0
        public void TestConversions()
        {
            var converter = new RepresentationConverter(false, false);

            Assert.Equal((double)1.5, converter.ToDouble((double)1.5));
            Assert.Equal((double)double.MinValue, converter.ToDouble(double.MinValue));
            Assert.Equal((double)double.MaxValue, converter.ToDouble(double.MaxValue));
            Assert.Equal((double)double.NegativeInfinity, converter.ToDouble(double.NegativeInfinity));
            Assert.Equal((double)double.PositiveInfinity, converter.ToDouble(double.PositiveInfinity));
            Assert.Equal((double)double.NaN, converter.ToDouble(double.NaN));
            Assert.Equal((double)1.5, converter.ToDouble((float)1.5F));
            Assert.Equal((double)double.MinValue, converter.ToDouble(float.MinValue));
            Assert.Equal((double)double.MaxValue, converter.ToDouble(float.MaxValue));
            Assert.Equal((double)double.NegativeInfinity, converter.ToDouble(float.NegativeInfinity));
            Assert.Equal((double)double.PositiveInfinity, converter.ToDouble(float.PositiveInfinity));
            Assert.Equal((double)double.NaN, converter.ToDouble(float.NaN));
            Assert.Equal((double)1.0, converter.ToDouble((int)1));
            Assert.Equal((double)int.MaxValue, converter.ToDouble(int.MaxValue));
            Assert.Equal((double)int.MinValue, converter.ToDouble(int.MinValue));
            Assert.Equal((double)1.0, converter.ToDouble((long)1));
            Assert.Equal((double)1.0, converter.ToDouble((short)1));
            Assert.Equal((double)short.MaxValue, converter.ToDouble(short.MaxValue));
            Assert.Equal((double)short.MinValue, converter.ToDouble(short.MinValue));
            Assert.Equal((double)1.0, converter.ToDouble((uint)1));
            Assert.Equal((double)uint.MaxValue, converter.ToDouble(uint.MaxValue));
            Assert.Equal((double)uint.MinValue, converter.ToDouble(uint.MinValue));
            Assert.Equal((double)1.0, converter.ToDouble((ulong)1));
            Assert.Equal((double)ulong.MinValue, converter.ToDouble(ulong.MinValue));
            Assert.Equal((double)1.0, converter.ToDouble((ushort)1));
            Assert.Equal((double)ushort.MaxValue, converter.ToDouble(ushort.MaxValue));
            Assert.Equal((double)ushort.MinValue, converter.ToDouble(ushort.MinValue));

            Assert.Equal((short)1, converter.ToInt16((double)1.0));
            Assert.Equal((short)1, converter.ToInt16((int)1));
            Assert.Equal((short)1, converter.ToInt16((long)1));

            Assert.Equal((int)1, converter.ToInt32((double)1.0));
            Assert.Equal((int)1, converter.ToInt32((float)1.0F));
            Assert.Equal((int)1, converter.ToInt32((int)1));
            Assert.Equal((int)int.MaxValue, converter.ToInt32(int.MaxValue));
            Assert.Equal((int)int.MinValue, converter.ToInt32(int.MinValue));
            Assert.Equal((int)1, converter.ToInt32((long)1));
            Assert.Equal((int)1, converter.ToInt32((short)1));
            Assert.Equal((int)short.MaxValue, converter.ToInt32(short.MaxValue));
            Assert.Equal((int)short.MinValue, converter.ToInt32(short.MinValue));
            Assert.Equal((int)1, converter.ToInt32((uint)1));
            Assert.Equal((int)uint.MinValue, converter.ToInt32(uint.MinValue));
            Assert.Equal((int)1, converter.ToInt32((ulong)1));
            Assert.Equal((int)ulong.MinValue, converter.ToInt32(ulong.MinValue));
            Assert.Equal((int)1, converter.ToInt32((ushort)1));
            Assert.Equal((int)ushort.MaxValue, converter.ToInt32(ushort.MaxValue));
            Assert.Equal((int)ushort.MinValue, converter.ToInt32(ushort.MinValue));

            Assert.Equal((long)1, converter.ToInt64((double)1.0));
            Assert.Equal((long)1, converter.ToInt64((float)1.0F));
            Assert.Equal((long)1, converter.ToInt64((int)1));
            Assert.Equal((long)int.MaxValue, converter.ToInt64(int.MaxValue));
            Assert.Equal((long)int.MinValue, converter.ToInt64(int.MinValue));
            Assert.Equal((long)1, converter.ToInt64((long)1));
            Assert.Equal((long)long.MaxValue, converter.ToInt64(long.MaxValue));
            Assert.Equal((long)long.MinValue, converter.ToInt64(long.MinValue));
            Assert.Equal((long)1, converter.ToInt64((short)1));
            Assert.Equal((long)short.MaxValue, converter.ToInt64(short.MaxValue));
            Assert.Equal((long)short.MinValue, converter.ToInt64(short.MinValue));
            Assert.Equal((long)1, converter.ToInt64((uint)1));
            Assert.Equal((long)uint.MaxValue, converter.ToInt64(uint.MaxValue));
            Assert.Equal((long)uint.MinValue, converter.ToInt64(uint.MinValue));
            Assert.Equal((long)1, converter.ToInt64((ulong)1));
            Assert.Equal((long)ulong.MinValue, converter.ToInt64(ulong.MinValue));
            Assert.Equal((long)1, converter.ToInt64((ushort)1));
            Assert.Equal((long)ushort.MaxValue, converter.ToInt64(ushort.MaxValue));
            Assert.Equal((long)ushort.MinValue, converter.ToInt64(ushort.MinValue));

            Assert.Equal((float)1.0F, converter.ToSingle((double)1.0));
            Assert.Equal((float)float.MinValue, converter.ToSingle(double.MinValue));
            Assert.Equal((float)float.MaxValue, converter.ToSingle(double.MaxValue));
            Assert.Equal((float)float.NegativeInfinity, converter.ToSingle(double.NegativeInfinity));
            Assert.Equal((float)float.PositiveInfinity, converter.ToSingle(double.PositiveInfinity));
            Assert.Equal((float)float.NaN, converter.ToSingle(double.NaN));
            Assert.Equal((float)1.0F, converter.ToSingle((int)1));
            Assert.Equal((float)1.0F, converter.ToSingle((long)1));

            Assert.Equal((ushort)1, converter.ToUInt16((double)1.0));
            Assert.Equal((ushort)1, converter.ToUInt16((int)1));
            Assert.Equal((ushort)1, converter.ToUInt16((long)1));

            Assert.Equal((uint)1, converter.ToUInt32((double)1.0));
            Assert.Equal((uint)1, converter.ToUInt32((int)1));
            Assert.Equal((uint)1, converter.ToUInt32((long)1));

            Assert.Equal((ulong)1, converter.ToUInt64((double)1.0));
            Assert.Equal((ulong)1, converter.ToUInt64((int)1));
            Assert.Equal((long)(ulong)int.MaxValue, converter.ToInt64(int.MaxValue));
            Assert.Equal(1UL, converter.ToUInt64((long)1));
            Assert.Equal((long)(ulong)long.MaxValue, converter.ToInt64(long.MaxValue));
        }