// public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(decimal));
            var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Array:
                    var array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
                    var bits = new int[4];
                    bits[0] = array[0].AsInt32;
                    bits[1] = array[1].AsInt32;
                    bits[2] = array[2].AsInt32;
                    bits[3] = array[3].AsInt32;
                    return new decimal(bits);
                case BsonType.Double:
                    return representationSerializationOptions.ToDecimal(bsonReader.ReadDouble());
                case BsonType.Int32:
                    return representationSerializationOptions.ToDecimal(bsonReader.ReadInt32());
                case BsonType.Int64:
                    return representationSerializationOptions.ToDecimal(bsonReader.ReadInt64());
                case BsonType.String:
                    return XmlConvert.ToDecimal(bsonReader.ReadString());
                default:
                    var message = string.Format("Cannot deserialize Decimal from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(bool));

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Boolean:
                    return bsonReader.ReadBoolean();
                case BsonType.Double:
                    return bsonReader.ReadDouble() != 0.0;
                case BsonType.Int32:
                    return bsonReader.ReadInt32() != 0;
                case BsonType.Int64:
                    return bsonReader.ReadInt64() != 0;
                case BsonType.Null:
                    bsonReader.ReadNull();
                    return false;
                case BsonType.String:
                    return XmlConvert.ToBoolean(bsonReader.ReadString().ToLower());
                default:
                    var message = string.Format("Cannot deserialize Boolean from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
        public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            var versionAsDouble = bsonReader.ReadDouble();
            var version         = EventSourceVersion.FromCombined(versionAsDouble);

            return(version);
        }
Ejemplo n.º 4
0
        public override object Deserialize(BsonReader bsonReader, Type nominalType,
                                           Type actualType, IBsonSerializationOptions options)
        {
            var bsonType = bsonReader.CurrentBsonType;

            switch (bsonType)
            {
            case BsonType.Null:
                bsonReader.ReadNull();
                return(string.Empty);

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

            case BsonType.Int32:
                return(bsonReader.ReadInt32().ToString(CultureInfo.InvariantCulture));

            case BsonType.Boolean:
                return(bsonReader.ReadBoolean().ToString(CultureInfo.InvariantCulture));

            case BsonType.Double:
                return(bsonReader.ReadDouble().ToString());

            default:
                var message = string.Format("Cannot deserialize BsonString,BsonBoolean or BsonInt32 from BsonType {0}.", bsonType);
                throw new BsonSerializationException(message);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType, // ignored
            IBsonSerializationOptions options
            )
        {
            VerifyDeserializeType(nominalType);
            var bsonType = bsonReader.CurrentBsonType;

            switch (bsonType)
            {
            case BsonType.Int32: return(Enum.ToObject(nominalType, bsonReader.ReadInt32()));

            case BsonType.Int64: return(Enum.ToObject(nominalType, bsonReader.ReadInt64()));

            case BsonType.Double: return(Enum.ToObject(nominalType, (long)bsonReader.ReadDouble()));

            case BsonType.String: return(Enum.Parse(nominalType, bsonReader.ReadString()));

            default:
                var message = string.Format("Cannot deserialize {0} from BsonType {1}.", nominalType.FullName, bsonType);
                throw new FileFormatException(message);
            }
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(double));
            var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Double:
                    return bsonReader.ReadDouble();
                case BsonType.Int32:
                    return representationSerializationOptions.ToDouble(bsonReader.ReadInt32());
                case BsonType.Int64:
                    return representationSerializationOptions.ToDouble(bsonReader.ReadInt64());
                case BsonType.String:
                    return XmlConvert.ToDouble(bsonReader.ReadString());
                default:
                    var message = string.Format("Cannot deserialize Double from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
Ejemplo n.º 7
0
        static object ReadObject(BsonReader bsonReader)         //_120509_173140 keep consistent
        {
            switch (bsonReader.GetCurrentBsonType())
            {
            case BsonType.Array: return(ReadArray(bsonReader));                                                                                               // replacement

            case BsonType.Binary: var binary = BsonSerializer.Deserialize <BsonValue>(bsonReader); return(BsonTypeMapper.MapToDotNetValue(binary) ?? binary); // byte[] or Guid else self

            case BsonType.Boolean: return(bsonReader.ReadBoolean());

            case BsonType.DateTime: return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()));

            case BsonType.Document: return(ReadCustomObject(bsonReader));                    // replacement

            case BsonType.Double: return(bsonReader.ReadDouble());

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

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

            case BsonType.Null: bsonReader.ReadNull(); return(null);

            case BsonType.ObjectId: return(bsonReader.ReadObjectId());

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

            default: return(BsonSerializer.Deserialize <BsonValue>(bsonReader));
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            IBsonSerializationOptions options
            )
        {
            var bsonType = bsonReader.CurrentBsonType;

            switch (bsonType)
            {
            case BsonType.Boolean:
                return(bsonReader.ReadBoolean());

            case BsonType.Double:
                return(bsonReader.ReadDouble() != 0.0);

            case BsonType.Int32:
                return(bsonReader.ReadInt32() != 0);

            case BsonType.Int64:
                return(bsonReader.ReadInt64() != 0);

            case BsonType.Null:
                bsonReader.ReadNull();
                return(false);

            case BsonType.String:
                return(XmlConvert.ToBoolean(bsonReader.ReadString().ToLower()));

            default:
                var message = string.Format("Cannot deserialize Boolean from BsonType: {0}", bsonType);
                throw new FileFormatException(message);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            IBsonSerializationOptions options
            )
        {
            var representationOptions = (RepresentationSerializationOptions)options ?? defaultRepresentationOptions;
            var bsonType = bsonReader.CurrentBsonType;

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

            case BsonType.Int32:
                return(representationOptions.ToInt64(bsonReader.ReadInt32()));

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

            case BsonType.String:
                return(XmlConvert.ToInt64(bsonReader.ReadString()));

            default:
                var message = string.Format("Cannot deserialize Int64 from BsonType: {0}", bsonType);
                throw new FileFormatException(message);
            }
        }
Ejemplo n.º 10
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(ushort));
            var representationSerializationOptions = EnsureSerializationOptions <RepresentationSerializationOptions>(options);

            var bsonType = bsonReader.GetCurrentBsonType();

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

            case BsonType.Int32:
                return(representationSerializationOptions.ToUInt16(bsonReader.ReadInt32()));

            case BsonType.Int64:
                return(representationSerializationOptions.ToUInt16(bsonReader.ReadInt64()));

            case BsonType.String:
                return(XmlConvert.ToUInt16(bsonReader.ReadString()));

            default:
                var message = string.Format("Cannot deserialize uInt16 from BsonType {0}.", bsonType);
                throw new Exception(message);
            }
        }
Ejemplo n.º 11
0
        protected override decimal readAmount(BsonReader reader)
        {
            var    amountMap = _map.GetMemberMap(m => m.Amount);
            double amount    = reader.ReadDouble(amountMap.ElementName);

            return(Convert.ToDecimal(amount));
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>
        /// An object.
        /// </returns>
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            if (bsonReader.GetCurrentBsonType() == BsonType.Null)
            {
                bsonReader.ReadNull();
                return(null);
            }
            else
            {
                bsonReader.ReadStartArray();
                var longitude = bsonReader.ReadDouble();
                var latitude  = bsonReader.ReadDouble();
                bsonReader.ReadEndArray();

                return(new GeoJson2DGeographicCoordinates(longitude, latitude));
            }
        }
Ejemplo n.º 13
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>
        /// An object.
        /// </returns>
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            if (bsonReader.GetCurrentBsonType() == BsonType.Null)
            {
                bsonReader.ReadNull();
                return(null);
            }
            else
            {
                bsonReader.ReadStartArray();
                var easting  = bsonReader.ReadDouble();
                var northing = bsonReader.ReadDouble();
                bsonReader.ReadEndArray();

                return(new GeoJson2DProjectedCoordinates(easting, northing));
            }
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>
        /// An object.
        /// </returns>
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            if (bsonReader.GetCurrentBsonType() == BsonType.Null)
            {
                bsonReader.ReadNull();
                return(null);
            }
            else
            {
                bsonReader.ReadStartArray();
                var x = bsonReader.ReadDouble();
                var y = bsonReader.ReadDouble();
                var z = bsonReader.ReadDouble();
                bsonReader.ReadEndArray();

                return(new GeoJson3DCoordinates(x, y, z));
            }
        }
Ejemplo n.º 15
0
        public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            object value     = null;
            var    valueType = actualType.GetConceptValueType();

            if (valueType == typeof(Guid))
            {
                var binaryData = bsonReader.ReadBinaryData();
                value = binaryData.ToGuid();
            }
            else if (valueType == typeof(double))
            {
                value = bsonReader.ReadDouble();
            }
            else if (valueType == typeof(float))
            {
                value = (float)bsonReader.ReadDouble();
            }
            else if (valueType == typeof(Int32))
            {
                value = bsonReader.ReadInt32();
            }
            else if (valueType == typeof(Int64))
            {
                value = bsonReader.ReadInt64();
            }
            else if (valueType == typeof(bool))
            {
                value = bsonReader.ReadBoolean();
            }
            else if (valueType == typeof(string))
            {
                value = bsonReader.ReadString();
            }
            else if (valueType == typeof(decimal))
            {
                value = decimal.Parse(bsonReader.ReadString());
            }

            var concept = ConceptFactory.CreateConceptInstance(actualType, value);

            return(concept);
        }
Ejemplo n.º 16
0
        public void TestDouble()
        {
            var json = "1.5";

            using (bsonReader = BsonReader.Create(json)) {
                Assert.AreEqual(BsonType.Double, bsonReader.ReadBsonType());
                Assert.AreEqual(1.5, bsonReader.ReadDouble());
                Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
            }
            Assert.AreEqual(json, BsonSerializer.Deserialize <double>(new StringReader(json)).ToJson());
        }
Ejemplo n.º 17
0
    public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
    {
        EvaluationResult er = new EvaluationResult();

        bsonReader.ReadStartDocument();
        er.Id = bsonReader.ReadObjectId();
        er.DurationInSeconds = bsonReader.ReadDouble();
        er.startTime         = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime());
        er.endTime           = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime());
        er.Result            = EvaluationStatus.Parse(bsonReader.ReadString());
        er.JSON = bsonReader.ReadString();
        bsonReader.ReadEndDocument();
        return(er);
    }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(BsonDouble));

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Double:
                    return new BsonDouble(bsonReader.ReadDouble());
                default:
                    var message = string.Format("Cannot deserialize BsonDouble from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
Ejemplo n.º 19
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(BsonDouble));

            var bsonType = bsonReader.GetCurrentBsonType();

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

            default:
                var message = string.Format("Cannot deserialize BsonDouble from BsonType {0}.", bsonType);
                throw new Exception(message);
            }
        }
        public void TestBsonAwesome()
        {
            string byteString = @"1\x00\x00\x00\x04BSON\x00&\x00\x00\x00\x020\x00\x08\x00\x00\x00awesome\x00\x011\x00333333\x14@\x102\x00\xc2\x07\x00\x00\x00\x00";

            byte[]       bytes  = DecodeByteString(byteString);
            MemoryStream stream = new MemoryStream(bytes);

            using (BsonReader bsonReader = BsonReader.Create(stream)) {
                bsonReader.ReadStartDocument();
                Assert.AreEqual(BsonType.Array, bsonReader.ReadBsonType());
                Assert.AreEqual("BSON", bsonReader.ReadName());
                bsonReader.ReadStartArray();
                Assert.AreEqual(BsonType.String, bsonReader.ReadBsonType());
                Assert.AreEqual("awesome", bsonReader.ReadString());
                Assert.AreEqual(BsonType.Double, bsonReader.ReadBsonType());
                Assert.AreEqual(5.05, bsonReader.ReadDouble());
                Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
                Assert.AreEqual(1986, bsonReader.ReadInt32());
                bsonReader.ReadEndArray();
                bsonReader.ReadEndDocument();
            }
        }
Ejemplo n.º 21
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(decimal));
            var representationSerializationOptions = EnsureSerializationOptions <RepresentationSerializationOptions>(options);

            var bsonType = bsonReader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Array:
                var array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
                var bits  = new int[4];
                bits[0] = array[0].AsInt32;
                bits[1] = array[1].AsInt32;
                bits[2] = array[2].AsInt32;
                bits[3] = array[3].AsInt32;
                return(new decimal(bits));

            case BsonType.Double:
                return(representationSerializationOptions.ToDecimal(bsonReader.ReadDouble()));

            case BsonType.Int32:
                return(representationSerializationOptions.ToDecimal(bsonReader.ReadInt32()));

            case BsonType.Int64:
                return(representationSerializationOptions.ToDecimal(bsonReader.ReadInt64()));

            case BsonType.String:
                return(XmlConvert.ToDecimal(bsonReader.ReadString()));

            default:
                var message = string.Format("Cannot deserialize Decimal from BsonType {0}.", bsonType);
                throw new Exception(message);
            }
        }
Ejemplo n.º 22
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(TimeSpan));

            // support RepresentationSerializationOptions for backward compatibility
            var representationSerializationOptions = options as RepresentationSerializationOptions;

            if (representationSerializationOptions != null)
            {
                options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation);
            }
            var timeSpanSerializationOptions = EnsureSerializationOptions <TimeSpanSerializationOptions>(options);

            BsonType bsonType = bsonReader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Double:
                return(FromDouble(bsonReader.ReadDouble(), timeSpanSerializationOptions.Units));

            case BsonType.Int32:
                return(FromInt32(bsonReader.ReadInt32(), timeSpanSerializationOptions.Units));

            case BsonType.Int64:
                return(FromInt64(bsonReader.ReadInt64(), timeSpanSerializationOptions.Units));

            case BsonType.String:
                return(TimeSpan.Parse(bsonReader.ReadString()));    // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan)

            default:
                var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType);
                throw new Exception(message);
            }
        }
Ejemplo n.º 23
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, // ignored
            IBsonSerializationOptions options)
        {
            VerifyDeserializeType(nominalType);

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Int32: return Enum.ToObject(nominalType, bsonReader.ReadInt32());
                case BsonType.Int64: return Enum.ToObject(nominalType, bsonReader.ReadInt64());
                case BsonType.Double: return Enum.ToObject(nominalType, (long)bsonReader.ReadDouble());
                case BsonType.String: return Enum.Parse(nominalType, bsonReader.ReadString());
                default:
                    var message = string.Format("Cannot deserialize {0} from BsonType {1}.", nominalType.FullName, bsonType);
                    throw new FileFormatException(message);
            }
        }
Ejemplo n.º 24
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(TimeSpan));

            // support RepresentationSerializationOptions for backward compatibility
            var representationSerializationOptions = options as RepresentationSerializationOptions;
            if (representationSerializationOptions != null)
            {
                options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation);
            }
            var timeSpanSerializationOptions = EnsureSerializationOptions<TimeSpanSerializationOptions>(options);

            BsonType bsonType = bsonReader.GetCurrentBsonType();
            if (bsonType == BsonType.String)
            {
                return TimeSpan.Parse(bsonReader.ReadString()); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan)
            }
            else if (timeSpanSerializationOptions.Units == TimeSpanUnits.Ticks)
            {
                long ticks;
                switch (bsonType)
                {
                    case BsonType.Double: ticks = (long)bsonReader.ReadDouble(); break;
                    case BsonType.Int32: ticks = (long)bsonReader.ReadInt32(); break;
                    case BsonType.Int64: ticks = bsonReader.ReadInt64(); break;
                    default:
                        var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType);
                        throw new FileFormatException(message);
                }
                return new TimeSpan(ticks);
            }
            else
            {
                double interval;
                switch (bsonType)
                {
                    case BsonType.Double: interval = bsonReader.ReadDouble(); break;
                    case BsonType.Int32: interval = bsonReader.ReadInt32(); break;
                    case BsonType.Int64: interval = bsonReader.ReadInt64(); break;
                    default:
                        var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType);
                        throw new FileFormatException(message);
                }

                switch (timeSpanSerializationOptions.Units)
                {
                    case TimeSpanUnits.Days: return TimeSpan.FromDays(interval);
                    case TimeSpanUnits.Hours: return TimeSpan.FromHours(interval);
                    case TimeSpanUnits.Minutes: return TimeSpan.FromMinutes(interval);
                    case TimeSpanUnits.Seconds: return TimeSpan.FromSeconds(interval);
                    case TimeSpanUnits.Milliseconds: return TimeSpan.FromMilliseconds(interval);
                    case TimeSpanUnits.Nanoseconds: return TimeSpan.FromMilliseconds(interval / 1000.0);
                    default:
                        var message = string.Format("'{0}' is not a valid TimeSpanUnits value.", timeSpanSerializationOptions.Units);
                        throw new BsonSerializationException(message);
                }
            }
        }
Ejemplo n.º 25
0
        private void ReadValue()
        {
            //bool value = true;
            _type = PBBsonReaderType.Value;
            switch (_bsonType)
            {
            case BsonType.Document:
                WriteLine(2, "ReadStartDocument");
                WriteLine(1, "{");
                //_indent += 2;
                Indent(2);
                _reader.ReadStartDocument();
                _type = PBBsonReaderType.Document;
                //value = false;
                break;

            case BsonType.Array:
                WriteLine(2, "ReadStartArray");
                WriteLine(1, "[");
                //_indent += 2;
                Indent(2);
                _reader.ReadStartArray();
                _type = PBBsonReaderType.Array;
                //value = false;
                break;

            case BsonType.Binary:
                _value = BsonValue.Create(_reader.ReadBytes());
                break;

            case BsonType.Boolean:
                _value = BsonValue.Create(_reader.ReadBoolean());
                break;

            case BsonType.DateTime:
                _value = BsonValue.Create(_reader.ReadDateTime());
                break;

            case BsonType.Double:
                _value = BsonValue.Create(_reader.ReadDouble());
                break;

            case BsonType.Int32:
                _value = BsonValue.Create(_reader.ReadInt32());
                break;

            case BsonType.Int64:
                _value = BsonValue.Create(_reader.ReadInt64());
                break;

            case BsonType.JavaScript:
                _value = BsonValue.Create(_reader.ReadJavaScript());
                break;

            case BsonType.JavaScriptWithScope:
                _value = BsonValue.Create(_reader.ReadJavaScriptWithScope());
                break;

            case BsonType.MaxKey:
                _reader.ReadMaxKey();
                _value = BsonMaxKey.Value;
                break;

            case BsonType.MinKey:
                _reader.ReadMinKey();
                _value = BsonMinKey.Value;
                break;

            case BsonType.Null:
                _reader.ReadNull();
                _value = BsonNull.Value;
                break;

            case BsonType.ObjectId:
                _value = BsonValue.Create(_reader.ReadObjectId());
                break;

            case BsonType.RegularExpression:
                _value = BsonValue.Create(_reader.ReadRegularExpression());
                break;

            case BsonType.String:
                _value = BsonValue.Create(_reader.ReadString());
                break;

            case BsonType.Symbol:
                _value = BsonValue.Create(_reader.ReadSymbol());
                break;

            case BsonType.Timestamp:
                _value = BsonValue.Create(_reader.ReadTimestamp());
                break;

            case BsonType.Undefined:
                _reader.ReadUndefined();
                _value = BsonUndefined.Value;
                break;

            default:
                throw new PBException("error unable to read value type {0}", _bsonType);
            }
            //return value;
        }
Ejemplo n.º 26
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(TimeSpan));

            // support RepresentationSerializationOptions for backward compatibility
            var representationSerializationOptions = options as RepresentationSerializationOptions;

            if (representationSerializationOptions != null)
            {
                options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation);
            }
            var timeSpanSerializationOptions = EnsureSerializationOptions <TimeSpanSerializationOptions>(options);

            BsonType bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.String)
            {
                return(TimeSpan.Parse(bsonReader.ReadString())); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan)
            }
            else if (timeSpanSerializationOptions.Units == TimeSpanUnits.Ticks)
            {
                long ticks;
                switch (bsonType)
                {
                case BsonType.Double: ticks = (long)bsonReader.ReadDouble(); break;

                case BsonType.Int32: ticks = (long)bsonReader.ReadInt32(); break;

                case BsonType.Int64: ticks = bsonReader.ReadInt64(); break;

                default:
                    var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
                }
                return(new TimeSpan(ticks));
            }
            else
            {
                double interval;
                switch (bsonType)
                {
                case BsonType.Double: interval = bsonReader.ReadDouble(); break;

                case BsonType.Int32: interval = bsonReader.ReadInt32(); break;

                case BsonType.Int64: interval = bsonReader.ReadInt64(); break;

                default:
                    var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
                }

                switch (timeSpanSerializationOptions.Units)
                {
                case TimeSpanUnits.Days: return(TimeSpan.FromDays(interval));

                case TimeSpanUnits.Hours: return(TimeSpan.FromHours(interval));

                case TimeSpanUnits.Minutes: return(TimeSpan.FromMinutes(interval));

                case TimeSpanUnits.Seconds: return(TimeSpan.FromSeconds(interval));

                case TimeSpanUnits.Milliseconds: return(TimeSpan.FromMilliseconds(interval));

                case TimeSpanUnits.Nanoseconds: return(TimeSpan.FromMilliseconds(interval / 1000.0));

                default:
                    var message = string.Format("'{0}' is not a valid TimeSpanUnits value.", timeSpanSerializationOptions.Units);
                    throw new BsonSerializationException(message);
                }
            }
        }