Decimal128Serializer.cs 7.3 KB

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