StringSerializer.cs 5.2 KB

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