DoubleSerializer.cs 7.3 KB

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