BsonSerializationInfo.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Copyright 2010-2014 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. private IBsonSerializationOptions _serializationOptions;
  30. // constructors
  31. /// <summary>
  32. /// Initializes a new instance of the BsonSerializationInfo class.
  33. /// </summary>
  34. /// <param name="elementName">The element name.</param>
  35. /// <param name="serializer">The serializer.</param>
  36. /// <param name="nominalType">The nominal type.</param>
  37. /// <param name="serializationOptions">The serialization options.</param>
  38. public BsonSerializationInfo(string elementName, IBsonSerializer serializer, Type nominalType, IBsonSerializationOptions serializationOptions)
  39. {
  40. _elementName = elementName;
  41. _serializer = serializer;
  42. _nominalType = nominalType;
  43. _serializationOptions = serializationOptions;
  44. }
  45. // public properties
  46. /// <summary>
  47. /// Gets or sets the dotted element name.
  48. /// </summary>
  49. public string ElementName
  50. {
  51. get { return _elementName; }
  52. }
  53. /// <summary>
  54. /// Gets or sets the serializer.
  55. /// </summary>
  56. public IBsonSerializer Serializer
  57. {
  58. get { return _serializer; }
  59. }
  60. /// <summary>
  61. /// Gets or sets the nominal type.
  62. /// </summary>
  63. public Type NominalType
  64. {
  65. get { return _nominalType; }
  66. }
  67. /// <summary>
  68. /// Gets or sets the serialization options.
  69. /// </summary>
  70. public IBsonSerializationOptions SerializationOptions
  71. {
  72. get { return _serializationOptions; }
  73. }
  74. /// <summary>
  75. /// Deserializes the value.
  76. /// </summary>
  77. /// <param name="value">The value.</param>
  78. /// <returns>The deserialized value.</returns>
  79. public object DeserializeValue(BsonValue value)
  80. {
  81. var tempDocument = new BsonDocument("value", value);
  82. using (var reader = BsonReader.Create(tempDocument))
  83. {
  84. reader.ReadStartDocument();
  85. reader.ReadName("value");
  86. var deserializedValue = _serializer.Deserialize(reader, _nominalType, _serializationOptions);
  87. reader.ReadEndDocument();
  88. return deserializedValue;
  89. }
  90. }
  91. /// <summary>
  92. /// Serializes the value.
  93. /// </summary>
  94. /// <param name="value">The value.</param>
  95. /// <returns>The serialized value.</returns>
  96. public BsonValue SerializeValue(object value)
  97. {
  98. var tempDocument = new BsonDocument();
  99. using (var bsonWriter = BsonWriter.Create(tempDocument))
  100. {
  101. bsonWriter.WriteStartDocument();
  102. bsonWriter.WriteName("value");
  103. Serialize(bsonWriter, value);
  104. bsonWriter.WriteEndDocument();
  105. return tempDocument[0];
  106. }
  107. }
  108. /// <summary>
  109. /// Serializes the values.
  110. /// </summary>
  111. /// <param name="values">The values.</param>
  112. /// <returns>The serialized values.</returns>
  113. public BsonArray SerializeValues(IEnumerable values)
  114. {
  115. var tempDocument = new BsonDocument();
  116. using (var bsonWriter = BsonWriter.Create(tempDocument))
  117. {
  118. bsonWriter.WriteStartDocument();
  119. bsonWriter.WriteName("values");
  120. bsonWriter.WriteStartArray();
  121. foreach (var value in values)
  122. {
  123. Serialize(bsonWriter, value);
  124. }
  125. bsonWriter.WriteEndArray();
  126. bsonWriter.WriteEndDocument();
  127. return tempDocument[0].AsBsonArray;
  128. }
  129. }
  130. // private methods
  131. private void Serialize(BsonWriter bsonWriter, object value)
  132. {
  133. var serializer = _serializer;
  134. var actualType = (value == null) ? _nominalType : value.GetType();
  135. if (actualType != _nominalType)
  136. {
  137. serializer = BsonSerializer.LookupSerializer(actualType);
  138. }
  139. serializer.Serialize(bsonWriter, _nominalType, value, _serializationOptions);
  140. }
  141. }
  142. }