-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5737: Add legacy representation for TimeOnly #1783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
papafe
wants to merge
10
commits into
mongodb:main
Choose a base branch
from
papafe:csharp5737
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−16
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b7f6663
CSHARP-5737: Add legacy representation for TimeOnly
papafe f3076bf
Added serialization test
papafe e184088
added comment
papafe 9ef4586
Added micro and nanosecond support
papafe 0f833fb
Fixed spelling and ordering
papafe 79efbf5
Fixes according to PR comments
papafe 2dc7058
Small fix
papafe 3653441
Various fixes.
papafe cbbba86
Corrected method.
papafe 25eddfa
Small fixes
papafe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
*/ | ||
|
||
using System; | ||
using MongoDB.Bson.IO; | ||
using MongoDB.Bson.Serialization.Options; | ||
|
||
namespace MongoDB.Bson.Serialization.Serializers | ||
|
@@ -32,7 +33,20 @@ public sealed class TimeOnlySerializer: StructSerializerBase<TimeOnly>, IReprese | |
/// </summary> | ||
public static TimeOnlySerializer Instance => __instance; | ||
|
||
// private constants | ||
private static class Flags | ||
{ | ||
public const long Hour = 1; | ||
public const long Minute = 2; | ||
public const long Second = 4; | ||
public const long Millisecond = 8; | ||
public const long Microsecond = 16; | ||
public const long Nanosecond = 32; | ||
public const long Ticks = 64; | ||
} | ||
|
||
// private fields | ||
private readonly SerializerHelper _helper; | ||
private readonly BsonType _representation; | ||
private readonly TimeOnlyUnits _units; | ||
|
||
|
@@ -58,11 +72,12 @@ public TimeOnlySerializer(BsonType representation) | |
/// Initializes a new instance of the <see cref="TimeOnlySerializer"/> class. | ||
/// </summary> | ||
/// <param name="representation">The representation.</param> | ||
/// <param name="units">The units.</param> | ||
/// <param name="units">The units. Ignored if representation is BsonType.Document.</param> | ||
public TimeOnlySerializer(BsonType representation, TimeOnlyUnits units) | ||
{ | ||
switch (representation) | ||
{ | ||
case BsonType.Document: | ||
case BsonType.Double: | ||
case BsonType.Int32: | ||
case BsonType.Int64: | ||
|
@@ -75,6 +90,20 @@ public TimeOnlySerializer(BsonType representation, TimeOnlyUnits units) | |
|
||
_representation = representation; | ||
_units = units; | ||
|
||
_helper = new SerializerHelper | ||
( | ||
// TimeOnlySerializer was introduced in version 3.0.0 of the driver. Prior to that, TimeOnly was serialized | ||
// as a class mapped POCO. Due to that, Microsecond and Nanosecond could be missing, as they were introduced in .NET 7. | ||
// To avoid deserialization issues, we treat Microsecond and Nanosecond as optional members. | ||
new SerializerHelper.Member("Hour", Flags.Hour, isOptional: false), | ||
new SerializerHelper.Member("Minute", Flags.Minute, isOptional: false), | ||
new SerializerHelper.Member("Second", Flags.Second, isOptional: false), | ||
new SerializerHelper.Member("Millisecond", Flags.Millisecond, isOptional: false), | ||
new SerializerHelper.Member("Microsecond", Flags.Microsecond, isOptional: true), | ||
new SerializerHelper.Member("Nanosecond", Flags.Nanosecond, isOptional: true), | ||
new SerializerHelper.Member("Ticks", Flags.Ticks, isOptional: false) | ||
); | ||
} | ||
|
||
// public properties | ||
|
@@ -98,10 +127,11 @@ public override TimeOnly Deserialize(BsonDeserializationContext context, BsonDes | |
|
||
return bsonType switch | ||
{ | ||
BsonType.String => TimeOnly.ParseExact(bsonReader.ReadString(), "o"), | ||
BsonType.Int64 => FromInt64(bsonReader.ReadInt64(), _units), | ||
BsonType.Int32 => FromInt32(bsonReader.ReadInt32(), _units), | ||
BsonType.Document => FromDocument(context), | ||
BsonType.Double => FromDouble(bsonReader.ReadDouble(), _units), | ||
BsonType.Int32 => FromInt32(bsonReader.ReadInt32(), _units), | ||
BsonType.Int64 => FromInt64(bsonReader.ReadInt64(), _units), | ||
BsonType.String => TimeOnly.ParseExact(bsonReader.ReadString(), "o"), | ||
_ => throw CreateCannotDeserializeFromBsonTypeException(bsonType) | ||
}; | ||
} | ||
|
@@ -129,6 +159,19 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati | |
|
||
switch (_representation) | ||
{ | ||
case BsonType.Document: | ||
bsonWriter.WriteStartDocument(); | ||
bsonWriter.WriteInt32("Hour", value.Hour); | ||
bsonWriter.WriteInt32("Minute", value.Minute); | ||
bsonWriter.WriteInt32("Second", value.Second); | ||
bsonWriter.WriteInt32("Millisecond", value.Millisecond); | ||
// Microsecond and Nanosecond properties were added in .NET 7 | ||
bsonWriter.WriteInt32("Microsecond", GetMicrosecondsComponent(value.Ticks)); | ||
bsonWriter.WriteInt32("Nanosecond", GetNanosecondsComponent(value.Ticks)); | ||
bsonWriter.WriteInt64("Ticks", value.Ticks); | ||
bsonWriter.WriteEndDocument(); | ||
break; | ||
|
||
case BsonType.Double: | ||
bsonWriter.WriteDouble(ToDouble(value, _units)); | ||
break; | ||
|
@@ -175,6 +218,60 @@ public TimeOnlySerializer WithRepresentation(BsonType representation, TimeOnlyUn | |
} | ||
|
||
// private methods | ||
private TimeOnly FromDocument(BsonDeserializationContext context) | ||
papafe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var bsonReader = context.Reader; | ||
var hour = 0; | ||
var minute = 0; | ||
var second = 0; | ||
var millisecond = 0; | ||
int? microsecond = null; | ||
int? nanosecond = null; | ||
var ticks = 0L; | ||
|
||
_helper.DeserializeMembers(context, (_, flag) => | ||
{ | ||
switch (flag) | ||
{ | ||
case Flags.Hour: | ||
hour = bsonReader.ReadInt32(); | ||
break; | ||
case Flags.Minute: | ||
minute = bsonReader.ReadInt32(); | ||
break; | ||
case Flags.Second: | ||
second = bsonReader.ReadInt32(); | ||
break; | ||
case Flags.Millisecond: | ||
millisecond = bsonReader.ReadInt32(); | ||
break; | ||
case Flags.Microsecond: | ||
microsecond = bsonReader.ReadInt32(); | ||
break; | ||
case Flags.Nanosecond: | ||
nanosecond = bsonReader.ReadInt32(); | ||
break; | ||
case Flags.Ticks: | ||
ticks = bsonReader.ReadInt64(); | ||
break; | ||
} | ||
}); | ||
|
||
var deserializedTimeOnly = new TimeOnly(ticks); | ||
|
||
if (deserializedTimeOnly.Hour != hour || | ||
deserializedTimeOnly.Minute != minute || | ||
deserializedTimeOnly.Second != second || | ||
deserializedTimeOnly.Millisecond != millisecond || | ||
(microsecond.HasValue && GetMicrosecondsComponent(deserializedTimeOnly.Ticks) != microsecond.Value) || | ||
(nanosecond.HasValue && GetNanosecondsComponent(deserializedTimeOnly.Ticks) != nanosecond.Value)) | ||
{ | ||
throw new BsonSerializationException("Deserialized TimeOnly components do not match the ticks value."); | ||
} | ||
|
||
return deserializedTimeOnly; | ||
} | ||
|
||
private TimeOnly FromDouble(double value, TimeOnlyUnits units) | ||
{ | ||
return units is TimeOnlyUnits.Nanoseconds | ||
|
@@ -196,6 +293,18 @@ private TimeOnly FromInt64(long value, TimeOnlyUnits units) | |
: new TimeOnly(value * TicksPerUnit(units)); | ||
} | ||
|
||
private int GetMicrosecondsComponent(long ticks) | ||
{ | ||
var ticksPerMicrosecond = TicksPerUnit(TimeOnlyUnits.Microseconds); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine to the |
||
return (int)(ticks / ticksPerMicrosecond % 1000); | ||
} | ||
|
||
private int GetNanosecondsComponent(long ticks) | ||
{ | ||
var nanosecondsPerTick = 100; | ||
return (int)(ticks * nanosecondsPerTick % 1000); | ||
} | ||
|
||
private long TicksPerUnit(TimeOnlyUnits units) | ||
{ | ||
return units switch | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.