ObjectIdSerializer.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 System.IO;
  17. using MongoDB.Bson.IO;
  18. using MongoDB.Bson.Serialization.Attributes;
  19. using MongoDB.Bson.Serialization.Options;
  20. namespace MongoDB.Bson.Serialization.Serializers
  21. {
  22. /// <summary>
  23. /// Represents a serializer for ObjectIds.
  24. /// </summary>
  25. public class ObjectIdSerializer : StructSerializerBase<ObjectId>, IRepresentationConfigurable<ObjectIdSerializer>
  26. {
  27. // private fields
  28. private readonly BsonType _representation;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="ObjectIdSerializer"/> class.
  32. /// </summary>
  33. public ObjectIdSerializer()
  34. : this(BsonType.ObjectId)
  35. {
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="ObjectIdSerializer"/> class.
  39. /// </summary>
  40. /// <param name="representation">The representation.</param>
  41. public ObjectIdSerializer(BsonType representation)
  42. {
  43. switch (representation)
  44. {
  45. case BsonType.ObjectId:
  46. case BsonType.String:
  47. break;
  48. default:
  49. var message = string.Format("{0} is not a valid representation for an ObjectIdSerializer.", representation);
  50. throw new ArgumentException(message);
  51. }
  52. _representation = representation;
  53. }
  54. // public properties
  55. /// <summary>
  56. /// Gets the representation.
  57. /// </summary>
  58. /// <value>
  59. /// The representation.
  60. /// </value>
  61. public BsonType Representation
  62. {
  63. get { return _representation; }
  64. }
  65. // public methods
  66. /// <summary>
  67. /// Deserializes a value.
  68. /// </summary>
  69. /// <param name="context">The deserialization context.</param>
  70. /// <param name="args">The deserialization args.</param>
  71. /// <returns>A deserialized value.</returns>
  72. public override ObjectId Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  73. {
  74. var bsonReader = context.Reader;
  75. BsonType bsonType = bsonReader.GetCurrentBsonType();
  76. switch (bsonType)
  77. {
  78. case BsonType.ObjectId:
  79. return bsonReader.ReadObjectId();
  80. case BsonType.String:
  81. return ObjectId.Parse(bsonReader.ReadString());
  82. default:
  83. throw CreateCannotDeserializeFromBsonTypeException(bsonType);
  84. }
  85. }
  86. /// <summary>
  87. /// Serializes a value.
  88. /// </summary>
  89. /// <param name="context">The serialization context.</param>
  90. /// <param name="args">The serialization args.</param>
  91. /// <param name="value">The object.</param>
  92. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, ObjectId value)
  93. {
  94. var bsonWriter = context.Writer;
  95. switch (_representation)
  96. {
  97. case BsonType.ObjectId:
  98. bsonWriter.WriteObjectId(value);
  99. break;
  100. case BsonType.String:
  101. bsonWriter.WriteString(value.ToString());
  102. break;
  103. default:
  104. var message = string.Format("'{0}' is not a valid ObjectId representation.", _representation);
  105. throw new BsonSerializationException(message);
  106. }
  107. }
  108. /// <summary>
  109. /// Returns a serializer that has been reconfigured with the specified representation.
  110. /// </summary>
  111. /// <param name="representation">The representation.</param>
  112. /// <returns>The reconfigured serializer.</returns>
  113. public ObjectIdSerializer WithRepresentation(BsonType representation)
  114. {
  115. if (representation == _representation)
  116. {
  117. return this;
  118. }
  119. else
  120. {
  121. return new ObjectIdSerializer(representation);
  122. }
  123. }
  124. // explicit interface implementations
  125. IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation)
  126. {
  127. return WithRepresentation(representation);
  128. }
  129. }
  130. }