JsonWriterSettings.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.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 bool _closeOutput = false;
  29. private Encoding _encoding = Encoding.UTF8;
  30. private bool _indent = false;
  31. private string _indentChars = " ";
  32. private string _newLineChars = "\r\n";
  33. private JsonOutputMode _outputMode = JsonOutputMode.Shell;
  34. private Version _shellVersion;
  35. // constructors
  36. /// <summary>
  37. /// Initializes a new instance of the JsonWriterSettings class.
  38. /// </summary>
  39. public JsonWriterSettings()
  40. {
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the JsonWriterSettings class.
  44. /// </summary>
  45. /// <param name="closeOutput">Whether to close the output when the writer is closed.</param>
  46. /// <param name="encoding">The output Encoding.</param>
  47. /// <param name="guidRepresentation">The representation for Guids.</param>
  48. /// <param name="indent">Whether to indent the output.</param>
  49. /// <param name="indentChars">The indentation characters.</param>
  50. /// <param name="newLineChars">The new line characters.</param>
  51. /// <param name="outputMode">The output mode.</param>
  52. /// <param name="shellVersion">The version of the shell to target.</param>
  53. [Obsolete("Use the no-argument constructor instead and set the properties.")]
  54. public JsonWriterSettings(
  55. bool closeOutput,
  56. Encoding encoding,
  57. GuidRepresentation guidRepresentation,
  58. bool indent,
  59. string indentChars,
  60. string newLineChars,
  61. JsonOutputMode outputMode,
  62. Version shellVersion)
  63. : base(guidRepresentation)
  64. {
  65. _closeOutput = closeOutput;
  66. _encoding = encoding;
  67. _indent = indent;
  68. _indentChars = indentChars;
  69. _newLineChars = newLineChars;
  70. _outputMode = outputMode;
  71. _shellVersion = shellVersion;
  72. }
  73. // public static properties
  74. /// <summary>
  75. /// Gets or sets the default JsonWriterSettings.
  76. /// </summary>
  77. public static JsonWriterSettings Defaults
  78. {
  79. get
  80. {
  81. if (__defaults == null)
  82. {
  83. __defaults = new JsonWriterSettings();
  84. }
  85. return __defaults;
  86. }
  87. set { __defaults = value; }
  88. }
  89. // public properties
  90. /// <summary>
  91. /// Gets or sets whether to close the output when the writer is closed.
  92. /// </summary>
  93. public bool CloseOutput
  94. {
  95. get { return _closeOutput; }
  96. set
  97. {
  98. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  99. _closeOutput = value;
  100. }
  101. }
  102. /// <summary>
  103. /// Gets or sets the output Encoding.
  104. /// </summary>
  105. [Obsolete("Set the Encoding when you create a StreamWriter instead (this property is ignored).")]
  106. public Encoding Encoding
  107. {
  108. get { return _encoding; }
  109. set
  110. {
  111. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  112. _encoding = value;
  113. }
  114. }
  115. /// <summary>
  116. /// Gets or sets whether to indent the output.
  117. /// </summary>
  118. public bool Indent
  119. {
  120. get { return _indent; }
  121. set
  122. {
  123. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  124. _indent = value;
  125. }
  126. }
  127. /// <summary>
  128. /// Gets or sets the indent characters.
  129. /// </summary>
  130. public string IndentChars
  131. {
  132. get { return _indentChars; }
  133. set
  134. {
  135. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  136. _indentChars = value;
  137. }
  138. }
  139. /// <summary>
  140. /// Gets or sets the new line characters.
  141. /// </summary>
  142. public string NewLineChars
  143. {
  144. get { return _newLineChars; }
  145. set
  146. {
  147. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  148. _newLineChars = value;
  149. }
  150. }
  151. /// <summary>
  152. /// Gets or sets the output mode.
  153. /// </summary>
  154. public JsonOutputMode OutputMode
  155. {
  156. get { return _outputMode; }
  157. set
  158. {
  159. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  160. _outputMode = value;
  161. }
  162. }
  163. /// <summary>
  164. /// Gets or sets the shell version (used with OutputMode Shell).
  165. /// </summary>
  166. public Version ShellVersion
  167. {
  168. get { return _shellVersion; }
  169. set
  170. {
  171. if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
  172. _shellVersion = value;
  173. }
  174. }
  175. // public methods
  176. /// <summary>
  177. /// Creates a clone of the settings.
  178. /// </summary>
  179. /// <returns>A clone of the settings.</returns>
  180. public new JsonWriterSettings Clone()
  181. {
  182. return (JsonWriterSettings)CloneImplementation();
  183. }
  184. // protected methods
  185. /// <summary>
  186. /// Creates a clone of the settings.
  187. /// </summary>
  188. /// <returns>A clone of the settings.</returns>
  189. protected override BsonWriterSettings CloneImplementation()
  190. {
  191. var clone = new JsonWriterSettings
  192. {
  193. CloseOutput = _closeOutput,
  194. #pragma warning disable 618
  195. Encoding = _encoding,
  196. #pragma warning restore
  197. GuidRepresentation = GuidRepresentation,
  198. Indent = _indent,
  199. IndentChars = _indentChars,
  200. MaxSerializationDepth = MaxSerializationDepth,
  201. NewLineChars = _newLineChars,
  202. OutputMode = _outputMode,
  203. ShellVersion = _shellVersion
  204. };
  205. return clone;
  206. }
  207. }
  208. }