Exemplo n.º 1
0
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(DateTimeOffset));

            BsonType bsonType = bsonReader.GetCurrentBsonType();
            long     ticks;
            TimeSpan offset;

            switch (bsonType)
            {
            case BsonType.Array:
                bsonReader.ReadStartArray();
                ticks  = bsonReader.ReadInt64();
                offset = TimeSpan.FromMinutes(bsonReader.ReadInt32());
                bsonReader.ReadEndArray();
                return(new DateTimeOffset(ticks, offset).Add(offset));

            case BsonType.Document:
                bsonReader.ReadStartDocument();
                bsonReader.ReadDateTime("DateTime");
                ticks  = bsonReader.ReadInt64("Ticks");
                offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset"));
                bsonReader.ReadEndDocument();
                return(new DateTimeOffset(ticks, offset).Add(offset));

            default:
                var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType);
                throw new ArgumentException(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(DateTimeOffset));

            BsonType bsonType = bsonReader.GetCurrentBsonType();
            long ticks;
            TimeSpan offset;
            switch (bsonType)
            {
                case BsonType.Array:
                    bsonReader.ReadStartArray();
                    ticks = bsonReader.ReadInt64();
                    offset = TimeSpan.FromMinutes(bsonReader.ReadInt32());
                    bsonReader.ReadEndArray();
                    return new DateTimeOffset(ticks, offset);
                case BsonType.Document:
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadDateTime("DateTime"); // ignore value
                    ticks = bsonReader.ReadInt64("Ticks");
                    offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset"));
                    bsonReader.ReadEndDocument();
                    return new DateTimeOffset(ticks, offset);
                case BsonType.String:
                    return XmlConvert.ToDateTimeOffset(bsonReader.ReadString());
                default:
                    var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
Exemplo n.º 3
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(DateTimeOffset));
            var dateTimeSerializationOptions = EnsureSerializationOptions <DateTimeSerializationOptions>(options);

            var            bsonType = bsonReader.GetCurrentBsonType();
            DateTimeOffset value;

            switch (bsonType)
            {
            case BsonType.DateTime:
                // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly
                value = BsonDateTime.Create(bsonReader.ReadDateTime()).Value;
                break;

            case BsonType.Document:
                bsonReader.ReadStartDocument();
                bsonReader.ReadDateTime("DateTimeUTC");     // ignore value (use Ticks instead)
                value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc);
                bsonReader.ReadEndDocument();
                break;

            case BsonType.Int64:
                value = new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc);
                break;

            case BsonType.String:
                // note: we're not using XmlConvert because of bugs in Mono
                if (dateTimeSerializationOptions.DateOnly)
                {
                    value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
                }
                else
                {
                    var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" };
                    value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
                }
                break;

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

            return(value);
        }
Exemplo n.º 4
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));
            }
        }
        // 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(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);
            }
        }
Exemplo n.º 7
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);
            }
        }
Exemplo 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);
            }
        }
        // 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);
            }
        }
Exemplo n.º 10
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);
            }
        }
Exemplo n.º 11
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);
            }
        }
Exemplo n.º 12
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(byte));

            var  bsonType = bsonReader.GetCurrentBsonType();
            byte value;
            var  lostData = false;

            switch (bsonType)
            {
            case BsonType.Binary:
                byte[]            bytes;
                BsonBinarySubType subType;
                bsonReader.ReadBinaryData(out bytes, out subType);
                if (bytes.Length != 1)
                {
                    throw new FileFormatException("Binary data for Byte must be exactly one byte long.");
                }
                value = bytes[0];
                break;

            case BsonType.Int32:
                var int32Value = bsonReader.ReadInt32();
                value    = (byte)int32Value;
                lostData = (int)value != int32Value;
                break;

            case BsonType.Int64:
                var int64Value = bsonReader.ReadInt64();
                value    = (byte)int64Value;
                lostData = (int)value != int64Value;
                break;

            case BsonType.String:
                var s = bsonReader.ReadString();
                if (s.Length == 1)
                {
                    s = "0" + s;
                }
                value = byte.Parse(s, NumberStyles.HexNumber);
                break;

            default:
                var message = string.Format("Cannot deserialize Byte from BsonType {0}.", bsonType);
                throw new FileFormatException(message);
            }
            if (lostData)
            {
                var message = string.Format("Data loss occurred when trying to convert from {0} to Byte.", bsonType);
                throw new FileFormatException(message);
            }

            return(value);
        }
Exemplo n.º 13
0
        public void TestInt64NumberLong()
        {
            var json = "NumberLong(123)";

            using (bsonReader = BsonReader.Create(json)) {
                Assert.AreEqual(BsonType.Int64, bsonReader.ReadBsonType());
                Assert.AreEqual(123, bsonReader.ReadInt64());
                Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
            }
            Assert.AreEqual(json, BsonSerializer.Deserialize <long>(new StringReader(json)).ToJson());
        }
Exemplo n.º 14
0
        public void TestInt64ConstructorUnqutoed()
        {
            var json = "NumberLong(123)";

            using (_bsonReader = new JsonReader(json))
            {
                Assert.AreEqual(BsonType.Int64, _bsonReader.ReadBsonType());
                Assert.AreEqual(123, _bsonReader.ReadInt64());
                Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
            }
            Assert.AreEqual(json, BsonSerializer.Deserialize <long>(json).ToJson());
        }
Exemplo n.º 15
0
        public void TestInt64()
        {
            var json = "NumberLong(\"123456789012\")";

            using (_bsonReader = new JsonReader(json))
            {
                Assert.AreEqual(BsonType.Int64, _bsonReader.ReadBsonType());
                Assert.AreEqual(123456789012, _bsonReader.ReadInt64());
                Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
            }
            Assert.AreEqual(json, BsonSerializer.Deserialize <long>(json).ToJson());
        }
Exemplo n.º 16
0
        public void TestInt64ExtendedJson()
        {
            var json = "{ \"$numberLong\" : \"123\" }";

            using (_bsonReader = new JsonReader(json))
            {
                Assert.AreEqual(BsonType.Int64, _bsonReader.ReadBsonType());
                Assert.AreEqual(123, _bsonReader.ReadInt64());
                Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
            }
            var canonicalJson = "NumberLong(123)";

            Assert.AreEqual(canonicalJson, BsonSerializer.Deserialize <long>(new StringReader(json)).ToJson());
        }
Exemplo n.º 17
0
        public override DateTimeOffset Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            BsonReader bsonReader = (BsonReader)context.Reader;

            var            bsonType = bsonReader.GetCurrentBsonType();
            DateTimeOffset value;

            switch (bsonType)
            {
            case BsonType.DateTime:
                // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly
                value = (new BsonDateTime(bsonReader.ReadDateTime())).ToUniversalTime();
                break;

            case BsonType.Document:
                bsonReader.ReadStartDocument();
                bsonReader.ReadDateTime("DateTimeUTC");     // ignore value (use Ticks instead)
                value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc);
                bsonReader.ReadEndDocument();
                break;

            case BsonType.Int64:
                value = new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc);
                break;

            case BsonType.String:
                var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" };
                value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
                break;

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

            return(value);
        }
Exemplo n.º 18
0
        public void TestInt64NumberLong()
        {
            var json = "NumberLong(123)";

            using (bsonReader = BsonReader.Create(json)) {
                Assert.AreEqual(BsonType.Int64, bsonReader.ReadBsonType());
                Assert.AreEqual(123, bsonReader.ReadInt64());
                Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
            }
            var settings = new JsonWriterSettings {
                OutputMode = JsonOutputMode.TenGen
            };

            Assert.AreEqual(json, BsonSerializer.Deserialize <long>(new StringReader(json)).ToJson(settings));
        }
        // 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(BsonInt64));

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Int64:
                    return new BsonInt64(bsonReader.ReadInt64());
                default:
                    var message = string.Format("Cannot deserialize BsonInt64 from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
Exemplo n.º 20
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);
        }
        // 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(BsonInt64));

            var bsonType = bsonReader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Int64:
                return(new BsonInt64(bsonReader.ReadInt64()));

            default:
                var message = string.Format("Cannot deserialize BsonInt64 from BsonType {0}.", bsonType);
                throw new FileFormatException(message);
            }
        }
Exemplo 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(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);
            }
        }
Exemplo 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,
            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);
            }
        }
        // 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(DateTime));
            var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);

            var bsonType = bsonReader.GetCurrentBsonType();
            DateTime value;
            switch (bsonType)
            {
                case BsonType.DateTime:
                    // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly
                    value = new BsonDateTime(bsonReader.ReadDateTime()).ToUniversalTime();
                    break;
                case BsonType.Document:
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadDateTime("DateTime"); // ignore value (use Ticks instead)
                    value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc);
                    bsonReader.ReadEndDocument();
                    break;
                case BsonType.Int64:
                    value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64()), DateTimeKind.Utc);
                    break;
                case BsonType.String:
                    // note: we're not using XmlConvert because of bugs in Mono
                    if (dateTimeSerializationOptions.DateOnly)
                    {
                        value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
                    }
                    else
                    {
                        var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" };
                        value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
                    }
                    break;
                default:
                    var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }

            if (dateTimeSerializationOptions.DateOnly)
            {
                if (value.TimeOfDay != TimeSpan.Zero)
                {
                    throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero.");
                }
                value = DateTime.SpecifyKind(value, dateTimeSerializationOptions.Kind); // not ToLocalTime or ToUniversalTime!
            }
            else
            {
                switch (dateTimeSerializationOptions.Kind)
                {
                    case DateTimeKind.Local:
                    case DateTimeKind.Unspecified:
                        value = DateTime.SpecifyKind(BsonUtils.ToLocalTime(value), dateTimeSerializationOptions.Kind);
                        break;
                    case DateTimeKind.Utc:
                        value = BsonUtils.ToUniversalTime(value);
                        break;
                }
            }

            return value;
        }
Exemplo n.º 25
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);
            }
        }
Exemplo n.º 26
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
            )
        {
            DateTime value;

            var bsonType        = bsonReader.CurrentBsonType;
            var dateTimeOptions = (options == null) ? DateTimeSerializationOptions.Defaults : (DateTimeSerializationOptions)options;

            switch (bsonType)
            {
            case BsonType.DateTime:
                value = bsonReader.ReadDateTime();
                break;

            case BsonType.Document:
                bsonReader.ReadStartDocument();
                bsonReader.ReadDateTime("DateTime");     // ignore value (use Ticks instead)
                value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64("Ticks")), DateTimeKind.Utc);
                bsonReader.ReadEndDocument();
                break;

            case BsonType.Int64:
                value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64()), DateTimeKind.Utc);
                break;

            case BsonType.String:
                // note: we're not using XmlConvert because of bugs in Mono
                if (dateTimeOptions.DateOnly)
                {
                    value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
                }
                else
                {
                    var formats = new string[] {
                        "yyyy-MM-ddK",
                        "yyyy-MM-ddTHH:mm:ssK",
                        "yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
                    };
                    value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
                }
                break;

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

            if (dateTimeOptions.DateOnly)
            {
                if (value.TimeOfDay != TimeSpan.Zero)
                {
                    throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero");
                }
                value = DateTime.SpecifyKind(value, dateTimeOptions.Kind); // not ToLocalTime or ToUniversalTime!
            }
            else
            {
                switch (dateTimeOptions.Kind)
                {
                case DateTimeKind.Local:
                case DateTimeKind.Unspecified:
                    value = ToLocalTimeHelper(value, dateTimeOptions.Kind);
                    break;

                case DateTimeKind.Utc:
                    value = ToUniversalTimeHelper(value);
                    break;
                }
            }

            return(value);
        }
Exemplo n.º 27
0
 public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
 {
     return(((decimal)bsonReader.ReadInt64()) / 10000);
 }
Exemplo n.º 28
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);
                }
            }
        }
Exemplo n.º 29
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(BsonDateTime));
            var dateTimeSerializationOptions = EnsureSerializationOptions <DateTimeSerializationOptions>(options);

            var bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.Null)
            {
                bsonReader.ReadNull();
                return(null);
            }
            else
            {
                long?millisecondsSinceEpoch = null;
                long?ticks = null;
                switch (bsonType)
                {
                case BsonType.DateTime:
                    millisecondsSinceEpoch = bsonReader.ReadDateTime();
                    break;

                case BsonType.Document:
                    bsonReader.ReadStartDocument();
                    millisecondsSinceEpoch = bsonReader.ReadDateTime("DateTime");
                    bsonReader.ReadName("Ticks");
                    var ticksValue = BsonValue.ReadFrom(bsonReader);
                    if (!ticksValue.IsBsonUndefined)
                    {
                        ticks = ticksValue.ToInt64();
                    }
                    bsonReader.ReadEndDocument();
                    break;

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

                case BsonType.String:
                    // note: we're not using XmlConvert because of bugs in Mono
                    DateTime dateTime;
                    if (dateTimeSerializationOptions.DateOnly)
                    {
                        dateTime = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
                    }
                    else
                    {
                        var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", };
                        dateTime = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
                    }
                    ticks = dateTime.Ticks;
                    break;

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

                BsonDateTime bsonDateTime;
                if (ticks.HasValue)
                {
                    bsonDateTime = BsonDateTime.Create(new DateTime(ticks.Value, DateTimeKind.Utc));
                }
                else
                {
                    bsonDateTime = BsonDateTime.Create(millisecondsSinceEpoch.Value);
                }

                if (dateTimeSerializationOptions.DateOnly)
                {
                    var dateTime = bsonDateTime.Value;
                    if (dateTime.TimeOfDay != TimeSpan.Zero)
                    {
                        throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero.");
                    }
                    bsonDateTime = BsonDateTime.Create(DateTime.SpecifyKind(dateTime, dateTimeSerializationOptions.Kind)); // not ToLocalTime or ToUniversalTime!
                }
                else
                {
                    if (bsonDateTime.IsValidDateTime)
                    {
                        var dateTime = bsonDateTime.Value;
                        switch (dateTimeSerializationOptions.Kind)
                        {
                        case DateTimeKind.Local:
                        case DateTimeKind.Unspecified:
                            dateTime = DateTime.SpecifyKind(BsonUtils.ToLocalTime(dateTime), dateTimeSerializationOptions.Kind);
                            break;

                        case DateTimeKind.Utc:
                            dateTime = BsonUtils.ToUniversalTime(dateTime);
                            break;
                        }
                        bsonDateTime = BsonDateTime.Create(dateTime);
                    }
                    else
                    {
                        if (dateTimeSerializationOptions.Kind != DateTimeKind.Utc)
                        {
                            throw new FileFormatException("BsonDateTime is outside the range of .NET DateTime.");
                        }
                    }
                }

                return(bsonDateTime);
            }
        }
Exemplo n.º 30
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;
        }
Exemplo n.º 31
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(DateTime));
            var dateTimeSerializationOptions = EnsureSerializationOptions <DateTimeSerializationOptions>(options);

            var      bsonType = bsonReader.GetCurrentBsonType();
            DateTime value;

            switch (bsonType)
            {
            case BsonType.DateTime:
                // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly
                value = new BsonDateTime(bsonReader.ReadDateTime()).ToUniversalTime();
                break;

            case BsonType.Document:
                bsonReader.ReadStartDocument();
                bsonReader.ReadDateTime("DateTime");     // ignore value (use Ticks instead)
                value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc);
                bsonReader.ReadEndDocument();
                break;

            case BsonType.Int64:
                value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64()), DateTimeKind.Utc);
                break;

            case BsonType.String:
                // note: we're not using XmlConvert because of bugs in Mono
                if (dateTimeSerializationOptions.DateOnly)
                {
                    value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
                }
                else
                {
                    var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" };
                    value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
                }
                break;

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

            if (dateTimeSerializationOptions.DateOnly)
            {
                if (value.TimeOfDay != TimeSpan.Zero)
                {
                    throw new Exception("TimeOfDay component for DateOnly DateTime value is not zero.");
                }
                value = DateTime.SpecifyKind(value, dateTimeSerializationOptions.Kind); // not ToLocalTime or ToUniversalTime!
            }
            else
            {
                switch (dateTimeSerializationOptions.Kind)
                {
                case DateTimeKind.Local:
                case DateTimeKind.Unspecified:
                    value = DateTime.SpecifyKind(BsonUtils.ToLocalTime(value), dateTimeSerializationOptions.Kind);
                    break;

                case DateTimeKind.Utc:
                    value = BsonUtils.ToUniversalTime(value);
                    break;
                }
            }

            return(value);
        }
Exemplo n.º 32
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);
                }
            }
        }
Exemplo n.º 33
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(BsonDateTime));
            var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);

            var bsonType = bsonReader.GetCurrentBsonType();
            if (bsonType == BsonType.Null)
            {
                bsonReader.ReadNull();
                return null;
            }
            else
            {
                long? millisecondsSinceEpoch = null;
                long? ticks = null;
                switch (bsonType)
                {
                    case BsonType.DateTime:
                        millisecondsSinceEpoch = bsonReader.ReadDateTime();
                        break;
                    case BsonType.Document:
                        bsonReader.ReadStartDocument();
                        millisecondsSinceEpoch = bsonReader.ReadDateTime("DateTime");
                        bsonReader.ReadName("Ticks");
                        var ticksValue = BsonValue.ReadFrom(bsonReader);
                        if (!ticksValue.IsBsonUndefined)
                        {
                            ticks = ticksValue.ToInt64();
                        }
                        bsonReader.ReadEndDocument();
                        break;
                    case BsonType.Int64:
                        ticks = bsonReader.ReadInt64();
                        break;
                    case BsonType.String:
                        // note: we're not using XmlConvert because of bugs in Mono
                        DateTime dateTime;
                        if (dateTimeSerializationOptions.DateOnly)
                        {
                            dateTime = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
                        }
                        else
                        {
                            var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", };
                            dateTime = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
                        }
                        ticks = dateTime.Ticks;
                        break;
                    default:
                        var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType);
                        throw new FileFormatException(message);
                }

                BsonDateTime bsonDateTime;
                if (ticks.HasValue)
                {
                    bsonDateTime = BsonDateTime.Create(new DateTime(ticks.Value, DateTimeKind.Utc));
                }
                else
                {
                    bsonDateTime = BsonDateTime.Create(millisecondsSinceEpoch.Value);
                }

                if (dateTimeSerializationOptions.DateOnly)
                {
                    var dateTime = bsonDateTime.Value;
                    if (dateTime.TimeOfDay != TimeSpan.Zero)
                    {
                        throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero.");
                    }
                    bsonDateTime = BsonDateTime.Create(DateTime.SpecifyKind(dateTime, dateTimeSerializationOptions.Kind)); // not ToLocalTime or ToUniversalTime!
                }
                else
                {
                    if (bsonDateTime.IsValidDateTime)
                    {
                        var dateTime = bsonDateTime.Value;
                        switch (dateTimeSerializationOptions.Kind)
                        {
                            case DateTimeKind.Local:
                            case DateTimeKind.Unspecified:
                                dateTime = DateTime.SpecifyKind(BsonUtils.ToLocalTime(dateTime), dateTimeSerializationOptions.Kind);
                                break;
                            case DateTimeKind.Utc:
                                dateTime = BsonUtils.ToUniversalTime(dateTime);
                                break;
                        }
                        bsonDateTime = BsonDateTime.Create(dateTime);
                    }
                    else
                    {
                        if (dateTimeSerializationOptions.Kind != DateTimeKind.Utc)
                        {
                            throw new FileFormatException("BsonDateTime is outside the range of .NET DateTime.");
                        }
                    }
                }

                return bsonDateTime;
            }
        }
Exemplo n.º 34
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(sbyte));

            var bsonType = bsonReader.GetCurrentBsonType();
            var lostData = false;
            sbyte value;
            switch (bsonType)
            {
                case BsonType.Binary:
                    var bytes = bsonReader.ReadBytes();
                    if (bytes.Length != 1)
                    {
                        throw new FileFormatException("Binary data for SByte must be exactly one byte long.");
                    }
                    value = (sbyte)bytes[0];
                    break;
                case BsonType.Int32:
                    var int32Value = bsonReader.ReadInt32();
                    value = (sbyte)int32Value;
                    lostData = (int)value != int32Value;
                    break;
                case BsonType.Int64:
                    var int64Value = bsonReader.ReadInt64();
                    value = (sbyte)int64Value;
                    lostData = (int)value != int64Value;
                    break;
                case BsonType.String:
                    var s = bsonReader.ReadString();
                    if (s.Length == 1)
                    {
                        s = "0" + s;
                    }
                    value = (sbyte)byte.Parse(s, NumberStyles.HexNumber);
                    break;
                default:
                    var message = string.Format("Cannot deserialize SByte from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
            if (lostData)
            {
                var message = string.Format("Data loss occurred when trying to convert from {0} to SByte.", bsonType);
                throw new FileFormatException(message);
            }

            return value;
        }