-
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
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,7 +72,7 @@ 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) | ||
|
@@ -67,6 +81,7 @@ public TimeOnlySerializer(BsonType representation, TimeOnlyUnits units) | |
case BsonType.Int32: | ||
case BsonType.Int64: | ||
case BsonType.String: | ||
case BsonType.Document: | ||
break; | ||
|
||
default: | ||
|
@@ -75,6 +90,17 @@ public TimeOnlySerializer(BsonType representation, TimeOnlyUnits units) | |
|
||
_representation = representation; | ||
_units = units; | ||
|
||
_helper = new SerializerHelper | ||
( | ||
new SerializerHelper.Member("Hour", Flags.Hour, isOptional: true), | ||
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 a little weird that these are optional. In understand that we ignore them during deserialization (we only use We also might want to validate during deserialization that the 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. Consider the following change:
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. I agree, that probably those should not be optional, I'll answer the question about validation in the other comment. |
||
new SerializerHelper.Member("Minute", Flags.Minute, isOptional: true), | ||
new SerializerHelper.Member("Second", Flags.Second, isOptional: true), | ||
new SerializerHelper.Member("Millisecond", Flags.Millisecond, isOptional: true), | ||
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 | ||
|
@@ -102,6 +128,7 @@ public override TimeOnly Deserialize(BsonDeserializationContext context, BsonDes | |
BsonType.Int64 => FromInt64(bsonReader.ReadInt64(), _units), | ||
BsonType.Int32 => FromInt32(bsonReader.ReadInt32(), _units), | ||
BsonType.Double => FromDouble(bsonReader.ReadDouble(), _units), | ||
BsonType.Document => FromDocument(context), | ||
papafe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_ => throw CreateCannotDeserializeFromBsonTypeException(bsonType) | ||
}; | ||
} | ||
|
@@ -145,6 +172,20 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati | |
bsonWriter.WriteString(value.ToString("o")); | ||
break; | ||
|
||
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); | ||
#if NET7_0_OR_GREATER | ||
|
||
bsonWriter.WriteInt32("Microsecond", value.Microsecond); | ||
bsonWriter.WriteInt32("Nanosecond", value.Nanosecond); | ||
#endif | ||
bsonWriter.WriteInt64("Ticks", value.Ticks); | ||
bsonWriter.WriteEndDocument(); | ||
break; | ||
|
||
default: | ||
throw new BsonSerializationException($"'{_representation}' is not a valid TimeOnly representation."); | ||
} | ||
|
@@ -196,6 +237,29 @@ private TimeOnly FromInt64(long value, TimeOnlyUnits units) | |
: new TimeOnly(value * TicksPerUnit(units)); | ||
} | ||
|
||
private TimeOnly FromDocument(BsonDeserializationContext context) | ||
{ | ||
var bsonReader = context.Reader; | ||
var ticks = 0L; | ||
|
||
_helper.DeserializeMembers(context, (_, flag) => | ||
{ | ||
switch (flag) | ||
{ | ||
case Flags.Hour: | ||
case Flags.Minute: | ||
case Flags.Second: | ||
case Flags.Millisecond: | ||
case Flags.Microsecond: | ||
case Flags.Nanosecond: | ||
bsonReader.SkipValue(); break; // ignore value (use Ticks instead) | ||
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. Do we really want to ignore these values? Should we check that they are in agreement with the 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. This is similar to what we do with |
||
case Flags.Ticks: ticks = Int64Serializer.Instance.Deserialize(context); break; | ||
} | ||
}); | ||
|
||
return FromInt64(ticks, TimeOnlyUnits.Ticks); | ||
} | ||
|
||
private long TicksPerUnit(TimeOnlyUnits units) | ||
{ | ||
return units switch | ||
|
Uh oh!
There was an error while loading. Please reload this page.