JsonWriterSettings.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.Text;
  17. namespace MongoDB.Bson.IO
  18. {
  19. /// <summary>
  20. /// Represents settings for a JsonWriter.
  21. /// </summary>
  22. [Serializable]
  23. public class JsonWriterSettings : BsonWriterSettings
  24. {
  25. // private static fields
  26. private static JsonWriterSettings __defaults = null; // delay creation to pick up the latest default values
  27. // private fields
  28. private Encoding _encoding = Encoding.UTF8;
  29. private bool _indent = false;
  30. private string _indentChars = " ";
  31. private string _newLineChars = "\r\n";
  32. private JsonOutputMode _outputMode = JsonOutputMode.Shell;
  33. private Version _shellVersion;
  34. // constructors
  35. /// <summary>
  36. /// Initializes a new instance of the JsonWriterSettings class.
  37. /// </summary>
  38. public JsonWriterSettings()
  39. {
  40. }
  41. // public static properties
  42. /// <summary>
  43. /// Gets or sets the default JsonWriterSettings.
  44. /// </summary>
  45. public static JsonWriterSettings Defaults
  46. {
  47. get
  48. {
  49. if (__defaults == null)
  50. {
  51. __defaults = new JsonWriterSettings();
  52. }
  53. return __defaults;
  54. }
  55. set { __defaults = value; }
  56. }
  57. // public properties
  58. /// <summary>
  59. /// Gets or sets the output Encoding.
  60. /// </summary>
  61. [Obsolete("Set the Encoding when you create a StreamWriter instead (this property is ignored).")]
  62. public Encoding Encoding
  63. {
  64. get { return _encoding; }
  65. set
  66. {
  67. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  68. _encoding = value;
  69. }
  70. }
  71. /// <summary>
  72. /// Gets or sets whether to indent the output.
  73. /// </summary>
  74. public bool Indent
  75. {
  76. get { return _indent; }
  77. set
  78. {
  79. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  80. _indent = value;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets or sets the indent characters.
  85. /// </summary>
  86. public string IndentChars
  87. {
  88. get { return _indentChars; }
  89. set
  90. {
  91. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  92. _indentChars = value;
  93. }
  94. }
  95. /// <summary>
  96. /// Gets or sets the new line characters.
  97. /// </summary>
  98. public string NewLineChars
  99. {
  100. get { return _newLineChars; }
  101. set
  102. {
  103. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  104. _newLineChars = value;
  105. }
  106. }
  107. /// <summary>
  108. /// Gets or sets the output mode.
  109. /// </summary>
  110. public JsonOutputMode OutputMode
  111. {
  112. get { return _outputMode; }
  113. set
  114. {
  115. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  116. _outputMode = value;
  117. }
  118. }
  119. /// <summary>
  120. /// Gets or sets the shell version (used with OutputMode Shell).
  121. /// </summary>
  122. public Version ShellVersion
  123. {
  124. get { return _shellVersion; }
  125. set
  126. {
  127. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  128. _shellVersion = value;
  129. }
  130. }
  131. // public methods
  132. /// <summary>
  133. /// Creates a clone of the settings.
  134. /// </summary>
  135. /// <returns>A clone of the settings.</returns>
  136. public new JsonWriterSettings Clone()
  137. {
  138. return (JsonWriterSettings)CloneImplementation();
  139. }
  140. // protected methods
  141. /// <summary>
  142. /// Creates a clone of the settings.
  143. /// </summary>
  144. /// <returns>A clone of the settings.</returns>
  145. protected override BsonWriterSettings CloneImplementation()
  146. {
  147. var clone = new JsonWriterSettings
  148. {
  149. #pragma warning disable 618
  150. Encoding = _encoding,
  151. #pragma warning restore 618
  152. Indent = _indent,
  153. IndentChars = _indentChars,
  154. MaxSerializationDepth = MaxSerializationDepth,
  155. NewLineChars = _newLineChars,
  156. OutputMode = _outputMode,
  157. ShellVersion = _shellVersion
  158. };
  159. #pragma warning disable 618
  160. if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
  161. {
  162. clone.GuidRepresentation = GuidRepresentation;
  163. }
  164. #pragma warning restore
  165. return clone;
  166. }
  167. }
  168. }