CharSerializer.cs 5.1 KB

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