BooleanSerializer.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 Booleans.
  24. /// </summary>
  25. public class BooleanSerializer : StructSerializerBase<bool>, IRepresentationConfigurable<BooleanSerializer>
  26. {
  27. // private fields
  28. private readonly BsonType _representation;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="BooleanSerializer"/> class.
  32. /// </summary>
  33. public BooleanSerializer()
  34. : this(BsonType.Boolean)
  35. {
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="BooleanSerializer"/> class.
  39. /// </summary>
  40. /// <param name="representation">The representation.</param>
  41. public BooleanSerializer(BsonType representation)
  42. {
  43. switch (representation)
  44. {
  45. case BsonType.Boolean:
  46. case BsonType.Decimal128:
  47. case BsonType.Double:
  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 a BooleanSerializer.", 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 bool Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  77. {
  78. var bsonReader = context.Reader;
  79. var bsonType = bsonReader.GetCurrentBsonType();
  80. switch (bsonType)
  81. {
  82. case BsonType.Boolean:
  83. return bsonReader.ReadBoolean();
  84. case BsonType.Decimal128:
  85. return bsonReader.ReadDecimal128() != Decimal128.Zero;
  86. case BsonType.Double:
  87. return bsonReader.ReadDouble() != 0.0;
  88. case BsonType.Int32:
  89. return bsonReader.ReadInt32() != 0;
  90. case BsonType.Int64:
  91. return bsonReader.ReadInt64() != 0;
  92. case BsonType.Null:
  93. bsonReader.ReadNull();
  94. return false;
  95. case BsonType.String:
  96. return JsonConvert.ToBoolean(bsonReader.ReadString().ToLower());
  97. default:
  98. throw CreateCannotDeserializeFromBsonTypeException(bsonType);
  99. }
  100. }
  101. /// <summary>
  102. /// Serializes a value.
  103. /// </summary>
  104. /// <param name="context">The serialization context.</param>
  105. /// <param name="args">The serialization args.</param>
  106. /// <param name="value">The object.</param>
  107. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, bool value)
  108. {
  109. var bsonWriter = context.Writer;
  110. switch (_representation)
  111. {
  112. case BsonType.Boolean:
  113. bsonWriter.WriteBoolean(value);
  114. break;
  115. case BsonType.Decimal128:
  116. bsonWriter.WriteDecimal128(value ? Decimal128.One : Decimal128.Zero);
  117. break;
  118. case BsonType.Double:
  119. bsonWriter.WriteDouble(value ? 1.0 : 0.0);
  120. break;
  121. case BsonType.Int32:
  122. bsonWriter.WriteInt32(value ? 1 : 0);
  123. break;
  124. case BsonType.Int64:
  125. bsonWriter.WriteInt64(value ? 1 : 0);
  126. break;
  127. case BsonType.String:
  128. bsonWriter.WriteString(JsonConvert.ToString(value));
  129. break;
  130. default:
  131. var message = string.Format("'{0}' is not a valid Boolean representation.", _representation);
  132. throw new BsonSerializationException(message);
  133. }
  134. }
  135. /// <summary>
  136. /// Returns a serializer that has been reconfigured with the specified representation.
  137. /// </summary>
  138. /// <param name="representation">The representation.</param>
  139. /// <returns>The reconfigured serializer.</returns>
  140. public BooleanSerializer WithRepresentation(BsonType representation)
  141. {
  142. if (representation == _representation)
  143. {
  144. return this;
  145. }
  146. else
  147. {
  148. return new BooleanSerializer(representation);
  149. }
  150. }
  151. // explicit interface implementations
  152. IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation)
  153. {
  154. return WithRepresentation(representation);
  155. }
  156. }
  157. }