-
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
Changes from 7 commits
b7f6663
f3076bf
e184088
9ef4586
0f833fb
79efbf5
2dc7058
3653441
cbbba86
25eddfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 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), | ||
papafe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
|
@@ -145,6 +188,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati | |
bsonWriter.WriteString(value.ToString("o")); | ||
break; | ||
|
||
|
||
|
||
default: | ||
throw new BsonSerializationException($"'{_representation}' is not a valid TimeOnly representation."); | ||
} | ||
|
@@ -196,6 +240,59 @@ private TimeOnly FromInt64(long value, TimeOnlyUnits units) | |
: new TimeOnly(value * TicksPerUnit(units)); | ||
} | ||
|
||
papafe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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 long TicksPerUnit(TimeOnlyUnits units) | ||
{ | ||
return units switch | ||
|
@@ -231,6 +328,18 @@ private long ToInt64(TimeOnly timeOnly, TimeOnlyUnits units) | |
: timeOnly.Ticks / TicksPerUnit(units); | ||
} | ||
|
||
private int GetNanosecondsComponent(long ticks) | ||
|
||
{ | ||
// ticks % 10 * 100 | ||
return (int)(ticks % TicksPerUnit(TimeOnlyUnits.Microseconds) * 100); | ||
|
||
} | ||
|
||
private int GetMicrosecondsComponent(long ticks) | ||
{ | ||
// ticks / 10 % 1000 | ||
return (int)(ticks / TicksPerUnit(TimeOnlyUnits.Microseconds) % 1000); | ||
|
||
} | ||
|
||
papafe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// explicit interface implementations | ||
IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nits: we usually put a space after
//
in comments.Also, instead of "was serialized as a POCO" say "was serialized as a class mapped POCO".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deal.