BsonDateTimeOptionsAttribute.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. using System;
  16. using MongoDB.Bson.Serialization.Serializers;
  17. namespace MongoDB.Bson.Serialization.Attributes
  18. {
  19. /// <summary>
  20. /// Specifies serialization options for a DateTime field or property.
  21. /// </summary>
  22. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
  23. public class BsonDateTimeOptionsAttribute : BsonSerializationOptionsAttribute
  24. {
  25. // private fields
  26. private bool _dateOnly = false;
  27. private DateTimeKind _kind = DateTimeKind.Utc;
  28. private BsonType _representation = BsonType.DateTime;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the BsonDateTimeOptionsAttribute class.
  32. /// </summary>
  33. public BsonDateTimeOptionsAttribute()
  34. {
  35. }
  36. // public properties
  37. /// <summary>
  38. /// Gets or sets whether the DateTime consists of a Date only.
  39. /// </summary>
  40. public bool DateOnly
  41. {
  42. get { return _dateOnly; }
  43. set { _dateOnly = value; }
  44. }
  45. /// <summary>
  46. /// Gets or sets the DateTimeKind (Local, Unspecified or Utc).
  47. /// </summary>
  48. public DateTimeKind Kind
  49. {
  50. get { return _kind; }
  51. set { _kind = value; }
  52. }
  53. /// <summary>
  54. /// Gets or sets the external representation.
  55. /// </summary>
  56. public BsonType Representation
  57. {
  58. get { return _representation; }
  59. set { _representation = value; }
  60. }
  61. // protected methods
  62. /// <summary>
  63. /// Reconfigures the specified serializer by applying this attribute to it.
  64. /// </summary>
  65. /// <param name="serializer">The serializer.</param>
  66. /// <returns>A reconfigured serializer.</returns>
  67. protected override IBsonSerializer Apply(IBsonSerializer serializer)
  68. {
  69. var dateTimeSerializer = serializer as DateTimeSerializer;
  70. if (dateTimeSerializer != null)
  71. {
  72. if (_dateOnly)
  73. {
  74. return dateTimeSerializer.WithDateOnly(_dateOnly, _representation);
  75. }
  76. else
  77. {
  78. return dateTimeSerializer.WithKind(_kind, _representation);
  79. }
  80. }
  81. return base.Apply(serializer);
  82. }
  83. }
  84. }