Int32Serializer.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 Int32.
  24. /// </summary>
  25. public class Int32Serializer : StructSerializerBase<int>, IRepresentationConfigurable<Int32Serializer>, IRepresentationConverterConfigurable<Int32Serializer>
  26. {
  27. // private fields
  28. private readonly BsonType _representation;
  29. private readonly RepresentationConverter _converter;
  30. // constructors
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="Int32Serializer"/> class.
  33. /// </summary>
  34. public Int32Serializer()
  35. : this(BsonType.Int32)
  36. {
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="Int32Serializer"/> class.
  40. /// </summary>
  41. /// <param name="representation">The representation.</param>
  42. public Int32Serializer(BsonType representation)
  43. : this(representation, new RepresentationConverter(false, false))
  44. {
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="Int32Serializer"/> class.
  48. /// </summary>
  49. /// <param name="representation">The representation.</param>
  50. /// <param name="converter">The converter.</param>
  51. public Int32Serializer(BsonType representation, RepresentationConverter converter)
  52. {
  53. switch (representation)
  54. {
  55. case BsonType.Decimal128:
  56. case BsonType.Double:
  57. case BsonType.Int32:
  58. case BsonType.Int64:
  59. case BsonType.String:
  60. break;
  61. default:
  62. var message = string.Format("{0} is not a valid representation for an Int32Serializer.", representation);
  63. throw new ArgumentException(message);
  64. }
  65. _representation = representation;
  66. _converter = converter;
  67. }
  68. // public properties
  69. /// <summary>
  70. /// Gets the converter.
  71. /// </summary>
  72. /// <value>
  73. /// The converter.
  74. /// </value>
  75. public RepresentationConverter Converter
  76. {
  77. get { return _converter; }
  78. }
  79. /// <summary>
  80. /// Gets the representation.
  81. /// </summary>
  82. /// <value>
  83. /// The representation.
  84. /// </value>
  85. public BsonType Representation
  86. {
  87. get { return _representation; }
  88. }
  89. // public methods
  90. /// <summary>
  91. /// Deserializes a value.
  92. /// </summary>
  93. /// <param name="context">The deserialization context.</param>
  94. /// <param name="args">The deserialization args.</param>
  95. /// <returns>A deserialized value.</returns>
  96. public override int Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  97. {
  98. var bsonReader = context.Reader;
  99. var bsonType = bsonReader.GetCurrentBsonType();
  100. switch (bsonType)
  101. {
  102. case BsonType.Decimal128:
  103. return _converter.ToInt32(bsonReader.ReadDecimal128());
  104. case BsonType.Double:
  105. return _converter.ToInt32(bsonReader.ReadDouble());
  106. case BsonType.Int32:
  107. return bsonReader.ReadInt32();
  108. case BsonType.Int64:
  109. return _converter.ToInt32(bsonReader.ReadInt64());
  110. case BsonType.String:
  111. return JsonConvert.ToInt32(bsonReader.ReadString());
  112. default:
  113. throw CreateCannotDeserializeFromBsonTypeException(bsonType);
  114. }
  115. }
  116. /// <summary>
  117. /// Serializes a value.
  118. /// </summary>
  119. /// <param name="context">The serialization context.</param>
  120. /// <param name="args">The serialization args.</param>
  121. /// <param name="value">The object.</param>
  122. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, int value)
  123. {
  124. var bsonWriter = context.Writer;
  125. switch (_representation)
  126. {
  127. case BsonType.Decimal128:
  128. bsonWriter.WriteDecimal128(_converter.ToDecimal128(value));
  129. break;
  130. case BsonType.Double:
  131. bsonWriter.WriteDouble(_converter.ToDouble(value));
  132. break;
  133. case BsonType.Int32:
  134. bsonWriter.WriteInt32(value);
  135. break;
  136. case BsonType.Int64:
  137. bsonWriter.WriteInt64(_converter.ToInt64(value));
  138. break;
  139. case BsonType.String:
  140. bsonWriter.WriteString(JsonConvert.ToString(value));
  141. break;
  142. default:
  143. var message = string.Format("'{0}' is not a valid Int32 representation.", _representation);
  144. throw new BsonSerializationException(message);
  145. }
  146. }
  147. /// <summary>
  148. /// Returns a serializer that has been reconfigured with the specified item serializer.
  149. /// </summary>
  150. /// <param name="converter">The converter.</param>
  151. /// <returns>The reconfigured serializer.</returns>
  152. public Int32Serializer WithConverter(RepresentationConverter converter)
  153. {
  154. if (converter == _converter)
  155. {
  156. return this;
  157. }
  158. else
  159. {
  160. return new Int32Serializer(_representation, converter);
  161. }
  162. }
  163. /// <summary>
  164. /// Returns a serializer that has been reconfigured with the specified representation.
  165. /// </summary>
  166. /// <param name="representation">The representation.</param>
  167. /// <returns>The reconfigured serializer.</returns>
  168. public Int32Serializer WithRepresentation(BsonType representation)
  169. {
  170. if (representation == _representation)
  171. {
  172. return this;
  173. }
  174. else
  175. {
  176. return new Int32Serializer(representation, _converter);
  177. }
  178. }
  179. // explicit interface implementations
  180. IBsonSerializer IRepresentationConverterConfigurable.WithConverter(RepresentationConverter converter)
  181. {
  182. return WithConverter(converter);
  183. }
  184. IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation)
  185. {
  186. return WithRepresentation(representation);
  187. }
  188. }
  189. }