BsonDeserializationContext.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. namespace MongoDB.Bson.Serialization
  18. {
  19. /// <summary>
  20. /// Represents all the contextual information needed by a serializer to deserialize a value.
  21. /// </summary>
  22. public class BsonDeserializationContext
  23. {
  24. // private fields
  25. private readonly bool _allowDuplicateElementNames;
  26. private readonly IBsonSerializer _dynamicArraySerializer;
  27. private readonly IBsonSerializer _dynamicDocumentSerializer;
  28. private readonly IBsonReader _reader;
  29. // constructors
  30. private BsonDeserializationContext(
  31. IBsonReader reader,
  32. bool allowDuplicateElementNames,
  33. IBsonSerializer dynamicArraySerializer,
  34. IBsonSerializer dynamicDocumentSerializer)
  35. {
  36. _reader = reader;
  37. _allowDuplicateElementNames = allowDuplicateElementNames;
  38. _dynamicArraySerializer = dynamicArraySerializer;
  39. _dynamicDocumentSerializer = dynamicDocumentSerializer;
  40. }
  41. // public properties
  42. /// <summary>
  43. /// Gets a value indicating whether to allow duplicate element names.
  44. /// </summary>
  45. /// <value>
  46. /// <c>true</c> if duplicate element names shoud be allowed; otherwise, <c>false</c>.
  47. /// </value>
  48. public bool AllowDuplicateElementNames
  49. {
  50. get { return _allowDuplicateElementNames; }
  51. }
  52. /// <summary>
  53. /// Gets the dynamic array serializer.
  54. /// </summary>
  55. /// <value>
  56. /// The dynamic array serializer.
  57. /// </value>
  58. public IBsonSerializer DynamicArraySerializer
  59. {
  60. get { return _dynamicArraySerializer; }
  61. }
  62. /// <summary>
  63. /// Gets the dynamic document serializer.
  64. /// </summary>
  65. /// <value>
  66. /// The dynamic document serializer.
  67. /// </value>
  68. public IBsonSerializer DynamicDocumentSerializer
  69. {
  70. get { return _dynamicDocumentSerializer; }
  71. }
  72. /// <summary>
  73. /// Gets the reader.
  74. /// </summary>
  75. /// <value>
  76. /// The reader.
  77. /// </value>
  78. public IBsonReader Reader
  79. {
  80. get { return _reader; }
  81. }
  82. // public static methods
  83. /// <summary>
  84. /// Creates a root context.
  85. /// </summary>
  86. /// <param name="reader">The reader.</param>
  87. /// <param name="configurator">The configurator.</param>
  88. /// <returns>
  89. /// A root context.
  90. /// </returns>
  91. public static BsonDeserializationContext CreateRoot(
  92. IBsonReader reader,
  93. Action<Builder> configurator = null)
  94. {
  95. var builder = new Builder(null, reader);
  96. if (configurator != null)
  97. {
  98. configurator(builder);
  99. }
  100. return builder.Build();
  101. }
  102. // public methods
  103. /// <summary>
  104. /// Creates a new context with some values changed.
  105. /// </summary>
  106. /// <param name="configurator">The configurator.</param>
  107. /// <returns>
  108. /// A new context.
  109. /// </returns>
  110. public BsonDeserializationContext With(
  111. Action<Builder> configurator = null)
  112. {
  113. var builder = new Builder(this, _reader);
  114. if (configurator != null)
  115. {
  116. configurator(builder);
  117. }
  118. return builder.Build();
  119. }
  120. // nested classes
  121. /// <summary>
  122. /// Represents a builder for a BsonDeserializationContext.
  123. /// </summary>
  124. public class Builder
  125. {
  126. // private fields
  127. private bool _allowDuplicateElementNames;
  128. private IBsonSerializer _dynamicArraySerializer;
  129. private IBsonSerializer _dynamicDocumentSerializer;
  130. private IBsonReader _reader;
  131. // constructors
  132. internal Builder(BsonDeserializationContext other, IBsonReader reader)
  133. {
  134. if (reader == null)
  135. {
  136. throw new ArgumentNullException("reader");
  137. }
  138. _reader = reader;
  139. if (other != null)
  140. {
  141. _allowDuplicateElementNames = other.AllowDuplicateElementNames;
  142. _dynamicArraySerializer = other.DynamicArraySerializer;
  143. _dynamicDocumentSerializer = other.DynamicDocumentSerializer;
  144. }
  145. else
  146. {
  147. _dynamicArraySerializer = BsonDefaults.DynamicArraySerializer;
  148. _dynamicDocumentSerializer = BsonDefaults.DynamicDocumentSerializer;
  149. }
  150. }
  151. // properties
  152. /// <summary>
  153. /// Gets or sets a value indicating whether to allow duplicate element names.
  154. /// </summary>
  155. /// <value>
  156. /// <c>true</c> if duplicate element names should be allowed; otherwise, <c>false</c>.
  157. /// </value>
  158. public bool AllowDuplicateElementNames
  159. {
  160. get { return _allowDuplicateElementNames; }
  161. set { _allowDuplicateElementNames = value; }
  162. }
  163. /// <summary>
  164. /// Gets or sets the dynamic array serializer.
  165. /// </summary>
  166. /// <value>
  167. /// The dynamic array serializer.
  168. /// </value>
  169. public IBsonSerializer DynamicArraySerializer
  170. {
  171. get { return _dynamicArraySerializer; }
  172. set { _dynamicArraySerializer = value; }
  173. }
  174. /// <summary>
  175. /// Gets or sets the dynamic document serializer.
  176. /// </summary>
  177. /// <value>
  178. /// The dynamic document serializer.
  179. /// </value>
  180. public IBsonSerializer DynamicDocumentSerializer
  181. {
  182. get { return _dynamicDocumentSerializer; }
  183. set { _dynamicDocumentSerializer = value; }
  184. }
  185. /// <summary>
  186. /// Gets the reader.
  187. /// </summary>
  188. /// <value>
  189. /// The reader.
  190. /// </value>
  191. public IBsonReader Reader
  192. {
  193. get { return _reader; }
  194. }
  195. // public methods
  196. /// <summary>
  197. /// Builds the BsonDeserializationContext instance.
  198. /// </summary>
  199. /// <returns>A BsonDeserializationContext.</returns>
  200. internal BsonDeserializationContext Build()
  201. {
  202. return new BsonDeserializationContext(_reader, _allowDuplicateElementNames, _dynamicArraySerializer, _dynamicDocumentSerializer);
  203. }
  204. }
  205. }
  206. }