Skip to content

Commit

Permalink
Add serializing and deserializing concepts of DateTime, DateTimeOffse…
Browse files Browse the repository at this point in the history
…t, DateOnly and TimeOnly.
  • Loading branch information
smellilac committed Aug 17, 2024
1 parent 0882148 commit 4eda44c
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions Source/DotNET/MongoDB/ConceptSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using System.Globalization;

Check failure on line 8 in Source/DotNET/MongoDB/ConceptSerializer.cs

View workflow job for this annotation

GitHub Actions / build

Using directive for 'System.Globalization' should appear before directive for 'MongoDB.Bson.Serialization' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1208.md)

Check failure on line 8 in Source/DotNET/MongoDB/ConceptSerializer.cs

View workflow job for this annotation

GitHub Actions / build

Using directive for 'System.Globalization' should appear before directive for 'MongoDB.Bson.Serialization' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1208.md)

namespace Cratis.Applications.MongoDB;

Expand Down Expand Up @@ -47,7 +48,7 @@ public T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs
var keyName = bsonReader.ReadName(Utf8NameDecoder.Instance);
if (keyName == "Value" || keyName == "value")
{
value = GetDeserializedValue(valueType, ref bsonReader);
value = GetDeserializedValue(context, args, valueType, ref bsonReader);
bsonReader.ReadEndDocument();
}
else
Expand All @@ -57,7 +58,7 @@ public T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs
}
else
{
value = GetDeserializedValue(valueType, ref bsonReader);
value = GetDeserializedValue(context, args, valueType, ref bsonReader);
}

if (value is null)
Expand Down Expand Up @@ -125,6 +126,26 @@ public void Serialize(BsonSerializationContext context, BsonSerializationArgs ar
{
bsonWriter.WriteDecimal128((decimal)underlyingValue);
}
else if (underlyingValueType == typeof(DateTime))
{
var dateTime = (DateTime)underlyingValue;
bsonWriter.WriteDateTime(dateTime.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond);
}
else if (underlyingValueType == typeof(DateTimeOffset))
{
var serializer = new DateTimeOffsetSupportingBsonDateTimeSerializer();
serializer.Serialize(context, args, (DateTimeOffset)underlyingValue);
}
else if (underlyingValueType == typeof(DateOnly))
{
var dateOnly = (DateOnly)underlyingValue;
bsonWriter.WriteString(dateOnly.ToString("yyyy-MM-dd"));
}
else if (underlyingValueType == typeof(TimeOnly))
{
var timeOnly = (TimeOnly)underlyingValue;
bsonWriter.WriteString(timeOnly.ToString("HH:mm:ss"));
}
}

/// <inheritdoc/>
Expand All @@ -136,7 +157,7 @@ public void Serialize(BsonSerializationContext context, BsonSerializationArgs ar
/// <inheritdoc/>
object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) => Deserialize(context, args)!;

object GetDeserializedValue(Type valueType, ref IBsonReader bsonReader)
object GetDeserializedValue(BsonDeserializationContext context, BsonDeserializationArgs args, Type valueType, ref IBsonReader bsonReader)
{
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null)
Expand Down Expand Up @@ -200,6 +221,30 @@ object GetDeserializedValue(Type valueType, ref IBsonReader bsonReader)
return bsonReader.ReadDecimal128();
}

if (valueType == typeof(DateTime))
{
var dateTimeValue = bsonReader.ReadDateTime();
return DateTimeOffset.FromUnixTimeMilliseconds(dateTimeValue).DateTime;
}

if (valueType == typeof(DateTimeOffset))
{
var serializer = new DateTimeOffsetSupportingBsonDateTimeSerializer();
return serializer.Deserialize(context, args);
}

if (valueType == typeof(DateOnly))
{
var dateOnlyString = bsonReader.ReadString();
return DateOnly.ParseExact(dateOnlyString, "yyyy-MM-dd", CultureInfo.InvariantCulture);
}

if (valueType == typeof(TimeOnly))
{
var timeOnlyString = bsonReader.ReadString();
return TimeOnly.ParseExact(timeOnlyString, "HH:mm:ss", CultureInfo.InvariantCulture);
}

throw new UnableToDeserializeValueForConcept(valueType);
}
}

0 comments on commit 4eda44c

Please sign in to comment.