VersionSerializer.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.IO;
  17. namespace MongoDB.Bson.Serialization.Serializers
  18. {
  19. /// <summary>
  20. /// Represents a serializer for Versions.
  21. /// </summary>
  22. public class VersionSerializer : SealedClassSerializerBase<Version>, IRepresentationConfigurable<VersionSerializer>
  23. {
  24. // private constants
  25. private static class Flags
  26. {
  27. public const long Major = 1;
  28. public const long Minor = 2;
  29. public const long Build = 4;
  30. public const long Revision = 8;
  31. public const long All = Major | Minor | Build | Revision;
  32. public const long MajorMinor = Major | Minor;
  33. public const long MajorMinorBuild = Major | Minor | Build;
  34. }
  35. // private fields
  36. private readonly SerializerHelper _helper;
  37. private readonly Int32Serializer _int32Serializer = new Int32Serializer();
  38. private readonly BsonType _representation;
  39. // constructors
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="VersionSerializer"/> class.
  42. /// </summary>
  43. public VersionSerializer()
  44. : this(BsonType.String)
  45. {
  46. }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="VersionSerializer"/> class.
  49. /// </summary>
  50. /// <param name="representation">The representation.</param>
  51. public VersionSerializer(BsonType representation)
  52. {
  53. switch (representation)
  54. {
  55. case BsonType.Document:
  56. case BsonType.String:
  57. break;
  58. default:
  59. var message = string.Format("{0} is not a valid representation for a VersionSerializer.", representation);
  60. throw new ArgumentException(message);
  61. }
  62. _representation = representation;
  63. _helper = new SerializerHelper
  64. (
  65. new SerializerHelper.Member("Major", Flags.Major),
  66. new SerializerHelper.Member("Minor", Flags.Minor),
  67. new SerializerHelper.Member("Build", Flags.Build, isOptional: true),
  68. new SerializerHelper.Member("Revision", Flags.Revision, isOptional: true)
  69. );
  70. }
  71. // public properties
  72. /// <summary>
  73. /// Gets the representation.
  74. /// </summary>
  75. /// <value>
  76. /// The representation.
  77. /// </value>
  78. public BsonType Representation
  79. {
  80. get { return _representation; }
  81. }
  82. // public methods
  83. /// <summary>
  84. /// Deserializes a value.
  85. /// </summary>
  86. /// <param name="context">The deserialization context.</param>
  87. /// <param name="args">The deserialization args.</param>
  88. /// <returns>A deserialized value.</returns>
  89. protected override Version DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  90. {
  91. var bsonReader = context.Reader;
  92. BsonType bsonType = bsonReader.GetCurrentBsonType();
  93. switch (bsonType)
  94. {
  95. case BsonType.Document:
  96. int major = 0, minor = 0, build = 0, revision = 0;
  97. var foundMemberFlags = _helper.DeserializeMembers(context, (elementName, flag) =>
  98. {
  99. switch (flag)
  100. {
  101. case Flags.Major: major = _int32Serializer.Deserialize(context); break;
  102. case Flags.Minor: minor = _int32Serializer.Deserialize(context); break;
  103. case Flags.Build: build = _int32Serializer.Deserialize(context); break;
  104. case Flags.Revision: revision = _int32Serializer.Deserialize(context); break;
  105. }
  106. });
  107. switch (foundMemberFlags)
  108. {
  109. case Flags.MajorMinor: return new Version(major, minor);
  110. case Flags.MajorMinorBuild: return new Version(major, minor, build);
  111. case Flags.All: return new Version(major, minor, build, revision);
  112. default: throw new BsonInternalException();
  113. }
  114. case BsonType.String:
  115. return new Version(bsonReader.ReadString());
  116. default:
  117. throw CreateCannotDeserializeFromBsonTypeException(bsonType);
  118. }
  119. }
  120. /// <summary>
  121. /// Serializes a value.
  122. /// </summary>
  123. /// <param name="context">The serialization context.</param>
  124. /// <param name="args">The serialization args.</param>
  125. /// <param name="value">The object.</param>
  126. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, Version value)
  127. {
  128. var bsonWriter = context.Writer;
  129. switch (_representation)
  130. {
  131. case BsonType.Document:
  132. bsonWriter.WriteStartDocument();
  133. bsonWriter.WriteInt32("Major", value.Major);
  134. bsonWriter.WriteInt32("Minor", value.Minor);
  135. if (value.Build != -1)
  136. {
  137. bsonWriter.WriteInt32("Build", value.Build);
  138. if (value.Revision != -1)
  139. {
  140. bsonWriter.WriteInt32("Revision", value.Revision);
  141. }
  142. }
  143. bsonWriter.WriteEndDocument();
  144. break;
  145. case BsonType.String:
  146. bsonWriter.WriteString(value.ToString());
  147. break;
  148. default:
  149. var message = string.Format("'{0}' is not a valid Version representation.", _representation);
  150. throw new BsonSerializationException(message);
  151. }
  152. }
  153. /// <summary>
  154. /// Returns a serializer that has been reconfigured with the specified representation.
  155. /// </summary>
  156. /// <param name="representation">The representation.</param>
  157. /// <returns>The reconfigured serializer.</returns>
  158. public VersionSerializer WithRepresentation(BsonType representation)
  159. {
  160. if (representation == _representation)
  161. {
  162. return this;
  163. }
  164. else
  165. {
  166. return new VersionSerializer(representation);
  167. }
  168. }
  169. // explicit interface implementations
  170. IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation)
  171. {
  172. return WithRepresentation(representation);
  173. }
  174. }
  175. }