BsonValueCSharpNullSerializer.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.Collections.Generic;
  16. using MongoDB.Bson.IO;
  17. namespace MongoDB.Bson.Serialization.Serializers
  18. {
  19. /// <summary>
  20. /// Represents a serializer for a BsonValue that can round trip C# null.
  21. /// </summary>
  22. /// <typeparam name="TBsonValue">The type of the BsonValue.</typeparam>
  23. public class BsonValueCSharpNullSerializer<TBsonValue> : SerializerBase<TBsonValue> where TBsonValue : BsonValue
  24. {
  25. // private fields
  26. private readonly IBsonSerializer<TBsonValue> _wrappedSerializer;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="BsonValueCSharpNullSerializer{TBsonValue}"/> class.
  30. /// </summary>
  31. /// <param name="wrappedSerializer">The wrapped serializer.</param>
  32. public BsonValueCSharpNullSerializer(IBsonSerializer<TBsonValue> wrappedSerializer)
  33. {
  34. _wrappedSerializer = wrappedSerializer;
  35. }
  36. // public methods
  37. /// <summary>
  38. /// Deserializes a value.
  39. /// </summary>
  40. /// <param name="context">The deserialization context.</param>
  41. /// <param name="args">The deserialization args.</param>
  42. /// <returns>A deserialized value.</returns>
  43. public override TBsonValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  44. {
  45. var bsonReader = context.Reader;
  46. var bsonType = bsonReader.GetCurrentBsonType();
  47. if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader))
  48. {
  49. // if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null
  50. return null;
  51. }
  52. // handle BSON null for backward compatibility with existing data (new data would have _csharpnull)
  53. if (bsonType == BsonType.Null && (args.NominalType != typeof(BsonValue) && args.NominalType != typeof(BsonNull)))
  54. {
  55. bsonReader.ReadNull();
  56. return null;
  57. }
  58. return _wrappedSerializer.Deserialize(context);
  59. }
  60. /// <summary>
  61. /// Serializes a value.
  62. /// </summary>
  63. /// <param name="context">The serialization context.</param>
  64. /// <param name="args">The serialization args.</param>
  65. /// <param name="value">The object.</param>
  66. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TBsonValue value)
  67. {
  68. if (value == null)
  69. {
  70. var bsonWriter = context.Writer;
  71. bsonWriter.WriteStartDocument();
  72. bsonWriter.WriteBoolean("_csharpnull", true);
  73. bsonWriter.WriteEndDocument();
  74. }
  75. else
  76. {
  77. _wrappedSerializer.Serialize(context, value);
  78. }
  79. }
  80. // private methods
  81. private bool IsCSharpNullRepresentation(IBsonReader bsonReader)
  82. {
  83. var bookmark = bsonReader.GetBookmark();
  84. bsonReader.ReadStartDocument();
  85. var bsonType = bsonReader.ReadBsonType();
  86. if (bsonType == BsonType.Boolean)
  87. {
  88. var name = bsonReader.ReadName();
  89. if (name == "_csharpnull" || name == "$csharpnull")
  90. {
  91. var value = bsonReader.ReadBoolean();
  92. if (value)
  93. {
  94. bsonType = bsonReader.ReadBsonType();
  95. if (bsonType == BsonType.EndOfDocument)
  96. {
  97. bsonReader.ReadEndDocument();
  98. return true;
  99. }
  100. }
  101. }
  102. }
  103. bsonReader.ReturnToBookmark(bookmark);
  104. return false;
  105. }
  106. }
  107. /// <summary>
  108. /// Represents a serializer for a BsonValue that can round trip C# null and implements IBsonArraySerializer and IBsonDocumentSerializer.
  109. /// </summary>
  110. /// <typeparam name="TBsonValue">The type of the bson value.</typeparam>
  111. public class BsonValueCSharpNullArrayAndDocumentSerializer<TBsonValue> : BsonValueCSharpNullSerializer<TBsonValue>, IBsonArraySerializer, IBsonDocumentSerializer
  112. where TBsonValue : BsonValue
  113. {
  114. // constructors
  115. /// <summary>
  116. /// Initializes a new instance of the <see cref="BsonValueCSharpNullArrayAndDocumentSerializer{TBsonValue}"/> class.
  117. /// </summary>
  118. /// <param name="wrappedSerializer">The wrapped serializer.</param>
  119. public BsonValueCSharpNullArrayAndDocumentSerializer(IBsonSerializer<TBsonValue> wrappedSerializer)
  120. : base(wrappedSerializer)
  121. {
  122. }
  123. /// <summary>
  124. /// Tries to get the serialization info for the individual items of the array.
  125. /// </summary>
  126. /// <param name="serializationInfo">The serialization information.</param>
  127. /// <returns>
  128. /// The serialization info for the items.
  129. /// </returns>
  130. public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
  131. {
  132. return BsonValueSerializer.Instance.TryGetItemSerializationInfo(out serializationInfo);
  133. }
  134. /// <summary>
  135. /// Tries to get the serialization info for a member.
  136. /// </summary>
  137. /// <param name="memberName">Name of the member.</param>
  138. /// <param name="serializationInfo">The serialization information.</param>
  139. /// <returns>
  140. /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
  141. /// </returns>
  142. public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
  143. {
  144. return BsonValueSerializer.Instance.TryGetMemberSerializationInfo(memberName, out serializationInfo);
  145. }
  146. }
  147. /// <summary>
  148. /// Represents a serializer for a BsonValue that can round trip C# null and implements IBsonArraySerializer.
  149. /// </summary>
  150. /// <typeparam name="TBsonValue">The type of the bson value.</typeparam>
  151. public class BsonValueCSharpNullArraySerializer<TBsonValue> : BsonValueCSharpNullSerializer<TBsonValue>, IBsonArraySerializer
  152. where TBsonValue : BsonValue
  153. {
  154. // constructors
  155. /// <summary>
  156. /// Initializes a new instance of the <see cref="BsonValueCSharpNullArraySerializer{TBsonValue}"/> class.
  157. /// </summary>
  158. /// <param name="wrappedSerializer">The wrapped serializer.</param>
  159. public BsonValueCSharpNullArraySerializer(IBsonSerializer<TBsonValue> wrappedSerializer)
  160. : base(wrappedSerializer)
  161. {
  162. }
  163. /// <summary>
  164. /// Tries to get the serialization info for the individual items of the array.
  165. /// </summary>
  166. /// <param name="serializationInfo">The serialization information.</param>
  167. /// <returns>
  168. /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
  169. /// </returns>
  170. public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
  171. {
  172. return BsonValueSerializer.Instance.TryGetItemSerializationInfo(out serializationInfo);
  173. }
  174. }
  175. /// <summary>
  176. /// Represents a serializer for a BsonValue that can round trip C# null and implements IBsonDocumentSerializer.
  177. /// </summary>
  178. /// <typeparam name="TBsonValue">The type of the bson value.</typeparam>
  179. public class BsonValueCSharpNullDocumentSerializer<TBsonValue> : BsonValueCSharpNullSerializer<TBsonValue>, IBsonDocumentSerializer
  180. where TBsonValue : BsonValue
  181. {
  182. // constructors
  183. /// <summary>
  184. /// Initializes a new instance of the <see cref="BsonValueCSharpNullDocumentSerializer{TBsonValue}"/> class.
  185. /// </summary>
  186. /// <param name="wrappedSerializer">The wrapped serializer.</param>
  187. public BsonValueCSharpNullDocumentSerializer(IBsonSerializer<TBsonValue> wrappedSerializer)
  188. : base(wrappedSerializer)
  189. {
  190. }
  191. /// <summary>
  192. /// Tries to get the serialization info for a member.
  193. /// </summary>
  194. /// <param name="memberName">Name of the member.</param>
  195. /// <param name="serializationInfo">The serialization information.</param>
  196. /// <returns>
  197. /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
  198. /// </returns>
  199. public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
  200. {
  201. return BsonValueSerializer.Instance.TryGetMemberSerializationInfo(memberName, out serializationInfo);
  202. }
  203. }
  204. }