-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serializing and deserializing concepts of DateTime, DateTimeOffse…
…t, DateOnly and TimeOnly.
- Loading branch information
Showing
1 changed file
with
48 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 GitHub Actions / build
Check failure on line 8 in Source/DotNET/MongoDB/ConceptSerializer.cs GitHub Actions / build
|
||
|
||
namespace Cratis.Applications.MongoDB; | ||
|
||
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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/> | ||
|
@@ -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) | ||
|
@@ -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); | ||
} | ||
} |