| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Text.Encodings.Web; |
| | | 5 | | |
| | | 6 | | namespace System.Text.Json |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides the ability for the user to define custom behavior when writing JSON |
| | | 10 | | /// using the <see cref="Utf8JsonWriter"/>. By default, the JSON is written without |
| | | 11 | | /// any indentation or extra white space. Also, the <see cref="Utf8JsonWriter"/> will |
| | | 12 | | /// throw an exception if the user attempts to write structurally invalid JSON. |
| | | 13 | | /// </summary> |
| | | 14 | | public struct JsonWriterOptions |
| | | 15 | | { |
| | 0 | 16 | | private static readonly string s_alternateNewLine = Environment.NewLine.Length == 2 ? JsonConstants.NewLineLineF |
| | | 17 | | |
| | | 18 | | internal const int DefaultMaxDepth = 1000; |
| | | 19 | | |
| | | 20 | | private int _maxDepth; |
| | | 21 | | private int _optionsMask; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The encoder to use when escaping strings, or <see langword="null" /> to use the default encoder. |
| | | 25 | | /// </summary> |
| | 0 | 26 | | public JavaScriptEncoder? Encoder { get; set; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Defines whether the <see cref="Utf8JsonWriter"/> should pretty print the JSON which includes: |
| | | 30 | | /// indenting nested JSON tokens, adding new lines, and adding white space between property names and values. |
| | | 31 | | /// By default, the JSON is written without any extra white space. |
| | | 32 | | /// </summary> |
| | | 33 | | public bool Indented |
| | | 34 | | { |
| | | 35 | | get |
| | 28 | 36 | | { |
| | 28 | 37 | | return (_optionsMask & IndentBit) != 0; |
| | 28 | 38 | | } |
| | | 39 | | set |
| | 0 | 40 | | { |
| | 0 | 41 | | if (value) |
| | 0 | 42 | | _optionsMask |= IndentBit; |
| | | 43 | | else |
| | 0 | 44 | | _optionsMask &= ~IndentBit; |
| | 0 | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Defines the indentation character used by <see cref="Utf8JsonWriter"/> when <see cref="Indented"/> is enable |
| | | 50 | | /// </summary> |
| | | 51 | | /// <remarks>Allowed characters are space and horizontal tab.</remarks> |
| | | 52 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> contains an invalid character.</excep |
| | | 53 | | public char IndentCharacter |
| | | 54 | | { |
| | 28 | 55 | | readonly get => (_optionsMask & IndentCharacterBit) != 0 ? JsonConstants.TabIndentCharacter : JsonConstants. |
| | | 56 | | set |
| | 0 | 57 | | { |
| | 0 | 58 | | JsonWriterHelper.ValidateIndentCharacter(value); |
| | 0 | 59 | | if (value is not JsonConstants.DefaultIndentCharacter) |
| | 0 | 60 | | _optionsMask |= IndentCharacterBit; |
| | | 61 | | else |
| | 0 | 62 | | _optionsMask &= ~IndentCharacterBit; |
| | 0 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Defines the indentation size used by <see cref="Utf8JsonWriter"/> when <see cref="Indented"/> is enabled. De |
| | | 68 | | /// </summary> |
| | | 69 | | /// <remarks>Allowed values are integers between 0 and 127, included.</remarks> |
| | | 70 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is out of the allowed range.</excepti |
| | | 71 | | public int IndentSize |
| | | 72 | | { |
| | 28 | 73 | | readonly get => EncodeIndentSize((_optionsMask & IndentSizeMask) >> OptionsBitCount); |
| | | 74 | | set |
| | 0 | 75 | | { |
| | 0 | 76 | | JsonWriterHelper.ValidateIndentSize(value); |
| | 0 | 77 | | _optionsMask = (_optionsMask & ~IndentSizeMask) | (EncodeIndentSize(value) << OptionsBitCount); |
| | 0 | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | // Encoding is applied by swapping 0 with the default value to ensure default(JsonWriterOptions) instances are w |
| | | 82 | | // As this operation is symmetrical, it can also be used to decode. |
| | 28 | 83 | | private static int EncodeIndentSize(int value) => value switch |
| | 28 | 84 | | { |
| | 28 | 85 | | 0 => JsonConstants.DefaultIndentSize, |
| | 0 | 86 | | JsonConstants.DefaultIndentSize => 0, |
| | 0 | 87 | | _ => value |
| | 28 | 88 | | }; |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets or sets the maximum depth allowed when writing JSON, with the default (i.e. 0) indicating a max depth o |
| | | 92 | | /// </summary> |
| | | 93 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 94 | | /// Thrown when the max depth is set to a negative value. |
| | | 95 | | /// </exception> |
| | | 96 | | /// <remarks> |
| | | 97 | | /// Reading past this depth will throw a <exception cref="JsonException"/>. |
| | | 98 | | /// </remarks> |
| | | 99 | | public int MaxDepth |
| | | 100 | | { |
| | 28 | 101 | | readonly get => _maxDepth; |
| | | 102 | | set |
| | 28 | 103 | | { |
| | 28 | 104 | | if (value < 0) |
| | 0 | 105 | | { |
| | 0 | 106 | | ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value)); |
| | | 107 | | } |
| | | 108 | | |
| | 28 | 109 | | _maxDepth = value; |
| | 28 | 110 | | } |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Defines whether the <see cref="Utf8JsonWriter"/> should skip structural validation and allow |
| | | 115 | | /// the user to write invalid JSON, when set to true. If set to false, any attempts to write invalid JSON will r |
| | | 116 | | /// a <exception cref="InvalidOperationException"/> to be thrown. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <remarks> |
| | | 119 | | /// If the JSON being written is known to be correct, |
| | | 120 | | /// then skipping validation (by setting it to true) could improve performance. |
| | | 121 | | /// An example of invalid JSON where the writer will throw (when SkipValidation |
| | | 122 | | /// is set to false) is when you write a value within a JSON object |
| | | 123 | | /// without a property name. |
| | | 124 | | /// </remarks> |
| | | 125 | | public bool SkipValidation |
| | | 126 | | { |
| | | 127 | | get |
| | 28 | 128 | | { |
| | 28 | 129 | | return (_optionsMask & SkipValidationBit) != 0; |
| | 28 | 130 | | } |
| | | 131 | | set |
| | 0 | 132 | | { |
| | 0 | 133 | | if (value) |
| | 0 | 134 | | _optionsMask |= SkipValidationBit; |
| | | 135 | | else |
| | 0 | 136 | | _optionsMask &= ~SkipValidationBit; |
| | 0 | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets or sets the new line string to use when <see cref="Indented"/> is <see langword="true"/>. |
| | | 142 | | /// The default is the value of <see cref="Environment.NewLine"/>. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <exception cref="ArgumentNullException"> |
| | | 145 | | /// Thrown when the new line string is <see langword="null"/>. |
| | | 146 | | /// </exception> |
| | | 147 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 148 | | /// Thrown when the new line string is not <c>\n</c> or <c>\r\n</c>. |
| | | 149 | | /// </exception> |
| | | 150 | | public string NewLine |
| | | 151 | | { |
| | 56 | 152 | | get => (_optionsMask & NewLineBit) != 0 ? s_alternateNewLine : Environment.NewLine; |
| | | 153 | | set |
| | 0 | 154 | | { |
| | 0 | 155 | | JsonWriterHelper.ValidateNewLine(value); |
| | 0 | 156 | | if (value != Environment.NewLine) |
| | 0 | 157 | | _optionsMask |= NewLineBit; |
| | | 158 | | else |
| | 0 | 159 | | _optionsMask &= ~NewLineBit; |
| | 0 | 160 | | } |
| | | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | internal bool IndentedOrNotSkipValidation => (_optionsMask & (IndentBit | SkipValidationBit)) != SkipValidationB |
| | | 164 | | |
| | | 165 | | private const int OptionsBitCount = 4; |
| | | 166 | | private const int IndentBit = 1; |
| | | 167 | | private const int SkipValidationBit = 2; |
| | | 168 | | private const int NewLineBit = 4; |
| | | 169 | | private const int IndentCharacterBit = 8; |
| | | 170 | | private const int IndentSizeMask = JsonConstants.MaximumIndentSize << OptionsBitCount; |
| | | 171 | | } |
| | | 172 | | } |