Skip to content

Commit

Permalink
Add TimeOnlySerializer and DateOnlySerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
smellilac committed Aug 26, 2024
1 parent 4eda44c commit 8b84f63
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Source/DotNET/MongoDB/ConceptSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using System.Globalization;

namespace Cratis.Applications.MongoDB;

Expand Down Expand Up @@ -138,13 +137,13 @@ public void Serialize(BsonSerializationContext context, BsonSerializationArgs ar
}
else if (underlyingValueType == typeof(DateOnly))
{
var dateOnly = (DateOnly)underlyingValue;
bsonWriter.WriteString(dateOnly.ToString("yyyy-MM-dd"));
var serializer = new DateOnlySerializer();
serializer.Serialize(context, args, (DateOnly)underlyingValue);
}
else if (underlyingValueType == typeof(TimeOnly))
{
var timeOnly = (TimeOnly)underlyingValue;
bsonWriter.WriteString(timeOnly.ToString("HH:mm:ss"));
var serializer = new TimeOnlySerializer();
serializer.Serialize(context, args, (TimeOnly)underlyingValue);
}
}

Expand Down Expand Up @@ -235,14 +234,14 @@ object GetDeserializedValue(BsonDeserializationContext context, BsonDeserializat

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

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

throw new UnableToDeserializeValueForConcept(valueType);
Expand Down

0 comments on commit 8b84f63

Please sign in to comment.