BsonSerializationInfo.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright 2010-2015 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;
  17. using MongoDB.Bson.IO;
  18. namespace MongoDB.Bson.Serialization
  19. {
  20. /// <summary>
  21. /// Represents the information needed to serialize a member.
  22. /// </summary>
  23. public class BsonSerializationInfo
  24. {
  25. // private fields
  26. private string _elementName;
  27. private IBsonSerializer _serializer;
  28. private Type _nominalType;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the BsonSerializationInfo class.
  32. /// </summary>
  33. /// <param name="elementName">The element name.</param>
  34. /// <param name="serializer">The serializer.</param>
  35. /// <param name="nominalType">The nominal type.</param>
  36. public BsonSerializationInfo(string elementName, IBsonSerializer serializer, Type nominalType)
  37. {
  38. _elementName = elementName;
  39. _serializer = serializer;
  40. _nominalType = nominalType;
  41. }
  42. // public properties
  43. /// <summary>
  44. /// Gets or sets the dotted element name.
  45. /// </summary>
  46. public string ElementName
  47. {
  48. get { return _elementName; }
  49. }
  50. /// <summary>
  51. /// Gets or sets the serializer.
  52. /// </summary>
  53. public IBsonSerializer Serializer
  54. {
  55. get { return _serializer; }
  56. }
  57. /// <summary>
  58. /// Gets or sets the nominal type.
  59. /// </summary>
  60. public Type NominalType
  61. {
  62. get { return _nominalType; }
  63. }
  64. /// <summary>
  65. /// Deserializes the value.
  66. /// </summary>
  67. /// <param name="value">The value.</param>
  68. /// <returns>A deserialized value.</returns>
  69. public object DeserializeValue(BsonValue value)
  70. {
  71. var tempDocument = new BsonDocument("value", value);
  72. using (var reader = new BsonDocumentReader(tempDocument))
  73. {
  74. var context = BsonDeserializationContext.CreateRoot(reader);
  75. reader.ReadStartDocument();
  76. reader.ReadName("value");
  77. var deserializedValue = _serializer.Deserialize(context);
  78. reader.ReadEndDocument();
  79. return deserializedValue;
  80. }
  81. }
  82. /// <summary>
  83. /// Merges the new BsonSerializationInfo by taking its properties and concatenating its ElementName.
  84. /// </summary>
  85. /// <param name="newSerializationInfo">The new info.</param>
  86. /// <returns>A new BsonSerializationInfo.</returns>
  87. public BsonSerializationInfo Merge(BsonSerializationInfo newSerializationInfo)
  88. {
  89. string elementName = null;
  90. if (_elementName != null && newSerializationInfo._elementName != null)
  91. {
  92. elementName = _elementName + "." + newSerializationInfo._elementName;
  93. }
  94. else if (_elementName != null)
  95. {
  96. elementName = _elementName;
  97. }
  98. else if (newSerializationInfo._elementName != null)
  99. {
  100. elementName = newSerializationInfo._elementName;
  101. }
  102. return new BsonSerializationInfo(
  103. elementName,
  104. newSerializationInfo._serializer,
  105. newSerializationInfo._nominalType);
  106. }
  107. /// <summary>
  108. /// Serializes the value.
  109. /// </summary>
  110. /// <param name="value">The value.</param>
  111. /// <returns>The serialized value.</returns>
  112. public BsonValue SerializeValue(object value)
  113. {
  114. var tempDocument = new BsonDocument();
  115. using (var bsonWriter = new BsonDocumentWriter(tempDocument))
  116. {
  117. var context = BsonSerializationContext.CreateRoot(bsonWriter);
  118. bsonWriter.WriteStartDocument();
  119. bsonWriter.WriteName("value");
  120. _serializer.Serialize(context, value);
  121. bsonWriter.WriteEndDocument();
  122. return tempDocument[0];
  123. }
  124. }
  125. /// <summary>
  126. /// Serializes the values.
  127. /// </summary>
  128. /// <param name="values">The values.</param>
  129. /// <returns>The serialized values.</returns>
  130. public BsonArray SerializeValues(IEnumerable values)
  131. {
  132. var tempDocument = new BsonDocument();
  133. using (var bsonWriter = new BsonDocumentWriter(tempDocument))
  134. {
  135. var context = BsonSerializationContext.CreateRoot(bsonWriter);
  136. bsonWriter.WriteStartDocument();
  137. bsonWriter.WriteName("values");
  138. bsonWriter.WriteStartArray();
  139. foreach (var value in values)
  140. {
  141. _serializer.Serialize(context, value);
  142. }
  143. bsonWriter.WriteEndArray();
  144. bsonWriter.WriteEndDocument();
  145. return tempDocument[0].AsBsonArray;
  146. }
  147. }
  148. /// <summary>
  149. /// Creates a new BsonSerializationInfo object using the elementName provided and copying all other attributes.
  150. /// </summary>
  151. /// <param name="elementName">Name of the element.</param>
  152. /// <returns>A new BsonSerializationInfo.</returns>
  153. public BsonSerializationInfo WithNewName(string elementName)
  154. {
  155. return new BsonSerializationInfo(
  156. elementName,
  157. _serializer,
  158. _nominalType);
  159. }
  160. }
  161. }