JsonWriterSettings.cs 5.3 KB

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