예제 #1
0
        /// <summary>
        /// Constructs a new <see cref="JsonReaderState"/> instance.
        /// </summary>
        /// <param name="maxDepth">Sets the maximum depth allowed when reading JSON, with the default set as 64.
        /// Reading past this depth will throw a <exception cref="JsonReaderException"/>.</param>
        /// <param name="options">Defines the customized behavior of the <see cref="Utf8JsonReader"/>
        /// that is different from the JSON RFC (for example how to handle comments).
        /// By default, the <see cref="Utf8JsonReader"/> follows the JSON RFC strictly (i.e. comments within the JSON are invalid).</param>
        /// <exception cref="ArgumentException">
        /// Thrown when the max depth is set to a non-positive value (&lt;= 0)
        /// </exception>
        /// <remarks>
        /// An instance of this state must be passed to the <see cref="Utf8JsonReader"/> ctor with the JSON data.
        /// Unlike the <see cref="Utf8JsonReader"/>, which is a ref struct, the state can survive
        /// across async/await boundaries and hence this type is required to provide support for reading
        /// in more data asynchronously before continuing with a new instance of the <see cref="Utf8JsonReader"/>.
        /// </remarks>
        public JsonReaderState(int maxDepth = StackFreeMaxDepth, JsonReaderOptions options = default)
        {
            if (maxDepth <= 0)
            {
                throw ThrowHelper.GetArgumentException_MaxDepthMustBePositive();
            }

            _stackFreeContainer = default;
            _lineNumber         = default;
            _bytePositionInLine = default;
            _bytesConsumed      = default;
            _currentDepth       = default;
            _maxDepth           = maxDepth;
            _inObject           = default;
            _isNotPrimitive     = default;
            _tokenType          = default;
            _previousTokenType  = default;
            _readerOptions      = options;

            // Only allocate the stack if the user reads a JSON payload beyond the depth that the _stackFreeContainer can handle.
            // This way we avoid allocations in the common, default cases, and allocate lazily.
            _stack = null;
        }