NullableGenericSerializer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.Attributes;
  18. namespace MongoDB.Bson.Serialization.Serializers
  19. {
  20. /// <summary>
  21. /// Represents a serializer for nullable values.
  22. /// </summary>
  23. /// <typeparam name="T">The underlying type.</typeparam>
  24. public class NullableSerializer<T> :
  25. SerializerBase<Nullable<T>>,
  26. IChildSerializerConfigurable
  27. where T : struct
  28. {
  29. // private fields
  30. private Lazy<IBsonSerializer<T>> _lazySerializer;
  31. // constructors
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="NullableSerializer{T}"/> class.
  34. /// </summary>
  35. public NullableSerializer()
  36. : this(BsonSerializer.SerializerRegistry)
  37. {
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="NullableSerializer{T}"/> class.
  41. /// </summary>
  42. /// <param name="serializer">The serializer.</param>
  43. public NullableSerializer(IBsonSerializer<T> serializer)
  44. {
  45. if (serializer == null)
  46. {
  47. throw new ArgumentNullException("serializer");
  48. }
  49. _lazySerializer = new Lazy<IBsonSerializer<T>>(() => serializer);
  50. }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="NullableSerializer{T}" /> class.
  53. /// </summary>
  54. /// <param name="serializerRegistry">The serializer registry.</param>
  55. public NullableSerializer(IBsonSerializerRegistry serializerRegistry)
  56. {
  57. if (serializerRegistry == null)
  58. {
  59. throw new ArgumentNullException("serializerRegistry");
  60. }
  61. _lazySerializer = new Lazy<IBsonSerializer<T>>(() => serializerRegistry.GetSerializer<T>());
  62. }
  63. // public methods
  64. /// <summary>
  65. /// Deserializes a value.
  66. /// </summary>
  67. /// <param name="context">The deserialization context.</param>
  68. /// <param name="args">The deserialization args.</param>
  69. /// <returns>A deserialized value.</returns>
  70. public override T? Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  71. {
  72. var bsonReader = context.Reader;
  73. var bsonType = bsonReader.GetCurrentBsonType();
  74. if (bsonType == BsonType.Null)
  75. {
  76. bsonReader.ReadNull();
  77. return null;
  78. }
  79. else
  80. {
  81. return _lazySerializer.Value.Deserialize(context);
  82. }
  83. }
  84. /// <summary>
  85. /// Serializes a value.
  86. /// </summary>
  87. /// <param name="context">The serialization context.</param>
  88. /// <param name="args">The serialization args.</param>
  89. /// <param name="value">The object.</param>
  90. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T? value)
  91. {
  92. var bsonWriter = context.Writer;
  93. if (value == null)
  94. {
  95. bsonWriter.WriteNull();
  96. }
  97. else
  98. {
  99. _lazySerializer.Value.Serialize(context, value.Value);
  100. }
  101. }
  102. /// <summary>
  103. /// Returns a serializer that has been reconfigured with the specified serializer.
  104. /// </summary>
  105. /// <param name="serializer">The serializer.</param>
  106. /// <returns>
  107. /// The reconfigured serializer.
  108. /// </returns>
  109. public NullableSerializer<T> WithSerializer(IBsonSerializer<T> serializer)
  110. {
  111. if (serializer == _lazySerializer.Value)
  112. {
  113. return this;
  114. }
  115. else
  116. {
  117. return new NullableSerializer<T>(serializer);
  118. }
  119. }
  120. // explicit interface implementations
  121. IBsonSerializer IChildSerializerConfigurable.ChildSerializer
  122. {
  123. get { return _lazySerializer.Value; }
  124. }
  125. IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
  126. {
  127. return WithSerializer((IBsonSerializer<T>)childSerializer);
  128. }
  129. }
  130. }