BsonDocumentSerializer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.Collections.Generic;
  17. using System.Linq;
  18. using MongoDB.Bson.IO;
  19. using MongoDB.Bson.Serialization.IdGenerators;
  20. namespace MongoDB.Bson.Serialization.Serializers
  21. {
  22. /// <summary>
  23. /// Represents a serializer for BsonDocuments.
  24. /// </summary>
  25. public class BsonDocumentSerializer : BsonValueSerializerBase<BsonDocument>, IBsonDocumentSerializer, IBsonIdProvider
  26. {
  27. // private static fields
  28. private static BsonDocumentSerializer __instance = new BsonDocumentSerializer();
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the BsonDocumentSerializer class.
  32. /// </summary>
  33. public BsonDocumentSerializer()
  34. : base(BsonType.Document)
  35. {
  36. }
  37. // public static properties
  38. /// <summary>
  39. /// Gets an instance of the BsonDocumentSerializer class.
  40. /// </summary>
  41. public static BsonDocumentSerializer Instance
  42. {
  43. get { return __instance; }
  44. }
  45. // public methods
  46. /// <summary>
  47. /// Deserializes a value.
  48. /// </summary>
  49. /// <param name="context">The deserialization context.</param>
  50. /// <param name="args">The deserialization args.</param>
  51. /// <returns>A deserialized value.</returns>
  52. protected override BsonDocument DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  53. {
  54. var bsonReader = context.Reader;
  55. bsonReader.ReadStartDocument();
  56. var document = new BsonDocument(allowDuplicateNames: context.AllowDuplicateElementNames);
  57. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  58. {
  59. var name = bsonReader.ReadName();
  60. var value = BsonValueSerializer.Instance.Deserialize(context);
  61. document.Add(name, value);
  62. }
  63. bsonReader.ReadEndDocument();
  64. return document;
  65. }
  66. /// <summary>
  67. /// Gets the document Id.
  68. /// </summary>
  69. /// <param name="document">The document.</param>
  70. /// <param name="id">The Id.</param>
  71. /// <param name="idNominalType">The nominal type of the Id.</param>
  72. /// <param name="idGenerator">The IdGenerator for the Id type.</param>
  73. /// <returns>True if the document has an Id.</returns>
  74. public bool GetDocumentId(
  75. object document,
  76. out object id,
  77. out Type idNominalType,
  78. out IIdGenerator idGenerator)
  79. {
  80. var bsonDocument = (BsonDocument)document;
  81. BsonValue idBsonValue;
  82. if (bsonDocument.TryGetValue("_id", out idBsonValue))
  83. {
  84. id = idBsonValue;
  85. idGenerator = BsonSerializer.LookupIdGenerator(id.GetType());
  86. if (idGenerator == null)
  87. {
  88. var idBinaryData = id as BsonBinaryData;
  89. if (idBinaryData != null && (idBinaryData.SubType == BsonBinarySubType.UuidLegacy || idBinaryData.SubType == BsonBinarySubType.UuidStandard))
  90. {
  91. idGenerator = BsonBinaryDataGuidGenerator.GetInstance(idBinaryData.GuidRepresentation);
  92. }
  93. }
  94. }
  95. else
  96. {
  97. id = null;
  98. idGenerator = BsonObjectIdGenerator.Instance;
  99. }
  100. idNominalType = typeof(BsonValue);
  101. return true;
  102. }
  103. /// <summary>
  104. /// Tries to get the serialization info for a member.
  105. /// </summary>
  106. /// <param name="memberName">Name of the member.</param>
  107. /// <param name="serializationInfo">The serialization information.</param>
  108. /// <returns>
  109. /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
  110. /// </returns>
  111. public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
  112. {
  113. serializationInfo = new BsonSerializationInfo(
  114. memberName,
  115. BsonValueSerializer.Instance,
  116. typeof(BsonValue));
  117. return true;
  118. }
  119. /// <summary>
  120. /// Serializes a value.
  121. /// </summary>
  122. /// <param name="context">The serialization context.</param>
  123. /// <param name="args">The serialization args.</param>
  124. /// <param name="value">The object.</param>
  125. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, BsonDocument value)
  126. {
  127. var bsonWriter = context.Writer;
  128. bsonWriter.WriteStartDocument();
  129. var alreadySerializedIndex = -1;
  130. if (args.SerializeIdFirst)
  131. {
  132. var idIndex = value.IndexOfName("_id");
  133. if (idIndex != -1)
  134. {
  135. bsonWriter.WriteName("_id");
  136. BsonValueSerializer.Instance.Serialize(context, value[idIndex]);
  137. alreadySerializedIndex = idIndex;
  138. }
  139. }
  140. var elementCount = value.ElementCount;
  141. for (var index = 0; index < elementCount; index++)
  142. {
  143. if (index == alreadySerializedIndex)
  144. {
  145. continue;
  146. }
  147. var element = value.GetElement(index);
  148. bsonWriter.WriteName(element.Name);
  149. BsonValueSerializer.Instance.Serialize(context, element.Value);
  150. }
  151. bsonWriter.WriteEndDocument();
  152. }
  153. /// <summary>
  154. /// Sets the document Id.
  155. /// </summary>
  156. /// <param name="document">The document.</param>
  157. /// <param name="id">The Id.</param>
  158. public void SetDocumentId(object document, object id)
  159. {
  160. if (document == null)
  161. {
  162. throw new ArgumentNullException("document");
  163. }
  164. if (id == null)
  165. {
  166. throw new ArgumentNullException("id");
  167. }
  168. var bsonDocument = (BsonDocument)document;
  169. var idBsonValue = id as BsonValue;
  170. if (idBsonValue == null)
  171. {
  172. idBsonValue = BsonValue.Create(id); // be helpful and provide automatic conversion to BsonValue if necessary
  173. }
  174. var idIndex = bsonDocument.IndexOfName("_id");
  175. if (idIndex != -1)
  176. {
  177. bsonDocument[idIndex] = idBsonValue;
  178. }
  179. else
  180. {
  181. bsonDocument.InsertAt(0, new BsonElement("_id", idBsonValue));
  182. }
  183. }
  184. }
  185. }