using System; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; namespace StardewModdingAPI.Web.Framework.Caching { /// Serializes to a UTC date field instead of the default array. public class UtcDateTimeOffsetSerializer : StructSerializerBase { /********* ** Fields *********/ /// The underlying date serializer. private static readonly DateTimeSerializer DateTimeSerializer = new DateTimeSerializer(DateTimeKind.Utc, BsonType.DateTime); /********* ** Public methods *********/ /// Deserializes a value. /// The deserialization context. /// The deserialization args. /// A deserialized value. public override DateTimeOffset Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { DateTime date = UtcDateTimeOffsetSerializer.DateTimeSerializer.Deserialize(context, args); return new DateTimeOffset(date, TimeSpan.Zero); } /// Serializes a value. /// The serialization context. /// The serialization args. /// The object. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTimeOffset value) { UtcDateTimeOffsetSerializer.DateTimeSerializer.Serialize(context, args, value.UtcDateTime); } } }