SByteSerializer.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.Globalization;
  17. using System.IO;
  18. using MongoDB.Bson.IO;
  19. using MongoDB.Bson.Serialization.Attributes;
  20. using MongoDB.Bson.Serialization.Options;
  21. namespace MongoDB.Bson.Serialization.Serializers
  22. {
  23. /// <summary>
  24. /// Represents a serializer for SBytes.
  25. /// </summary>
  26. [CLSCompliant(false)]
  27. public class SByteSerializer : StructSerializerBase<sbyte>, IRepresentationConfigurable<SByteSerializer>
  28. {
  29. // private fields
  30. private readonly BsonType _representation;
  31. // constructors
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="SByteSerializer"/> class.
  34. /// </summary>
  35. public SByteSerializer()
  36. : this(BsonType.Int32)
  37. {
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="SByteSerializer"/> class.
  41. /// </summary>
  42. /// <param name="representation">The representation.</param>
  43. public SByteSerializer(BsonType representation)
  44. {
  45. switch (representation)
  46. {
  47. case BsonType.Binary:
  48. case BsonType.Int32:
  49. case BsonType.Int64:
  50. case BsonType.String:
  51. break;
  52. default:
  53. var message = string.Format("{0} is not a valid representation for an SByteSerializer.", representation);
  54. throw new ArgumentException(message);
  55. }
  56. _representation = representation;
  57. }
  58. // public properties
  59. /// <summary>
  60. /// Gets the representation.
  61. /// </summary>
  62. /// <value>
  63. /// The representation.
  64. /// </value>
  65. public BsonType Representation
  66. {
  67. get { return _representation; }
  68. }
  69. // public methods
  70. /// <summary>
  71. /// Deserializes a value.
  72. /// </summary>
  73. /// <param name="context">The deserialization context.</param>
  74. /// <param name="args">The deserialization args.</param>
  75. /// <returns>A deserialized value.</returns>
  76. public override sbyte Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  77. {
  78. var bsonReader = context.Reader;
  79. sbyte value;
  80. var lostData = false;
  81. var bsonType = bsonReader.GetCurrentBsonType();
  82. switch (bsonType)
  83. {
  84. case BsonType.Binary:
  85. var bytes = bsonReader.ReadBytes();
  86. if (bytes.Length != 1)
  87. {
  88. throw new FormatException("Binary data for SByte must be exactly one byte long.");
  89. }
  90. value = (sbyte)bytes[0];
  91. break;
  92. case BsonType.Int32:
  93. var int32Value = bsonReader.ReadInt32();
  94. value = (sbyte)int32Value;
  95. lostData = (int)value != int32Value;
  96. break;
  97. case BsonType.Int64:
  98. var int64Value = bsonReader.ReadInt64();
  99. value = (sbyte)int64Value;
  100. lostData = (int)value != int64Value;
  101. break;
  102. case BsonType.String:
  103. var s = bsonReader.ReadString();
  104. if (s.Length == 1)
  105. {
  106. s = "0" + s;
  107. }
  108. value = (sbyte)byte.Parse(s, NumberStyles.HexNumber);
  109. break;
  110. default:
  111. throw CreateCannotDeserializeFromBsonTypeException(bsonType);
  112. }
  113. if (lostData)
  114. {
  115. var message = string.Format("Data loss occurred when trying to convert from {0} to SByte.", bsonType);
  116. throw new FormatException(message);
  117. }
  118. return value;
  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. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, sbyte value)
  127. {
  128. var bsonWriter = context.Writer;
  129. switch (_representation)
  130. {
  131. case BsonType.Binary:
  132. bsonWriter.WriteBytes(new byte[] { (byte)value });
  133. break;
  134. case BsonType.Int32:
  135. bsonWriter.WriteInt32(value);
  136. break;
  137. case BsonType.Int64:
  138. bsonWriter.WriteInt64(value);
  139. break;
  140. case BsonType.String:
  141. bsonWriter.WriteString(string.Format("{0:x2}", (byte)value));
  142. break;
  143. default:
  144. var message = string.Format("'{0}' is not a valid SByte representation.", _representation);
  145. throw new BsonSerializationException(message);
  146. }
  147. }
  148. /// <summary>
  149. /// Returns a serializer that has been reconfigured with the specified representation.
  150. /// </summary>
  151. /// <param name="representation">The representation.</param>
  152. /// <returns>The reconfigured serializer.</returns>
  153. public SByteSerializer WithRepresentation(BsonType representation)
  154. {
  155. if (representation == _representation)
  156. {
  157. return this;
  158. }
  159. else
  160. {
  161. return new SByteSerializer(representation);
  162. }
  163. }
  164. // explicit interface implementations
  165. IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation)
  166. {
  167. return WithRepresentation(representation);
  168. }
  169. }
  170. }