Deserialize() public method

Deserializes an object from a BsonReader.
public Deserialize ( MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options ) : object
bsonReader MongoDB.Bson.IO.BsonReader The BsonReader.
nominalType System.Type The nominal type of the object.
actualType System.Type The actual type of the object.
options IBsonSerializationOptions The serialization options.
return object
        // public methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <param name="args">The deserialization args.</param>
        /// <returns>A deserialized value.</returns>
        public override DateTimeOffset Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var      bsonReader = context.Reader;
            long     ticks;
            TimeSpan offset;

            BsonType bsonType = bsonReader.GetCurrentBsonType();

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

            case BsonType.DateTime:
                var millisecondsSinceEpoch = bsonReader.ReadDateTime();
                return(DateTimeOffset.FromUnixTimeMilliseconds(millisecondsSinceEpoch));

            case BsonType.Document:
                ticks  = 0;
                offset = TimeSpan.Zero;
                _helper.DeserializeMembers(context, (elementName, flag) =>
                {
                    switch (flag)
                    {
                    case Flags.DateTime: bsonReader.SkipValue(); break;         // ignore value

                    case Flags.Ticks: ticks = _int64Serializer.Deserialize(context); break;

                    case Flags.Offset: offset = TimeSpan.FromMinutes(_int32Serializer.Deserialize(context)); break;
                    }
                });
                return(new DateTimeOffset(ticks, offset));

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

            default:
                throw CreateCannotDeserializeFromBsonTypeException(bsonType);
            }
        }
Exemplo n.º 2
0
        // public methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <param name="args">The deserialization args.</param>
        /// <returns>A deserialized value.</returns>
        public override DateTime Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var      bsonReader = context.Reader;
            DateTime value;

            var bsonType = bsonReader.GetCurrentBsonType();

            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:
                value = default(DateTime);
                _helper.DeserializeMembers(context, (elementName, flag) =>
                {
                    switch (flag)
                    {
                    case Flags.DateTime: bsonReader.SkipValue(); break;         // ignore value (use Ticks instead)

                    case Flags.Ticks: value = new DateTime(_int64Serializer.Deserialize(context), DateTimeKind.Utc); break;
                    }
                });
                break;

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

            case BsonType.String:
                if (_dateOnly)
                {
                    value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
                }
                else
                {
                    value = JsonConvert.ToDateTime(bsonReader.ReadString());
                }
                break;

            default:
                throw CreateCannotDeserializeFromBsonTypeException(bsonType);
            }

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

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

            return(value);
        }