|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +using System; |
| 17 | +using MongoDB.Bson.IO; |
| 18 | +using MongoDB.Bson.Serialization.Options; |
| 19 | + |
| 20 | +namespace MongoDB.Bson.Serialization.Serializers |
| 21 | +{ |
| 22 | +#if NET6_0_OR_GREATER |
| 23 | + /// <summary> |
| 24 | + /// Represents a serializer for DateOnlys. |
| 25 | + /// </summary> |
| 26 | + public sealed class DateOnlySerializer : StructSerializerBase<DateOnly>, IRepresentationConfigurable<DateOnlySerializer> |
| 27 | + { |
| 28 | + // static |
| 29 | + private static readonly DateOnlySerializer __instance = new DateOnlySerializer(); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets the default DateOnlySerializer. |
| 33 | + /// </summary> |
| 34 | + public static DateOnlySerializer Instance => __instance; |
| 35 | + |
| 36 | + // private constants |
| 37 | + private static class Flags |
| 38 | + { |
| 39 | + public const long DateTime = 1; |
| 40 | + public const long Ticks = 2; |
| 41 | + } |
| 42 | + |
| 43 | + // private fields |
| 44 | + private readonly RepresentationConverter _converter; |
| 45 | + private readonly SerializerHelper _helper; |
| 46 | + private readonly BsonType _representation; |
| 47 | + |
| 48 | + // constructors |
| 49 | + /// <summary> |
| 50 | + /// Initializes a new instance of the <see cref="DateOnlySerializer"/> class. |
| 51 | + /// </summary> |
| 52 | + public DateOnlySerializer() |
| 53 | + : this(BsonType.DateTime) |
| 54 | + { |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Initializes a new instance of the <see cref="DateOnlySerializer"/> class. |
| 59 | + /// </summary> |
| 60 | + /// <param name="representation">The representation.</param> |
| 61 | + public DateOnlySerializer(BsonType representation) |
| 62 | + { |
| 63 | + switch (representation) |
| 64 | + { |
| 65 | + case BsonType.DateTime: |
| 66 | + case BsonType.Document: |
| 67 | + case BsonType.Int64: |
| 68 | + case BsonType.String: |
| 69 | + break; |
| 70 | + |
| 71 | + default: |
| 72 | + throw new ArgumentException($"{representation} is not a valid representation for a DateOnlySerializer."); |
| 73 | + } |
| 74 | + |
| 75 | + _representation = representation; |
| 76 | + _converter = new RepresentationConverter(false, false); |
| 77 | + |
| 78 | + _helper = new SerializerHelper |
| 79 | + ( |
| 80 | + new SerializerHelper.Member("DateTime", Flags.DateTime), |
| 81 | + new SerializerHelper.Member("Ticks", Flags.Ticks) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + // public properties |
| 86 | + /// <inheritdoc /> |
| 87 | + public BsonType Representation => _representation; |
| 88 | + |
| 89 | + //public methods |
| 90 | + /// <inheritdoc /> |
| 91 | + public override DateOnly Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) |
| 92 | + { |
| 93 | + var bsonReader = context.Reader; |
| 94 | + DateOnly value; |
| 95 | + |
| 96 | + var bsonType = bsonReader.GetCurrentBsonType(); |
| 97 | + |
| 98 | + switch (bsonType) |
| 99 | + { |
| 100 | + case BsonType.DateTime: |
| 101 | + value = VerifyAndMakeDateOnly(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime())); |
| 102 | + break; |
| 103 | + |
| 104 | + case BsonType.Document: |
| 105 | + value = default; |
| 106 | + _helper.DeserializeMembers(context, (_, flag) => |
| 107 | + { |
| 108 | + switch (flag) |
| 109 | + { |
| 110 | + case Flags.DateTime: bsonReader.SkipValue(); break; // ignore value (use Ticks instead) |
| 111 | + case Flags.Ticks: |
| 112 | + value = VerifyAndMakeDateOnly(new DateTime(Int64Serializer.Instance.Deserialize(context), DateTimeKind.Utc)); |
| 113 | + break; |
| 114 | + } |
| 115 | + }); |
| 116 | + break; |
| 117 | + |
| 118 | + case BsonType.Decimal128: |
| 119 | + value = VerifyAndMakeDateOnly(new DateTime(_converter.ToInt64(bsonReader.ReadDecimal128()), DateTimeKind.Utc)); |
| 120 | + break; |
| 121 | + |
| 122 | + case BsonType.Double: |
| 123 | + value = VerifyAndMakeDateOnly(new DateTime(_converter.ToInt64(bsonReader.ReadDouble()), DateTimeKind.Utc)); |
| 124 | + break; |
| 125 | + |
| 126 | + case BsonType.Int32: |
| 127 | + value = VerifyAndMakeDateOnly(new DateTime(bsonReader.ReadInt32(), DateTimeKind.Utc)); |
| 128 | + break; |
| 129 | + |
| 130 | + case BsonType.Int64: |
| 131 | + value = VerifyAndMakeDateOnly(new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc)); |
| 132 | + break; |
| 133 | + |
| 134 | + case BsonType.String: |
| 135 | + value = DateOnly.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd"); |
| 136 | + break; |
| 137 | + |
| 138 | + default: |
| 139 | + throw CreateCannotDeserializeFromBsonTypeException(bsonType); |
| 140 | + } |
| 141 | + |
| 142 | + return value; |
| 143 | + |
| 144 | + DateOnly VerifyAndMakeDateOnly(DateTime dt) |
| 145 | + { |
| 146 | + if (dt.TimeOfDay != TimeSpan.Zero) |
| 147 | + { |
| 148 | + throw new FormatException("Deserialized value has a non-zero time component."); |
| 149 | + } |
| 150 | + |
| 151 | + return DateOnly.FromDateTime(dt); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /// <inheritdoc/> |
| 156 | + public override bool Equals(object obj) |
| 157 | + { |
| 158 | + if (object.ReferenceEquals(obj, null)) { return false; } |
| 159 | + if (object.ReferenceEquals(this, obj)) { return true; } |
| 160 | + return |
| 161 | + base.Equals(obj) && |
| 162 | + obj is DateOnlySerializer other && |
| 163 | + _representation.Equals(other._representation); |
| 164 | + } |
| 165 | + |
| 166 | + /// <inheritdoc/> |
| 167 | + public override int GetHashCode() => 0; |
| 168 | + |
| 169 | + /// <inheritdoc /> |
| 170 | + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateOnly value) |
| 171 | + { |
| 172 | + var bsonWriter = context.Writer; |
| 173 | + |
| 174 | + var utcDateTime = value.ToDateTime(new TimeOnly(0), DateTimeKind.Utc); |
| 175 | + var millisecondsSinceEpoch = BsonUtils.ToMillisecondsSinceEpoch(utcDateTime); |
| 176 | + |
| 177 | + switch (_representation) |
| 178 | + { |
| 179 | + case BsonType.DateTime: |
| 180 | + bsonWriter.WriteDateTime(millisecondsSinceEpoch); |
| 181 | + break; |
| 182 | + |
| 183 | + case BsonType.Document: |
| 184 | + bsonWriter.WriteStartDocument(); |
| 185 | + bsonWriter.WriteDateTime("DateTime", millisecondsSinceEpoch); |
| 186 | + bsonWriter.WriteInt64("Ticks", utcDateTime.Ticks); |
| 187 | + bsonWriter.WriteEndDocument(); |
| 188 | + break; |
| 189 | + |
| 190 | + case BsonType.Int64: |
| 191 | + bsonWriter.WriteInt64(utcDateTime.Ticks); |
| 192 | + break; |
| 193 | + |
| 194 | + case BsonType.String: |
| 195 | + bsonWriter.WriteString(value.ToString("yyyy-MM-dd")); |
| 196 | + break; |
| 197 | + |
| 198 | + default: |
| 199 | + throw new BsonSerializationException($"'{_representation}' is not a valid DateOnly representation."); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /// <inheritdoc /> |
| 204 | + public DateOnlySerializer WithRepresentation(BsonType representation) |
| 205 | + { |
| 206 | + return representation == _representation ? this : new DateOnlySerializer(representation); |
| 207 | + } |
| 208 | + |
| 209 | + // explicit interface implementations |
| 210 | + IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation) |
| 211 | + { |
| 212 | + return WithRepresentation(representation); |
| 213 | + } |
| 214 | + } |
| 215 | +#endif |
| 216 | +} |
| 217 | + |
0 commit comments