BsonDateTimeSerializer.cs 2.5 KB
Newer Older
T
tanghai 已提交
1
/* Copyright 2010-2015 MongoDB Inc.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
    /// <summary>
    /// Represents a serializer for BsonDateTimes.
    /// </summary>
T
tanghai 已提交
22
    public class BsonDateTimeSerializer : BsonValueSerializerBase<BsonDateTime>
23 24 25 26 27 28 29 30 31
    {
        // private static fields
        private static BsonDateTimeSerializer __instance = new BsonDateTimeSerializer();

        // constructors
        /// <summary>
        /// Initializes a new instance of the BsonDateTimeSerializer class.
        /// </summary>
        public BsonDateTimeSerializer()
T
tanghai 已提交
32
            : base(BsonType.DateTime)
33 34 35 36 37 38 39 40 41 42 43 44
        {
        }

        // public static properties
        /// <summary>
        /// Gets an instance of the BsonDateTimeSerializer class.
        /// </summary>
        public static BsonDateTimeSerializer Instance
        {
            get { return __instance; }
        }

T
tanghai 已提交
45
        // protected methods
46
        /// <summary>
T
tanghai 已提交
47
        /// Deserializes a value.
48
        /// </summary>
T
tanghai 已提交
49 50 51 52
        /// <param name="context">The deserialization context.</param>
        /// <param name="args">The deserialization args.</param>
        /// <returns>A deserialized value.</returns>
        protected override BsonDateTime DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
53
        {
T
tanghai 已提交
54 55
            var bsonReader = context.Reader;
            return new BsonDateTime(bsonReader.ReadDateTime());
56 57 58
        }

        /// <summary>
T
tanghai 已提交
59
        /// Serializes a value.
60
        /// </summary>
T
tanghai 已提交
61 62
        /// <param name="context">The serialization context.</param>
        /// <param name="args">The serialization args.</param>
63
        /// <param name="value">The object.</param>
T
tanghai 已提交
64
        protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, BsonDateTime value)
65
        {
T
tanghai 已提交
66 67
            var bsonWriter = context.Writer;
            bsonWriter.WriteDateTime(value.MillisecondsSinceEpoch);
68 69 70
        }
    }
}