summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Caching
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/Framework/Caching')
-rw-r--r--src/SMAPI.Web/Framework/Caching/UtcDateTimeOffsetSerializer.cs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/SMAPI.Web/Framework/Caching/UtcDateTimeOffsetSerializer.cs b/src/SMAPI.Web/Framework/Caching/UtcDateTimeOffsetSerializer.cs
new file mode 100644
index 00000000..ad95a975
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Caching/UtcDateTimeOffsetSerializer.cs
@@ -0,0 +1,40 @@
+using System;
+using MongoDB.Bson;
+using MongoDB.Bson.Serialization;
+using MongoDB.Bson.Serialization.Serializers;
+
+namespace StardewModdingAPI.Web.Framework.Caching
+{
+ /// <summary>Serialises <see cref="DateTimeOffset"/> to a UTC date field instead of the default array.</summary>
+ public class UtcDateTimeOffsetSerializer : StructSerializerBase<DateTimeOffset>
+ {
+ /*********
+ ** Fields
+ *********/
+ /// <summary>The underlying date serializer.</summary>
+ private static readonly DateTimeSerializer DateTimeSerializer = new DateTimeSerializer(DateTimeKind.Utc, BsonType.DateTime);
+
+
+ /*********
+ ** 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)
+ {
+ DateTime date = UtcDateTimeOffsetSerializer.DateTimeSerializer.Deserialize(context, args);
+ return new DateTimeOffset(date, TimeSpan.Zero);
+ }
+
+ /// <summary>Serializes a value.</summary>
+ /// <param name="context">The serialization context.</param>
+ /// <param name="args">The serialization args.</param>
+ /// <param name="value">The object.</param>
+ public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTimeOffset value)
+ {
+ UtcDateTimeOffsetSerializer.DateTimeSerializer.Serialize(context, args, value.UtcDateTime);
+ }
+ }
+}