< Summary

Information
Line coverage
35%
Covered lines: 18
Uncovered lines: 33
Coverable lines: 51
Total lines: 172
Line coverage: 35.2%
Branch coverage
20%
Covered branches: 4
Total branches: 20
Branch coverage: 20%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()0%220%
EncodeIndentSize(...)25%4466.66%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\JsonWriterOptions.cs

#LineLine coverage
 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
 4using System.Text.Encodings.Web;
 5
 6namespace 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    {
 016        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>
 026        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
 2836            {
 2837                return (_optionsMask & IndentBit) != 0;
 2838            }
 39            set
 040            {
 041                if (value)
 042                    _optionsMask |= IndentBit;
 43                else
 044                    _optionsMask &= ~IndentBit;
 045            }
 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        {
 2855            readonly get => (_optionsMask & IndentCharacterBit) != 0 ? JsonConstants.TabIndentCharacter : JsonConstants.
 56            set
 057            {
 058                JsonWriterHelper.ValidateIndentCharacter(value);
 059                if (value is not JsonConstants.DefaultIndentCharacter)
 060                    _optionsMask |= IndentCharacterBit;
 61                else
 062                    _optionsMask &= ~IndentCharacterBit;
 063            }
 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        {
 2873            readonly get => EncodeIndentSize((_optionsMask & IndentSizeMask) >> OptionsBitCount);
 74            set
 075            {
 076                JsonWriterHelper.ValidateIndentSize(value);
 077                _optionsMask = (_optionsMask & ~IndentSizeMask) | (EncodeIndentSize(value) << OptionsBitCount);
 078            }
 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.
 2883        private static int EncodeIndentSize(int value) => value switch
 2884        {
 2885            0 => JsonConstants.DefaultIndentSize,
 086            JsonConstants.DefaultIndentSize => 0,
 087            _ => value
 2888        };
 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        {
 28101            readonly get => _maxDepth;
 102            set
 28103            {
 28104                if (value < 0)
 0105                {
 0106                    ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value));
 107                }
 108
 28109                _maxDepth = value;
 28110            }
 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
 28128            {
 28129                return (_optionsMask & SkipValidationBit) != 0;
 28130            }
 131            set
 0132            {
 0133                if (value)
 0134                    _optionsMask |= SkipValidationBit;
 135                else
 0136                    _optionsMask &= ~SkipValidationBit;
 0137            }
 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        {
 56152            get => (_optionsMask & NewLineBit) != 0 ? s_alternateNewLine : Environment.NewLine;
 153            set
 0154            {
 0155                JsonWriterHelper.ValidateNewLine(value);
 0156                if (value != Environment.NewLine)
 0157                    _optionsMask |= NewLineBit;
 158                else
 0159                    _optionsMask &= ~NewLineBit;
 0160            }
 161        }
 162
 0163        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}