BsonDefaults.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.Dynamic;
  18. using MongoDB.Bson.Serialization;
  19. namespace MongoDB.Bson
  20. {
  21. /// <summary>
  22. /// A static helper class containing BSON defaults.
  23. /// </summary>
  24. public static class BsonDefaults
  25. {
  26. // private static fields
  27. private static bool __dynamicArraySerializerWasSet;
  28. private static IBsonSerializer __dynamicArraySerializer;
  29. private static bool __dynamicDocumentSerializerWasSet;
  30. private static IBsonSerializer __dynamicDocumentSerializer;
  31. private static GuidRepresentation __guidRepresentation = GuidRepresentation.CSharpLegacy;
  32. private static GuidRepresentationMode __guidRepresentationMode = GuidRepresentationMode.V2;
  33. private static int __maxDocumentSize = int.MaxValue;
  34. private static int __maxSerializationDepth = 100;
  35. // static constructor
  36. static BsonDefaults()
  37. {
  38. var testWithDefaultGuidRepresentation = Environment.GetEnvironmentVariable("TEST_WITH_DEFAULT_GUID_REPRESENTATION");
  39. if (testWithDefaultGuidRepresentation != null)
  40. {
  41. var _ = Enum.TryParse(testWithDefaultGuidRepresentation, out __guidRepresentation); // ignore errors
  42. }
  43. var testWithDefaultGuidRepresentationMode = Environment.GetEnvironmentVariable("TEST_WITH_DEFAULT_GUID_REPRESENTATION_MODE");
  44. if (testWithDefaultGuidRepresentationMode != null)
  45. {
  46. var _ = Enum.TryParse(testWithDefaultGuidRepresentationMode, out __guidRepresentationMode); // ignore errors
  47. }
  48. }
  49. // public static properties
  50. /// <summary>
  51. /// Gets or sets the dynamic array serializer.
  52. /// </summary>
  53. public static IBsonSerializer DynamicArraySerializer
  54. {
  55. get
  56. {
  57. if (!__dynamicArraySerializerWasSet)
  58. {
  59. __dynamicArraySerializer = BsonSerializer.LookupSerializer<List<object>>();
  60. }
  61. return __dynamicArraySerializer;
  62. }
  63. set
  64. {
  65. __dynamicArraySerializerWasSet = true;
  66. __dynamicArraySerializer = value;
  67. }
  68. }
  69. /// <summary>
  70. /// Gets or sets the dynamic document serializer.
  71. /// </summary>
  72. public static IBsonSerializer DynamicDocumentSerializer
  73. {
  74. get
  75. {
  76. if (!__dynamicDocumentSerializerWasSet)
  77. {
  78. __dynamicDocumentSerializer = BsonSerializer.LookupSerializer<ExpandoObject>();
  79. }
  80. return __dynamicDocumentSerializer;
  81. }
  82. set
  83. {
  84. __dynamicDocumentSerializerWasSet = true;
  85. __dynamicDocumentSerializer = value;
  86. }
  87. }
  88. /// <summary>
  89. /// Gets or sets the default representation to be used in serialization of
  90. /// Guids to the database.
  91. /// <seealso cref="MongoDB.Bson.GuidRepresentation"/>
  92. /// </summary>
  93. [Obsolete("Configure serializers instead.")]
  94. public static GuidRepresentation GuidRepresentation
  95. {
  96. get
  97. {
  98. if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
  99. {
  100. throw new InvalidOperationException("BsonDefaults.GuidRepresentation can only be used when BsonDefaults.GuidRepresentationMode is V2.");
  101. }
  102. return __guidRepresentation;
  103. }
  104. set
  105. {
  106. if (BsonDefaults.GuidRepresentationMode != GuidRepresentationMode.V2)
  107. {
  108. throw new InvalidOperationException("BsonDefaults.GuidRepresentation can only be used when BsonDefaults.GuidRepresentationMode is V2.");
  109. }
  110. __guidRepresentation = value;
  111. }
  112. }
  113. /// <summary>
  114. /// Gets or sets the default representation to be used in serialization of
  115. /// Guids to the database.
  116. /// <seealso cref="MongoDB.Bson.GuidRepresentation"/>
  117. /// </summary>
  118. [Obsolete("This property will be removed in a later release.")]
  119. public static GuidRepresentationMode GuidRepresentationMode
  120. {
  121. get { return __guidRepresentationMode; }
  122. set
  123. {
  124. __guidRepresentationMode = value;
  125. if (value == GuidRepresentationMode.V3)
  126. {
  127. __guidRepresentation = GuidRepresentation.Unspecified;
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Gets or sets the default max document size. The default is 4MiB.
  133. /// </summary>
  134. public static int MaxDocumentSize
  135. {
  136. get { return __maxDocumentSize; }
  137. set { __maxDocumentSize = value; }
  138. }
  139. /// <summary>
  140. /// Gets or sets the default max serialization depth (used to detect circular references during serialization). The default is 100.
  141. /// </summary>
  142. public static int MaxSerializationDepth
  143. {
  144. get { return __maxSerializationDepth; }
  145. set { __maxSerializationDepth = value; }
  146. }
  147. }
  148. }