< Summary

Line coverage
11%
Covered lines: 60
Uncovered lines: 468
Coverable lines: 528
Total lines: 2095
Line coverage: 11.3%
Branch coverage
6%
Covered branches: 8
Total branches: 126
Branch coverage: 6.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
File 1: .ctor(...)100%110%
File 1: GetEnumerator()100%110%
File 1: System.Collections.IEnumerable.GetEnumerator()100%110%
File 1: System.Collections.Generic.IEnumerable<System.Text.Json.JsonElement>.GetEnumerator()100%110%
File 1: Dispose()100%110%
File 1: Reset()100%110%
File 1: MoveNext()0%440%
File 2: .ctor(...)100%11100%
File 2: GetArrayLength()100%110%
File 2: GetPropertyCount()100%110%
File 2: GetProperty(...)0%220%
File 2: GetProperty(...)0%220%
File 2: GetProperty(...)0%220%
File 2: TryGetProperty(...)100%110%
File 2: TryGetProperty(...)100%110%
File 2: TryGetProperty(...)100%110%
File 2: GetBoolean()0%440%
File 2: ThrowJsonElementWrongTypeException(System.Text.Json.JsonTokenType)100%110%
File 2: GetString()100%110%
File 2: TryGetBytesFromBase64(...)100%110%
File 2: GetBytesFromBase64()0%220%
File 2: TryGetSByte(...)100%110%
File 2: GetSByte()0%220%
File 2: TryGetByte(...)100%110%
File 2: GetByte()0%220%
File 2: TryGetInt16(...)100%110%
File 2: GetInt16()0%220%
File 2: TryGetUInt16(...)100%110%
File 2: GetUInt16()0%220%
File 2: TryGetInt32(...)100%110%
File 2: GetInt32()0%220%
File 2: TryGetUInt32(...)100%110%
File 2: GetUInt32()0%220%
File 2: TryGetInt64(...)100%110%
File 2: GetInt64()0%220%
File 2: TryGetUInt64(...)100%110%
File 2: GetUInt64()0%220%
File 2: TryGetDouble(...)100%110%
File 2: GetDouble()0%220%
File 2: TryGetSingle(...)100%110%
File 2: GetSingle()0%220%
File 2: TryGetDecimal(...)100%110%
File 2: GetDecimal()0%220%
File 2: TryGetDateTime(...)100%110%
File 2: GetDateTime()0%220%
File 2: TryGetDateTimeOffset(...)100%110%
File 2: GetDateTimeOffset()0%220%
File 2: TryGetGuid(...)100%110%
File 2: GetGuid()0%220%
File 2: GetPropertyName()100%110%
File 2: GetPropertyNameRaw()100%110%
File 2: GetRawText()100%110%
File 2: GetRawValue()100%11100%
File 2: GetPropertyRawText()100%110%
File 2: DeepEquals(...)17.24%292918.64%
File 2: UnorderedObjectDeepEquals(System.Text.Json.JsonElement/ObjectEnumerator,System.Text.Json.JsonElement/ObjectEnumerator,System.Int32)0%10100%
File 2: NameEquals(System.Text.Json.JsonProperty,System.Text.Json.JsonProperty)0%440%
File 2: ValueEquals(...)0%220%
File 2: ValueEquals(...)50%2266.66%
File 2: ValueEquals(...)0%220%
File 2: TextEqualsHelper(...)100%11100%
File 2: TextEqualsHelper(...)100%110%
File 2: ValueIsEscapedHelper(...)100%110%
File 2: WriteTo(...)100%110%
File 2: WritePropertyNameTo(...)100%110%
File 2: EnumerateArray()0%220%
File 2: EnumerateObject()0%220%
File 2: ToString()0%13130%
File 2: Clone()0%220%
File 2: CheckValidInstance()50%2260%
File 3: .ctor(...)100%110%
File 3: GetEnumerator()100%110%
File 3: System.Collections.IEnumerable.GetEnumerator()100%110%
File 3: System.Collections.Generic.IEnumerable<System.Text.Json.JsonProperty>.GetEnumerator()100%110%
File 3: Dispose()100%110%
File 3: Reset()100%110%
File 3: MoveNext()0%440%
File 4: ParseValue(...)100%11100%
File 4: ParseValue(...)100%11100%
File 4: ParseValue(...)100%110%
File 4: Parse(...)100%110%
File 4: Parse(...)100%110%
File 4: Parse(...)100%110%
File 4: TryParseValue(...)0%220%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Document\JsonElement.ArrayEnumerator.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.Collections;
 5using System.Collections.Generic;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public partial struct JsonElement
 11    {
 12        /// <summary>
 13        ///   An enumerable and enumerator for the contents of a JSON array.
 14        /// </summary>
 15        [DebuggerDisplay("{Current,nq}")]
 16        public struct ArrayEnumerator : IEnumerable<JsonElement>, IEnumerator<JsonElement>
 17        {
 18            private readonly JsonElement _target;
 19            private int _curIdx;
 20            private readonly int _endIdxOrVersion;
 21
 22            internal ArrayEnumerator(JsonElement target, int currentIndex = -1)
 023            {
 024                _target = target;
 025                _curIdx = currentIndex;
 26
 027                Debug.Assert(target.TokenType == JsonTokenType.StartArray);
 28
 029                _endIdxOrVersion = target._parent.GetEndIndex(_target._idx, includeEndElement: false);
 030            }
 31
 32            /// <inheritdoc />
 33            public JsonElement Current
 34            {
 35                get
 036                {
 037                    if (_curIdx < 0)
 038                    {
 039                        return default;
 40                    }
 41
 042                    return new JsonElement(_target._parent, _curIdx);
 043                }
 44            }
 45
 46            /// <summary>
 47            ///   Returns an enumerator that iterates through a collection.
 48            /// </summary>
 49            /// <returns>
 50            ///   An <see cref="ArrayEnumerator"/> value that can be used to iterate
 51            ///   through the array.
 52            /// </returns>
 53            public ArrayEnumerator GetEnumerator()
 054            {
 055                ArrayEnumerator ator = this;
 056                ator._curIdx = -1;
 057                return ator;
 058            }
 59
 60            /// <inheritdoc />
 061            IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 62
 63            /// <inheritdoc />
 064            IEnumerator<JsonElement> IEnumerable<JsonElement>.GetEnumerator() => GetEnumerator();
 65
 66            /// <inheritdoc />
 67            public void Dispose()
 068            {
 069                _curIdx = _endIdxOrVersion;
 070            }
 71
 72            /// <inheritdoc />
 73            public void Reset()
 074            {
 075                _curIdx = -1;
 076            }
 77
 78            /// <inheritdoc />
 079            object IEnumerator.Current => Current;
 80
 81            /// <inheritdoc />
 82            public bool MoveNext()
 083            {
 084                if (_curIdx >= _endIdxOrVersion)
 085                {
 086                    return false;
 87                }
 88
 089                if (_curIdx < 0)
 090                {
 091                    _curIdx = _target._idx + JsonDocument.DbRow.Size;
 092                }
 93                else
 094                {
 095                    _curIdx = _target._parent.GetEndIndex(_curIdx, includeEndElement: true);
 096                }
 97
 098                return _curIdx < _endIdxOrVersion;
 099            }
 100        }
 101    }
 102}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Document\JsonElement.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.Buffers.Text;
 5using System.Collections.Generic;
 6using System.Diagnostics;
 7using System.Diagnostics.CodeAnalysis;
 8using System.Runtime.CompilerServices;
 9using System.Runtime.InteropServices;
 10
 11namespace System.Text.Json
 12{
 13    /// <summary>
 14    ///   Represents a specific JSON value within a <see cref="JsonDocument"/>.
 15    /// </summary>
 16    [DebuggerDisplay("{DebuggerDisplay,nq}")]
 17    public readonly partial struct JsonElement
 18    {
 19        private readonly JsonDocument _parent;
 20        private readonly int _idx;
 21
 22        internal JsonElement(JsonDocument parent, int idx)
 96623        {
 24            // parent is usually not null, but the Current property
 25            // on the enumerators (when initialized as `default`) can
 26            // get here with a null.
 96627            Debug.Assert(idx >= 0);
 28
 96629            _parent = parent;
 96630            _idx = idx;
 96631        }
 32
 33        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 34        private JsonTokenType TokenType
 35        {
 36            get
 14537            {
 14538                return _parent?.GetJsonTokenType(_idx) ?? JsonTokenType.None;
 14539            }
 40        }
 41        /// <summary>
 42        ///   The <see cref="JsonValueKind"/> that the value is.
 43        /// </summary>
 44        /// <exception cref="ObjectDisposedException">
 45        ///   The parent <see cref="JsonDocument"/> has been disposed.
 46        /// </exception>
 14247        public JsonValueKind ValueKind => TokenType.ToValueKind();
 48
 49        /// <summary>
 50        ///   Get the value at a specified index when the current value is a
 51        ///   <see cref="JsonValueKind.Array"/>.
 52        /// </summary>
 53        /// <exception cref="InvalidOperationException">
 54        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Array"/>.
 55        /// </exception>
 56        /// <exception cref="IndexOutOfRangeException">
 57        ///   <paramref name="index"/> is not in the range [0, <see cref="GetArrayLength"/>()).
 58        /// </exception>
 59        /// <exception cref="ObjectDisposedException">
 60        ///   The parent <see cref="JsonDocument"/> has been disposed.
 61        /// </exception>
 62        public JsonElement this[int index]
 63        {
 64            get
 065            {
 066                CheckValidInstance();
 67
 068                return _parent.GetArrayIndexElement(_idx, index);
 069            }
 70        }
 71
 72        /// <summary>
 73        ///   Get the number of values contained within the current array value.
 74        /// </summary>
 75        /// <returns>The number of values contained within the current array value.</returns>
 76        /// <exception cref="InvalidOperationException">
 77        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Array"/>.
 78        /// </exception>
 79        /// <exception cref="ObjectDisposedException">
 80        ///   The parent <see cref="JsonDocument"/> has been disposed.
 81        /// </exception>
 82        public int GetArrayLength()
 083        {
 084            CheckValidInstance();
 85
 086            return _parent.GetArrayLength(_idx);
 087        }
 88
 89        /// <summary>
 90        ///   Get the number of properties contained within the current object value.
 91        /// </summary>
 92        /// <returns>The number of properties contained within the current object value.</returns>
 93        /// <exception cref="InvalidOperationException">
 94        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 95        /// </exception>
 96        /// <exception cref="ObjectDisposedException">
 97        ///   The parent <see cref="JsonDocument"/> has been disposed.
 98        /// </exception>
 99        public int GetPropertyCount()
 0100        {
 0101            CheckValidInstance();
 102
 0103            return _parent.GetPropertyCount(_idx);
 0104        }
 105
 106        /// <summary>
 107        ///   Gets a <see cref="JsonElement"/> representing the value of a required property identified
 108        ///   by <paramref name="propertyName"/>.
 109        /// </summary>
 110        /// <remarks>
 111        ///   Property name matching is performed as an ordinal, case-sensitive, comparison.
 112        ///
 113        ///   If a property is defined multiple times for the same object, the last such definition is
 114        ///   what is matched.
 115        /// </remarks>
 116        /// <param name="propertyName">Name of the property whose value to return.</param>
 117        /// <returns>
 118        ///   A <see cref="JsonElement"/> representing the value of the requested property.
 119        /// </returns>
 120        /// <seealso cref="EnumerateObject"/>
 121        /// <exception cref="InvalidOperationException">
 122        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 123        /// </exception>
 124        /// <exception cref="KeyNotFoundException">
 125        ///   No property was found with the requested name.
 126        /// </exception>
 127        /// <exception cref="ArgumentNullException">
 128        ///   <paramref name="propertyName"/> is <see langword="null"/>.
 129        /// </exception>
 130        /// <exception cref="ObjectDisposedException">
 131        ///   The parent <see cref="JsonDocument"/> has been disposed.
 132        /// </exception>
 133        public JsonElement GetProperty(string propertyName)
 0134        {
 0135            ArgumentNullException.ThrowIfNull(propertyName);
 136
 0137            if (TryGetProperty(propertyName, out JsonElement property))
 0138            {
 0139                return property;
 140            }
 141
 0142            throw new KeyNotFoundException();
 0143        }
 144
 145        /// <summary>
 146        ///   Gets a <see cref="JsonElement"/> representing the value of a required property identified
 147        ///   by <paramref name="propertyName"/>.
 148        /// </summary>
 149        /// <remarks>
 150        ///   <para>
 151        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 152        ///   </para>
 153        ///
 154        ///   <para>
 155        ///     If a property is defined multiple times for the same object, the last such definition is
 156        ///     what is matched.
 157        ///   </para>
 158        /// </remarks>
 159        /// <param name="propertyName">Name of the property whose value to return.</param>
 160        /// <returns>
 161        ///   A <see cref="JsonElement"/> representing the value of the requested property.
 162        /// </returns>
 163        /// <seealso cref="EnumerateObject"/>
 164        /// <exception cref="InvalidOperationException">
 165        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 166        /// </exception>
 167        /// <exception cref="KeyNotFoundException">
 168        ///   No property was found with the requested name.
 169        /// </exception>
 170        /// <exception cref="ObjectDisposedException">
 171        ///   The parent <see cref="JsonDocument"/> has been disposed.
 172        /// </exception>
 173        public JsonElement GetProperty(ReadOnlySpan<char> propertyName)
 0174        {
 0175            if (TryGetProperty(propertyName, out JsonElement property))
 0176            {
 0177                return property;
 178            }
 179
 0180            throw new KeyNotFoundException();
 0181        }
 182
 183        /// <summary>
 184        ///   Gets a <see cref="JsonElement"/> representing the value of a required property identified
 185        ///   by <paramref name="utf8PropertyName"/>.
 186        /// </summary>
 187        /// <remarks>
 188        ///   <para>
 189        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 190        ///   </para>
 191        ///
 192        ///   <para>
 193        ///     If a property is defined multiple times for the same object, the last such definition is
 194        ///     what is matched.
 195        ///   </para>
 196        /// </remarks>
 197        /// <param name="utf8PropertyName">
 198        ///   The UTF-8 (with no Byte-Order-Mark (BOM)) representation of the name of the property to return.
 199        /// </param>
 200        /// <returns>
 201        ///   A <see cref="JsonElement"/> representing the value of the requested property.
 202        /// </returns>
 203        /// <exception cref="InvalidOperationException">
 204        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 205        /// </exception>
 206        /// <exception cref="KeyNotFoundException">
 207        ///   No property was found with the requested name.
 208        /// </exception>
 209        /// <exception cref="ObjectDisposedException">
 210        ///   The parent <see cref="JsonDocument"/> has been disposed.
 211        /// </exception>
 212        /// <seealso cref="EnumerateObject"/>
 213        public JsonElement GetProperty(ReadOnlySpan<byte> utf8PropertyName)
 0214        {
 0215            if (TryGetProperty(utf8PropertyName, out JsonElement property))
 0216            {
 0217                return property;
 218            }
 219
 0220            throw new KeyNotFoundException();
 0221        }
 222
 223        /// <summary>
 224        ///   Looks for a property named <paramref name="propertyName"/> in the current object, returning
 225        ///   whether or not such a property existed. When the property exists <paramref name="value"/>
 226        ///   is assigned to the value of that property.
 227        /// </summary>
 228        /// <remarks>
 229        ///   <para>
 230        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 231        ///   </para>
 232        ///
 233        ///   <para>
 234        ///     If a property is defined multiple times for the same object, the last such definition is
 235        ///     what is matched.
 236        ///   </para>
 237        /// </remarks>
 238        /// <param name="propertyName">Name of the property to find.</param>
 239        /// <param name="value">Receives the value of the located property.</param>
 240        /// <returns>
 241        ///   <see langword="true"/> if the property was found, <see langword="false"/> otherwise.
 242        /// </returns>
 243        /// <exception cref="InvalidOperationException">
 244        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 245        /// </exception>
 246        /// <exception cref="ArgumentNullException">
 247        ///   <paramref name="propertyName"/> is <see langword="null"/>.
 248        /// </exception>
 249        /// <exception cref="ObjectDisposedException">
 250        ///   The parent <see cref="JsonDocument"/> has been disposed.
 251        /// </exception>
 252        /// <seealso cref="EnumerateObject"/>
 253        public bool TryGetProperty(string propertyName, out JsonElement value)
 0254        {
 0255            ArgumentNullException.ThrowIfNull(propertyName);
 256
 0257            return TryGetProperty(propertyName.AsSpan(), out value);
 0258        }
 259
 260        /// <summary>
 261        ///   Looks for a property named <paramref name="propertyName"/> in the current object, returning
 262        ///   whether or not such a property existed. When the property exists <paramref name="value"/>
 263        ///   is assigned to the value of that property.
 264        /// </summary>
 265        /// <remarks>
 266        ///   <para>
 267        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 268        ///   </para>
 269        ///
 270        ///   <para>
 271        ///     If a property is defined multiple times for the same object, the last such definition is
 272        ///     what is matched.
 273        ///   </para>
 274        /// </remarks>
 275        /// <param name="propertyName">Name of the property to find.</param>
 276        /// <param name="value">Receives the value of the located property.</param>
 277        /// <returns>
 278        ///   <see langword="true"/> if the property was found, <see langword="false"/> otherwise.
 279        /// </returns>
 280        /// <seealso cref="EnumerateObject"/>
 281        /// <exception cref="InvalidOperationException">
 282        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 283        /// </exception>
 284        /// <exception cref="ObjectDisposedException">
 285        ///   The parent <see cref="JsonDocument"/> has been disposed.
 286        /// </exception>
 287        public bool TryGetProperty(ReadOnlySpan<char> propertyName, out JsonElement value)
 0288        {
 0289            CheckValidInstance();
 290
 0291            return _parent.TryGetNamedPropertyValue(_idx, propertyName, out value);
 0292        }
 293
 294        /// <summary>
 295        ///   Looks for a property named <paramref name="utf8PropertyName"/> in the current object, returning
 296        ///   whether or not such a property existed. When the property exists <paramref name="value"/>
 297        ///   is assigned to the value of that property.
 298        /// </summary>
 299        /// <remarks>
 300        ///   <para>
 301        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 302        ///   </para>
 303        ///
 304        ///   <para>
 305        ///     If a property is defined multiple times for the same object, the last such definition is
 306        ///     what is matched.
 307        ///   </para>
 308        /// </remarks>
 309        /// <param name="utf8PropertyName">
 310        ///   The UTF-8 (with no Byte-Order-Mark (BOM)) representation of the name of the property to return.
 311        /// </param>
 312        /// <param name="value">Receives the value of the located property.</param>
 313        /// <returns>
 314        ///   <see langword="true"/> if the property was found, <see langword="false"/> otherwise.
 315        /// </returns>
 316        /// <seealso cref="EnumerateObject"/>
 317        /// <exception cref="InvalidOperationException">
 318        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 319        /// </exception>
 320        /// <exception cref="ObjectDisposedException">
 321        ///   The parent <see cref="JsonDocument"/> has been disposed.
 322        /// </exception>
 323        public bool TryGetProperty(ReadOnlySpan<byte> utf8PropertyName, out JsonElement value)
 0324        {
 0325            CheckValidInstance();
 326
 0327            return _parent.TryGetNamedPropertyValue(_idx, utf8PropertyName, out value);
 0328        }
 329
 330        /// <summary>
 331        ///   Gets the value of the element as a <see cref="bool"/>.
 332        /// </summary>
 333        /// <remarks>
 334        ///   This method does not parse the contents of a JSON string value.
 335        /// </remarks>
 336        /// <returns>The value of the element as a <see cref="bool"/>.</returns>
 337        /// <exception cref="InvalidOperationException">
 338        ///   This value's <see cref="ValueKind"/> is neither <see cref="JsonValueKind.True"/> or
 339        ///   <see cref="JsonValueKind.False"/>.
 340        /// </exception>
 341        /// <exception cref="ObjectDisposedException">
 342        ///   The parent <see cref="JsonDocument"/> has been disposed.
 343        /// </exception>
 344        public bool GetBoolean()
 0345        {
 346            // CheckValidInstance is redundant.  Asking for the type will
 347            // return None, which then throws the same exception in the return statement.
 348
 0349            JsonTokenType type = TokenType;
 350
 0351            return
 0352                type == JsonTokenType.True ? true :
 0353                type == JsonTokenType.False ? false :
 0354                ThrowJsonElementWrongTypeException(type);
 355
 356            static bool ThrowJsonElementWrongTypeException(JsonTokenType actualType)
 0357            {
 0358                throw ThrowHelper.GetJsonElementWrongTypeException(nameof(Boolean), actualType.ToValueKind());
 359            }
 0360        }
 361
 362        /// <summary>
 363        ///   Gets the value of the element as a <see cref="string"/>.
 364        /// </summary>
 365        /// <remarks>
 366        ///   This method does not create a string representation of values other than JSON strings.
 367        /// </remarks>
 368        /// <returns>The value of the element as a <see cref="string"/>.</returns>
 369        /// <exception cref="InvalidOperationException">
 370        ///   This value's <see cref="ValueKind"/> is neither <see cref="JsonValueKind.String"/> nor <see cref="JsonValu
 371        /// </exception>
 372        /// <exception cref="ObjectDisposedException">
 373        ///   The parent <see cref="JsonDocument"/> has been disposed.
 374        /// </exception>
 375        /// <seealso cref="ToString"/>
 376        public string? GetString()
 0377        {
 0378            CheckValidInstance();
 379
 0380            return _parent.GetString(_idx, JsonTokenType.String);
 0381        }
 382
 383        /// <summary>
 384        ///   Attempts to represent the current JSON string as bytes assuming it is Base64 encoded.
 385        /// </summary>
 386        /// <param name="value">Receives the value.</param>
 387        /// <remarks>
 388        ///  This method does not create a byte[] representation of values other than base 64 encoded JSON strings.
 389        /// </remarks>
 390        /// <returns>
 391        ///   <see langword="true"/> if the entire token value is encoded as valid Base64 text and can be successfully d
 392        ///   <see langword="false"/> otherwise.
 393        /// </returns>
 394        /// <exception cref="InvalidOperationException">
 395        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 396        /// </exception>
 397        /// <exception cref="ObjectDisposedException">
 398        ///   The parent <see cref="JsonDocument"/> has been disposed.
 399        /// </exception>
 400        public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value)
 0401        {
 0402            CheckValidInstance();
 403
 0404            return _parent.TryGetValue(_idx, out value);
 0405        }
 406
 407        /// <summary>
 408        ///   Gets the value of the element as bytes.
 409        /// </summary>
 410        /// <remarks>
 411        ///   This method does not create a byte[] representation of values other than Base64 encoded JSON strings.
 412        /// </remarks>
 413        /// <returns>The value decode to bytes.</returns>
 414        /// <exception cref="InvalidOperationException">
 415        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 416        /// </exception>
 417        /// <exception cref="FormatException">
 418        ///   The value is not encoded as Base64 text and hence cannot be decoded to bytes.
 419        /// </exception>
 420        /// <exception cref="ObjectDisposedException">
 421        ///   The parent <see cref="JsonDocument"/> has been disposed.
 422        /// </exception>
 423        /// <seealso cref="ToString"/>
 424        public byte[] GetBytesFromBase64()
 0425        {
 0426            if (!TryGetBytesFromBase64(out byte[]? value))
 0427            {
 0428                ThrowHelper.ThrowFormatException();
 429            }
 430
 0431            return value;
 0432        }
 433
 434        /// <summary>
 435        ///   Attempts to represent the current JSON number as an <see cref="sbyte"/>.
 436        /// </summary>
 437        /// <param name="value">Receives the value.</param>
 438        /// <remarks>
 439        ///   This method does not parse the contents of a JSON string value.
 440        /// </remarks>
 441        /// <returns>
 442        ///   <see langword="true"/> if the number can be represented as an <see cref="sbyte"/>,
 443        ///   <see langword="false"/> otherwise.
 444        /// </returns>
 445        /// <exception cref="InvalidOperationException">
 446        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 447        /// </exception>
 448        /// <exception cref="ObjectDisposedException">
 449        ///   The parent <see cref="JsonDocument"/> has been disposed.
 450        /// </exception>
 451        [CLSCompliant(false)]
 452        public bool TryGetSByte(out sbyte value)
 0453        {
 0454            CheckValidInstance();
 455
 0456            return _parent.TryGetValue(_idx, out value);
 0457        }
 458
 459        /// <summary>
 460        ///   Gets the current JSON number as an <see cref="sbyte"/>.
 461        /// </summary>
 462        /// <returns>The current JSON number as an <see cref="sbyte"/>.</returns>
 463        /// <exception cref="InvalidOperationException">
 464        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 465        /// </exception>
 466        /// <exception cref="FormatException">
 467        ///   The value cannot be represented as an <see cref="sbyte"/>.
 468        /// </exception>
 469        /// <exception cref="ObjectDisposedException">
 470        ///   The parent <see cref="JsonDocument"/> has been disposed.
 471        /// </exception>
 472        [CLSCompliant(false)]
 473        public sbyte GetSByte()
 0474        {
 0475            if (TryGetSByte(out sbyte value))
 0476            {
 0477                return value;
 478            }
 479
 0480            throw new FormatException();
 0481        }
 482
 483        /// <summary>
 484        ///   Attempts to represent the current JSON number as a <see cref="byte"/>.
 485        /// </summary>
 486        /// <param name="value">Receives the value.</param>
 487        /// <remarks>
 488        ///   This method does not parse the contents of a JSON string value.
 489        /// </remarks>
 490        /// <returns>
 491        ///   <see langword="true"/> if the number can be represented as a <see cref="byte"/>,
 492        ///   <see langword="false"/> otherwise.
 493        /// </returns>
 494        /// <exception cref="InvalidOperationException">
 495        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 496        /// </exception>
 497        /// <exception cref="ObjectDisposedException">
 498        ///   The parent <see cref="JsonDocument"/> has been disposed.
 499        /// </exception>
 500        public bool TryGetByte(out byte value)
 0501        {
 0502            CheckValidInstance();
 503
 0504            return _parent.TryGetValue(_idx, out value);
 0505        }
 506
 507        /// <summary>
 508        ///   Gets the current JSON number as a <see cref="byte"/>.
 509        /// </summary>
 510        /// <returns>The current JSON number as a <see cref="byte"/>.</returns>
 511        /// <remarks>
 512        ///   This method does not parse the contents of a JSON string value.
 513        /// </remarks>
 514        /// <exception cref="InvalidOperationException">
 515        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 516        /// </exception>
 517        /// <exception cref="FormatException">
 518        ///   The value cannot be represented as a <see cref="byte"/>.
 519        /// </exception>
 520        /// <exception cref="ObjectDisposedException">
 521        ///   The parent <see cref="JsonDocument"/> has been disposed.
 522        /// </exception>
 523        public byte GetByte()
 0524        {
 0525            if (TryGetByte(out byte value))
 0526            {
 0527                return value;
 528            }
 529
 0530            throw new FormatException();
 0531        }
 532
 533        /// <summary>
 534        ///   Attempts to represent the current JSON number as an <see cref="short"/>.
 535        /// </summary>
 536        /// <param name="value">Receives the value.</param>
 537        /// <remarks>
 538        ///   This method does not parse the contents of a JSON string value.
 539        /// </remarks>
 540        /// <returns>
 541        ///   <see langword="true"/> if the number can be represented as an <see cref="short"/>,
 542        ///   <see langword="false"/> otherwise.
 543        /// </returns>
 544        /// <exception cref="InvalidOperationException">
 545        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 546        /// </exception>
 547        /// <exception cref="ObjectDisposedException">
 548        ///   The parent <see cref="JsonDocument"/> has been disposed.
 549        /// </exception>
 550        public bool TryGetInt16(out short value)
 0551        {
 0552            CheckValidInstance();
 553
 0554            return _parent.TryGetValue(_idx, out value);
 0555        }
 556
 557        /// <summary>
 558        ///   Gets the current JSON number as an <see cref="short"/>.
 559        /// </summary>
 560        /// <returns>The current JSON number as an <see cref="short"/>.</returns>
 561        /// <exception cref="InvalidOperationException">
 562        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 563        /// </exception>
 564        /// <exception cref="FormatException">
 565        ///   The value cannot be represented as an <see cref="short"/>.
 566        /// </exception>
 567        /// <exception cref="ObjectDisposedException">
 568        ///   The parent <see cref="JsonDocument"/> has been disposed.
 569        /// </exception>
 570        public short GetInt16()
 0571        {
 0572            if (TryGetInt16(out short value))
 0573            {
 0574                return value;
 575            }
 576
 0577            throw new FormatException();
 0578        }
 579
 580        /// <summary>
 581        ///   Attempts to represent the current JSON number as a <see cref="ushort"/>.
 582        /// </summary>
 583        /// <param name="value">Receives the value.</param>
 584        /// <remarks>
 585        ///   This method does not parse the contents of a JSON string value.
 586        /// </remarks>
 587        /// <returns>
 588        ///   <see langword="true"/> if the number can be represented as a <see cref="ushort"/>,
 589        ///   <see langword="false"/> otherwise.
 590        /// </returns>
 591        /// <exception cref="InvalidOperationException">
 592        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 593        /// </exception>
 594        /// <exception cref="ObjectDisposedException">
 595        ///   The parent <see cref="JsonDocument"/> has been disposed.
 596        /// </exception>
 597        [CLSCompliant(false)]
 598        public bool TryGetUInt16(out ushort value)
 0599        {
 0600            CheckValidInstance();
 601
 0602            return _parent.TryGetValue(_idx, out value);
 0603        }
 604
 605        /// <summary>
 606        ///   Gets the current JSON number as a <see cref="ushort"/>.
 607        /// </summary>
 608        /// <returns>The current JSON number as a <see cref="ushort"/>.</returns>
 609        /// <remarks>
 610        ///   This method does not parse the contents of a JSON string value.
 611        /// </remarks>
 612        /// <exception cref="InvalidOperationException">
 613        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 614        /// </exception>
 615        /// <exception cref="FormatException">
 616        ///   The value cannot be represented as a <see cref="ushort"/>.
 617        /// </exception>
 618        /// <exception cref="ObjectDisposedException">
 619        ///   The parent <see cref="JsonDocument"/> has been disposed.
 620        /// </exception>
 621        [CLSCompliant(false)]
 622        public ushort GetUInt16()
 0623        {
 0624            if (TryGetUInt16(out ushort value))
 0625            {
 0626                return value;
 627            }
 628
 0629            throw new FormatException();
 0630        }
 631
 632        /// <summary>
 633        ///   Attempts to represent the current JSON number as an <see cref="int"/>.
 634        /// </summary>
 635        /// <param name="value">Receives the value.</param>
 636        /// <remarks>
 637        ///   This method does not parse the contents of a JSON string value.
 638        /// </remarks>
 639        /// <returns>
 640        ///   <see langword="true"/> if the number can be represented as an <see cref="int"/>,
 641        ///   <see langword="false"/> otherwise.
 642        /// </returns>
 643        /// <exception cref="InvalidOperationException">
 644        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 645        /// </exception>
 646        /// <exception cref="ObjectDisposedException">
 647        ///   The parent <see cref="JsonDocument"/> has been disposed.
 648        /// </exception>
 649        public bool TryGetInt32(out int value)
 0650        {
 0651            CheckValidInstance();
 652
 0653            return _parent.TryGetValue(_idx, out value);
 0654        }
 655
 656        /// <summary>
 657        ///   Gets the current JSON number as an <see cref="int"/>.
 658        /// </summary>
 659        /// <returns>The current JSON number as an <see cref="int"/>.</returns>
 660        /// <exception cref="InvalidOperationException">
 661        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 662        /// </exception>
 663        /// <exception cref="FormatException">
 664        ///   The value cannot be represented as an <see cref="int"/>.
 665        /// </exception>
 666        /// <exception cref="ObjectDisposedException">
 667        ///   The parent <see cref="JsonDocument"/> has been disposed.
 668        /// </exception>
 669        public int GetInt32()
 0670        {
 0671            if (!TryGetInt32(out int value))
 0672            {
 0673                ThrowHelper.ThrowFormatException();
 674            }
 675
 0676            return value;
 0677        }
 678
 679        /// <summary>
 680        ///   Attempts to represent the current JSON number as a <see cref="uint"/>.
 681        /// </summary>
 682        /// <param name="value">Receives the value.</param>
 683        /// <remarks>
 684        ///   This method does not parse the contents of a JSON string value.
 685        /// </remarks>
 686        /// <returns>
 687        ///   <see langword="true"/> if the number can be represented as a <see cref="uint"/>,
 688        ///   <see langword="false"/> otherwise.
 689        /// </returns>
 690        /// <exception cref="InvalidOperationException">
 691        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 692        /// </exception>
 693        /// <exception cref="ObjectDisposedException">
 694        ///   The parent <see cref="JsonDocument"/> has been disposed.
 695        /// </exception>
 696        [CLSCompliant(false)]
 697        public bool TryGetUInt32(out uint value)
 0698        {
 0699            CheckValidInstance();
 700
 0701            return _parent.TryGetValue(_idx, out value);
 0702        }
 703
 704        /// <summary>
 705        ///   Gets the current JSON number as a <see cref="uint"/>.
 706        /// </summary>
 707        /// <returns>The current JSON number as a <see cref="uint"/>.</returns>
 708        /// <remarks>
 709        ///   This method does not parse the contents of a JSON string value.
 710        /// </remarks>
 711        /// <exception cref="InvalidOperationException">
 712        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 713        /// </exception>
 714        /// <exception cref="FormatException">
 715        ///   The value cannot be represented as a <see cref="uint"/>.
 716        /// </exception>
 717        /// <exception cref="ObjectDisposedException">
 718        ///   The parent <see cref="JsonDocument"/> has been disposed.
 719        /// </exception>
 720        [CLSCompliant(false)]
 721        public uint GetUInt32()
 0722        {
 0723            if (!TryGetUInt32(out uint value))
 0724            {
 0725                ThrowHelper.ThrowFormatException();
 726            }
 727
 0728            return value;
 0729        }
 730
 731        /// <summary>
 732        ///   Attempts to represent the current JSON number as a <see cref="long"/>.
 733        /// </summary>
 734        /// <param name="value">Receives the value.</param>
 735        /// <remarks>
 736        ///   This method does not parse the contents of a JSON string value.
 737        /// </remarks>
 738        /// <returns>
 739        ///   <see langword="true"/> if the number can be represented as a <see cref="long"/>,
 740        ///   <see langword="false"/> otherwise.
 741        /// </returns>
 742        /// <exception cref="InvalidOperationException">
 743        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 744        /// </exception>
 745        /// <exception cref="ObjectDisposedException">
 746        ///   The parent <see cref="JsonDocument"/> has been disposed.
 747        /// </exception>
 748        public bool TryGetInt64(out long value)
 0749        {
 0750            CheckValidInstance();
 751
 0752            return _parent.TryGetValue(_idx, out value);
 0753        }
 754
 755        /// <summary>
 756        ///   Gets the current JSON number as a <see cref="long"/>.
 757        /// </summary>
 758        /// <returns>The current JSON number as a <see cref="long"/>.</returns>
 759        /// <remarks>
 760        ///   This method does not parse the contents of a JSON string value.
 761        /// </remarks>
 762        /// <exception cref="InvalidOperationException">
 763        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 764        /// </exception>
 765        /// <exception cref="FormatException">
 766        ///   The value cannot be represented as a <see cref="long"/>.
 767        /// </exception>
 768        /// <exception cref="ObjectDisposedException">
 769        ///   The parent <see cref="JsonDocument"/> has been disposed.
 770        /// </exception>
 771        public long GetInt64()
 0772        {
 0773            if (!TryGetInt64(out long value))
 0774            {
 0775                ThrowHelper.ThrowFormatException();
 776            }
 777
 0778            return value;
 0779        }
 780
 781        /// <summary>
 782        ///   Attempts to represent the current JSON number as a <see cref="ulong"/>.
 783        /// </summary>
 784        /// <param name="value">Receives the value.</param>
 785        /// <remarks>
 786        ///   This method does not parse the contents of a JSON string value.
 787        /// </remarks>
 788        /// <returns>
 789        ///   <see langword="true"/> if the number can be represented as a <see cref="ulong"/>,
 790        ///   <see langword="false"/> otherwise.
 791        /// </returns>
 792        /// <exception cref="InvalidOperationException">
 793        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 794        /// </exception>
 795        /// <exception cref="ObjectDisposedException">
 796        ///   The parent <see cref="JsonDocument"/> has been disposed.
 797        /// </exception>
 798        [CLSCompliant(false)]
 799        public bool TryGetUInt64(out ulong value)
 0800        {
 0801            CheckValidInstance();
 802
 0803            return _parent.TryGetValue(_idx, out value);
 0804        }
 805
 806        /// <summary>
 807        ///   Gets the current JSON number as a <see cref="ulong"/>.
 808        /// </summary>
 809        /// <returns>The current JSON number as a <see cref="ulong"/>.</returns>
 810        /// <remarks>
 811        ///   This method does not parse the contents of a JSON string value.
 812        /// </remarks>
 813        /// <exception cref="InvalidOperationException">
 814        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 815        /// </exception>
 816        /// <exception cref="FormatException">
 817        ///   The value cannot be represented as a <see cref="ulong"/>.
 818        /// </exception>
 819        /// <exception cref="ObjectDisposedException">
 820        ///   The parent <see cref="JsonDocument"/> has been disposed.
 821        /// </exception>
 822        [CLSCompliant(false)]
 823        public ulong GetUInt64()
 0824        {
 0825            if (!TryGetUInt64(out ulong value))
 0826            {
 0827                ThrowHelper.ThrowFormatException();
 828            }
 829
 0830            return value;
 0831        }
 832
 833        /// <summary>
 834        ///   Attempts to represent the current JSON number as a <see cref="double"/>.
 835        /// </summary>
 836        /// <param name="value">Receives the value.</param>
 837        /// <remarks>
 838        ///   <para>
 839        ///     This method does not parse the contents of a JSON string value.
 840        ///   </para>
 841        ///
 842        ///   <para>
 843        ///     On .NET Core this method does not return <see langword="false"/> for values larger than
 844        ///     <see cref="double.MaxValue"/> (or smaller than <see cref="double.MinValue"/>),
 845        ///     instead <see langword="true"/> is returned and <see cref="double.PositiveInfinity"/> (or
 846        ///     <see cref="double.NegativeInfinity"/>) is emitted.
 847        ///   </para>
 848        /// </remarks>
 849        /// <returns>
 850        ///   <see langword="true"/> if the number can be represented as a <see cref="double"/>,
 851        ///   <see langword="false"/> otherwise.
 852        /// </returns>
 853        /// <exception cref="InvalidOperationException">
 854        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 855        /// </exception>
 856        /// <exception cref="ObjectDisposedException">
 857        ///   The parent <see cref="JsonDocument"/> has been disposed.
 858        /// </exception>
 859        public bool TryGetDouble(out double value)
 0860        {
 0861            CheckValidInstance();
 862
 0863            return _parent.TryGetValue(_idx, out value);
 0864        }
 865
 866        /// <summary>
 867        ///   Gets the current JSON number as a <see cref="double"/>.
 868        /// </summary>
 869        /// <returns>The current JSON number as a <see cref="double"/>.</returns>
 870        /// <remarks>
 871        ///   <para>
 872        ///     This method does not parse the contents of a JSON string value.
 873        ///   </para>
 874        ///
 875        ///   <para>
 876        ///     On .NET Core this method returns <see cref="double.PositiveInfinity"/> (or
 877        ///     <see cref="double.NegativeInfinity"/>) for values larger than
 878        ///     <see cref="double.MaxValue"/> (or smaller than <see cref="double.MinValue"/>).
 879        ///   </para>
 880        /// </remarks>
 881        /// <exception cref="InvalidOperationException">
 882        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 883        /// </exception>
 884        /// <exception cref="FormatException">
 885        ///   The value cannot be represented as a <see cref="double"/>.
 886        /// </exception>
 887        /// <exception cref="ObjectDisposedException">
 888        ///   The parent <see cref="JsonDocument"/> has been disposed.
 889        /// </exception>
 890        public double GetDouble()
 0891        {
 0892            if (!TryGetDouble(out double value))
 0893            {
 0894                ThrowHelper.ThrowFormatException();
 895            }
 896
 0897            return value;
 0898        }
 899
 900        /// <summary>
 901        ///   Attempts to represent the current JSON number as a <see cref="float"/>.
 902        /// </summary>
 903        /// <param name="value">Receives the value.</param>
 904        /// <remarks>
 905        ///   <para>
 906        ///     This method does not parse the contents of a JSON string value.
 907        ///   </para>
 908        ///
 909        ///   <para>
 910        ///     On .NET Core this method does not return <see langword="false"/> for values larger than
 911        ///     <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>),
 912        ///     instead <see langword="true"/> is returned and <see cref="float.PositiveInfinity"/> (or
 913        ///     <see cref="float.NegativeInfinity"/>) is emitted.
 914        ///   </para>
 915        /// </remarks>
 916        /// <returns>
 917        ///   <see langword="true"/> if the number can be represented as a <see cref="float"/>,
 918        ///   <see langword="false"/> otherwise.
 919        /// </returns>
 920        /// <exception cref="InvalidOperationException">
 921        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 922        /// </exception>
 923        /// <exception cref="ObjectDisposedException">
 924        ///   The parent <see cref="JsonDocument"/> has been disposed.
 925        /// </exception>
 926        public bool TryGetSingle(out float value)
 0927        {
 0928            CheckValidInstance();
 929
 0930            return _parent.TryGetValue(_idx, out value);
 0931        }
 932
 933        /// <summary>
 934        ///   Gets the current JSON number as a <see cref="float"/>.
 935        /// </summary>
 936        /// <returns>The current JSON number as a <see cref="float"/>.</returns>
 937        /// <remarks>
 938        ///   <para>
 939        ///     This method does not parse the contents of a JSON string value.
 940        ///   </para>
 941        ///
 942        ///   <para>
 943        ///     On .NET Core this method returns <see cref="float.PositiveInfinity"/> (or
 944        ///     <see cref="float.NegativeInfinity"/>) for values larger than
 945        ///     <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>).
 946        ///   </para>
 947        /// </remarks>
 948        /// <exception cref="InvalidOperationException">
 949        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 950        /// </exception>
 951        /// <exception cref="FormatException">
 952        ///   The value cannot be represented as a <see cref="float"/>.
 953        /// </exception>
 954        /// <exception cref="ObjectDisposedException">
 955        ///   The parent <see cref="JsonDocument"/> has been disposed.
 956        /// </exception>
 957        public float GetSingle()
 0958        {
 0959            if (!TryGetSingle(out float value))
 0960            {
 0961                ThrowHelper.ThrowFormatException();
 962            }
 963
 0964            return value;
 0965        }
 966
 967        /// <summary>
 968        ///   Attempts to represent the current JSON number as a <see cref="decimal"/>.
 969        /// </summary>
 970        /// <param name="value">Receives the value.</param>
 971        /// <remarks>
 972        ///   This method does not parse the contents of a JSON string value.
 973        /// </remarks>
 974        /// <returns>
 975        ///   <see langword="true"/> if the number can be represented as a <see cref="decimal"/>,
 976        ///   <see langword="false"/> otherwise.
 977        /// </returns>
 978        /// <exception cref="InvalidOperationException">
 979        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 980        /// </exception>
 981        /// <exception cref="ObjectDisposedException">
 982        ///   The parent <see cref="JsonDocument"/> has been disposed.
 983        /// </exception>
 984        /// <seealso cref="GetRawText"/>
 985        public bool TryGetDecimal(out decimal value)
 0986        {
 0987            CheckValidInstance();
 988
 0989            return _parent.TryGetValue(_idx, out value);
 0990        }
 991
 992        /// <summary>
 993        ///   Gets the current JSON number as a <see cref="decimal"/>.
 994        /// </summary>
 995        /// <returns>The current JSON number as a <see cref="decimal"/>.</returns>
 996        /// <remarks>
 997        ///   This method does not parse the contents of a JSON string value.
 998        /// </remarks>
 999        /// <exception cref="InvalidOperationException">
 1000        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 1001        /// </exception>
 1002        /// <exception cref="FormatException">
 1003        ///   The value cannot be represented as a <see cref="decimal"/>.
 1004        /// </exception>
 1005        /// <exception cref="ObjectDisposedException">
 1006        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1007        /// </exception>
 1008        /// <seealso cref="GetRawText"/>
 1009        public decimal GetDecimal()
 01010        {
 01011            if (!TryGetDecimal(out decimal value))
 01012            {
 01013                ThrowHelper.ThrowFormatException();
 1014            }
 1015
 01016            return value;
 01017        }
 1018
 1019        /// <summary>
 1020        ///   Attempts to represent the current JSON string as a <see cref="DateTime"/>.
 1021        /// </summary>
 1022        /// <param name="value">Receives the value.</param>
 1023        /// <remarks>
 1024        ///   This method does not create a DateTime representation of values other than JSON strings.
 1025        /// </remarks>
 1026        /// <returns>
 1027        ///   <see langword="true"/> if the string can be represented as a <see cref="DateTime"/>,
 1028        ///   <see langword="false"/> otherwise.
 1029        /// </returns>
 1030        /// <exception cref="InvalidOperationException">
 1031        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1032        /// </exception>
 1033        /// <exception cref="ObjectDisposedException">
 1034        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1035        /// </exception>
 1036        public bool TryGetDateTime(out DateTime value)
 01037        {
 01038            CheckValidInstance();
 1039
 01040            return _parent.TryGetValue(_idx, out value);
 01041        }
 1042
 1043        /// <summary>
 1044        ///   Gets the value of the element as a <see cref="DateTime"/>.
 1045        /// </summary>
 1046        /// <remarks>
 1047        ///   This method does not create a DateTime representation of values other than JSON strings.
 1048        /// </remarks>
 1049        /// <returns>The value of the element as a <see cref="DateTime"/>.</returns>
 1050        /// <exception cref="InvalidOperationException">
 1051        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1052        /// </exception>
 1053        /// <exception cref="FormatException">
 1054        ///   The value cannot be represented as a <see cref="DateTime"/>.
 1055        /// </exception>
 1056        /// <exception cref="ObjectDisposedException">
 1057        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1058        /// </exception>
 1059        /// <seealso cref="ToString"/>
 1060        public DateTime GetDateTime()
 01061        {
 01062            if (!TryGetDateTime(out DateTime value))
 01063            {
 01064                ThrowHelper.ThrowFormatException();
 1065            }
 1066
 01067            return value;
 01068        }
 1069
 1070        /// <summary>
 1071        ///   Attempts to represent the current JSON string as a <see cref="DateTimeOffset"/>.
 1072        /// </summary>
 1073        /// <param name="value">Receives the value.</param>
 1074        /// <remarks>
 1075        ///   This method does not create a DateTimeOffset representation of values other than JSON strings.
 1076        /// </remarks>
 1077        /// <returns>
 1078        ///   <see langword="true"/> if the string can be represented as a <see cref="DateTimeOffset"/>,
 1079        ///   <see langword="false"/> otherwise.
 1080        /// </returns>
 1081        /// <exception cref="InvalidOperationException">
 1082        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1083        /// </exception>
 1084        /// <exception cref="ObjectDisposedException">
 1085        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1086        /// </exception>
 1087        public bool TryGetDateTimeOffset(out DateTimeOffset value)
 01088        {
 01089            CheckValidInstance();
 1090
 01091            return _parent.TryGetValue(_idx, out value);
 01092        }
 1093
 1094        /// <summary>
 1095        ///   Gets the value of the element as a <see cref="DateTimeOffset"/>.
 1096        /// </summary>
 1097        /// <remarks>
 1098        ///   This method does not create a DateTimeOffset representation of values other than JSON strings.
 1099        /// </remarks>
 1100        /// <returns>The value of the element as a <see cref="DateTimeOffset"/>.</returns>
 1101        /// <exception cref="InvalidOperationException">
 1102        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1103        /// </exception>
 1104        /// <exception cref="FormatException">
 1105        ///   The value cannot be represented as a <see cref="DateTimeOffset"/>.
 1106        /// </exception>
 1107        /// <exception cref="ObjectDisposedException">
 1108        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1109        /// </exception>
 1110        /// <seealso cref="ToString"/>
 1111        public DateTimeOffset GetDateTimeOffset()
 01112        {
 01113            if (!TryGetDateTimeOffset(out DateTimeOffset value))
 01114            {
 01115                ThrowHelper.ThrowFormatException();
 1116            }
 1117
 01118            return value;
 01119        }
 1120
 1121        /// <summary>
 1122        ///   Attempts to represent the current JSON string as a <see cref="Guid"/>.
 1123        /// </summary>
 1124        /// <param name="value">Receives the value.</param>
 1125        /// <remarks>
 1126        ///   This method does not create a Guid representation of values other than JSON strings.
 1127        /// </remarks>
 1128        /// <returns>
 1129        ///   <see langword="true"/> if the string can be represented as a <see cref="Guid"/>,
 1130        ///   <see langword="false"/> otherwise.
 1131        /// </returns>
 1132        /// <exception cref="InvalidOperationException">
 1133        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1134        /// </exception>
 1135        /// <exception cref="ObjectDisposedException">
 1136        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1137        /// </exception>
 1138        public bool TryGetGuid(out Guid value)
 01139        {
 01140            CheckValidInstance();
 1141
 01142            return _parent.TryGetValue(_idx, out value);
 01143        }
 1144
 1145        /// <summary>
 1146        ///   Gets the value of the element as a <see cref="Guid"/>.
 1147        /// </summary>
 1148        /// <remarks>
 1149        ///   This method does not create a Guid representation of values other than JSON strings.
 1150        /// </remarks>
 1151        /// <returns>The value of the element as a <see cref="Guid"/>.</returns>
 1152        /// <exception cref="InvalidOperationException">
 1153        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1154        /// </exception>
 1155        /// <exception cref="FormatException">
 1156        ///   The value cannot be represented as a <see cref="Guid"/>.
 1157        /// </exception>
 1158        /// <exception cref="ObjectDisposedException">
 1159        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1160        /// </exception>
 1161        /// <seealso cref="ToString"/>
 1162        public Guid GetGuid()
 01163        {
 01164            if (!TryGetGuid(out Guid value))
 01165            {
 01166                ThrowHelper.ThrowFormatException();
 1167            }
 1168
 01169            return value;
 01170        }
 1171
 1172        internal string GetPropertyName()
 01173        {
 01174            CheckValidInstance();
 1175
 01176            return _parent.GetNameOfPropertyValue(_idx);
 01177        }
 1178
 1179        internal ReadOnlySpan<byte> GetPropertyNameRaw()
 01180        {
 01181            CheckValidInstance();
 1182
 01183            return _parent.GetPropertyNameRaw(_idx);
 01184        }
 1185
 1186        /// <summary>
 1187        ///   Gets the original input data backing this value, returning it as a <see cref="string"/>.
 1188        /// </summary>
 1189        /// <returns>
 1190        ///   The original input data backing this value, returning it as a <see cref="string"/>.
 1191        /// </returns>
 1192        /// <exception cref="ObjectDisposedException">
 1193        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1194        /// </exception>
 1195        public string GetRawText()
 01196        {
 01197            CheckValidInstance();
 1198
 01199            return _parent.GetRawValueAsString(_idx);
 01200        }
 1201
 1202        internal ReadOnlyMemory<byte> GetRawValue()
 701203        {
 701204            CheckValidInstance();
 1205
 701206            return _parent.GetRawValue(_idx, includeQuotes: true);
 701207        }
 1208
 1209        internal string GetPropertyRawText()
 01210        {
 01211            CheckValidInstance();
 1212
 01213            return _parent.GetPropertyRawValueAsString(_idx);
 01214        }
 1215
 1216        internal bool ValueIsEscaped
 1217        {
 1218            get
 31219            {
 31220                CheckValidInstance();
 1221
 31222                return _parent.ValueIsEscaped(_idx, isPropertyName: false);
 31223            }
 1224        }
 1225
 1226        internal ReadOnlySpan<byte> ValueSpan
 1227        {
 1228            get
 31229            {
 31230                CheckValidInstance();
 1231
 31232                return _parent.GetRawValue(_idx, includeQuotes: false).Span;
 31233            }
 1234        }
 1235
 1236        /// <summary>
 1237        /// Compares the values of two <see cref="JsonElement"/> values for equality, including the values of all descen
 1238        /// </summary>
 1239        /// <param name="element1">The first <see cref="JsonElement"/> to compare.</param>
 1240        /// <param name="element2">The second <see cref="JsonElement"/> to compare.</param>
 1241        /// <returns><see langword="true"/> if the two values are equal; otherwise, <see langword="false"/>.</returns>
 1242        /// <remarks>
 1243        /// Deep equality of two JSON values is defined as follows:
 1244        /// <list type="bullet">
 1245        /// <item>JSON values of different kinds are not equal.</item>
 1246        /// <item>JSON constants <see langword="null"/>, <see langword="false"/>, and <see langword="true"/> only equal 
 1247        /// <item>JSON numbers are equal if and only if they have they have equivalent decimal representations, with no 
 1248        /// <item>JSON strings are equal if and only if they are equal using ordinal string comparison.</item>
 1249        /// <item>JSON arrays are equal if and only if they are of equal length and each of their elements are pairwise 
 1250        /// <item>
 1251        ///     JSON objects are equal if and only if they have the same number of properties and each property in the f
 1252        ///     has a corresponding property in the second object with the same name and equal value. The order of prope
 1253        ///     significant, with the exception of repeated properties that must be specified in the same order (with in
 1254        /// </item>
 1255        /// </list>
 1256        /// </remarks>
 1257        public static bool DeepEquals(JsonElement element1, JsonElement element2)
 381258        {
 381259            if (!StackHelper.TryEnsureSufficientExecutionStack())
 01260            {
 01261                ThrowHelper.ThrowInsufficientExecutionStackException_JsonElementDeepEqualsInsufficientExecutionStack();
 1262            }
 1263
 381264            element1.CheckValidInstance();
 381265            element2.CheckValidInstance();
 1266
 381267            JsonValueKind kind = element1.ValueKind;
 381268            if (kind != element2.ValueKind)
 01269            {
 01270                return false;
 1271            }
 1272
 381273            switch (kind)
 1274            {
 1275                case JsonValueKind.Null or JsonValueKind.False or JsonValueKind.True:
 01276                    return true;
 1277
 1278                case JsonValueKind.Number:
 351279                    return JsonHelpers.AreEqualJsonNumbers(element1.GetRawValue().Span, element2.GetRawValue().Span);
 1280
 1281                case JsonValueKind.String:
 31282                    if (element2.ValueIsEscaped)
 01283                    {
 01284                        if (element1.ValueIsEscaped)
 01285                        {
 1286                            // Need to unescape and compare both inputs.
 01287                            return JsonReaderHelper.UnescapeAndCompareBothInputs(element1.ValueSpan, element2.ValueSpan)
 1288                        }
 1289
 1290                        // Swap values so that unescaping is handled by the LHS.
 01291                        (element1, element2) = (element2, element1);
 01292                    }
 1293
 31294                    return element1.ValueEquals(element2.ValueSpan);
 1295
 1296                case JsonValueKind.Array:
 01297                    if (element1.GetArrayLength() != element2.GetArrayLength())
 01298                    {
 01299                        return false;
 1300                    }
 1301
 01302                    ArrayEnumerator arrayEnumerator2 = element2.EnumerateArray();
 01303                    foreach (JsonElement e1 in element1.EnumerateArray())
 01304                    {
 01305                        bool success = arrayEnumerator2.MoveNext();
 01306                        Debug.Assert(success, "enumerators must have matching length");
 1307
 01308                        if (!DeepEquals(e1, arrayEnumerator2.Current))
 01309                        {
 01310                            return false;
 1311                        }
 01312                    }
 1313
 01314                    Debug.Assert(!arrayEnumerator2.MoveNext());
 01315                    return true;
 1316
 1317                default:
 01318                    Debug.Assert(kind is JsonValueKind.Object);
 1319
 01320                    int count = element1.GetPropertyCount();
 01321                    if (count != element2.GetPropertyCount())
 01322                    {
 01323                        return false;
 1324                    }
 1325
 01326                    ObjectEnumerator objectEnumerator1 = element1.EnumerateObject();
 01327                    ObjectEnumerator objectEnumerator2 = element2.EnumerateObject();
 1328
 1329                    // Two JSON objects are considered equal if they define the same set of properties.
 1330                    // Start optimistically with pairwise comparison, but fall back to unordered
 1331                    // comparison as soon as a mismatch is encountered.
 1332
 01333                    while (objectEnumerator1.MoveNext())
 01334                    {
 01335                        bool success = objectEnumerator2.MoveNext();
 01336                        Debug.Assert(success, "enumerators should have matching lengths");
 1337
 01338                        JsonProperty prop1 = objectEnumerator1.Current;
 01339                        JsonProperty prop2 = objectEnumerator2.Current;
 1340
 01341                        if (!NameEquals(prop1, prop2))
 01342                        {
 1343                            // We have our first mismatch, fall back to unordered comparison.
 01344                            return UnorderedObjectDeepEquals(objectEnumerator1, objectEnumerator2, remainingProps: count
 1345                        }
 1346
 01347                        if (!DeepEquals(prop1.Value, prop2.Value))
 01348                        {
 01349                            return false;
 1350                        }
 1351
 01352                        count--;
 01353                    }
 1354
 01355                    Debug.Assert(!objectEnumerator2.MoveNext());
 01356                    return true;
 1357
 1358                    static bool UnorderedObjectDeepEquals(ObjectEnumerator objectEnumerator1, ObjectEnumerator objectEnu
 01359                    {
 1360                        // JsonElement objects allow duplicate property names, which is optional per the JSON RFC.
 1361                        // Even though this implementation of equality does not take property ordering into account,
 1362                        // repeated property names must be specified in the same order (although they may be interleaved
 1363                        // This is to preserve a degree of coherence with JSON serialization, where either the first
 1364                        // or last occurrence of a repeated property name is used. It also simplifies the implementation
 1365                        // and keeps it at O(n + m) complexity.
 1366
 01367                        Dictionary<string, ValueQueue<JsonElement>> properties2 = new(capacity: remainingProps, StringCo
 1368                        do
 01369                        {
 01370                            JsonProperty prop2 = objectEnumerator2.Current;
 1371#if NET
 01372                            ref ValueQueue<JsonElement> values = ref CollectionsMarshal.GetValueRefOrAddDefault(properti
 1373#else
 1374                            properties2.TryGetValue(prop2.Name, out ValueQueue<JsonElement> values);
 1375#endif
 01376                            values.Enqueue(prop2.Value);
 1377#if !NET
 1378                            properties2[prop2.Name] = values;
 1379#endif
 01380                        }
 01381                        while (objectEnumerator2.MoveNext());
 1382
 1383                        do
 01384                        {
 01385                            JsonProperty prop = objectEnumerator1.Current;
 1386#if NET
 01387                            ref ValueQueue<JsonElement> values = ref CollectionsMarshal.GetValueRefOrAddDefault(properti
 1388#else
 1389                            bool exists = properties2.TryGetValue(prop.Name, out ValueQueue<JsonElement> values);
 1390#endif
 01391                            if (!exists || !values.TryDequeue(out JsonElement value) || !DeepEquals(prop.Value, value))
 01392                            {
 01393                                return false;
 1394                            }
 1395#if !NET
 1396                            properties2[prop.Name] = values;
 1397#endif
 01398                        }
 01399                        while (objectEnumerator1.MoveNext());
 1400
 01401                        return true;
 01402                    }
 1403
 1404                    static bool NameEquals(JsonProperty left, JsonProperty right)
 01405                    {
 01406                        if (right.NameIsEscaped)
 01407                        {
 01408                            if (left.NameIsEscaped)
 01409                            {
 1410                                // Need to unescape and compare both inputs.
 01411                                return JsonReaderHelper.UnescapeAndCompareBothInputs(left.NameSpan, right.NameSpan);
 1412                            }
 1413
 1414                            // Swap values so that unescaping is handled by the LHS
 01415                            (left, right) = (right, left);
 01416                        }
 1417
 01418                        return left.NameEquals(right.NameSpan);
 01419                    }
 1420            }
 381421        }
 1422
 1423        /// <summary>
 1424        ///   Compares <paramref name="text" /> to the string value of this element.
 1425        /// </summary>
 1426        /// <param name="text">The text to compare against.</param>
 1427        /// <returns>
 1428        ///   <see langword="true" /> if the string value of this element matches <paramref name="text"/>,
 1429        ///   <see langword="false" /> otherwise.
 1430        /// </returns>
 1431        /// <exception cref="InvalidOperationException">
 1432        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1433        /// </exception>
 1434        /// <remarks>
 1435        ///   This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and
 1436        ///   the result of calling <see cref="GetString" />, but avoids creating the string instance.
 1437        /// </remarks>
 1438        public bool ValueEquals(string? text)
 01439        {
 1440            // CheckValidInstance is done in the helper
 1441
 01442            if (TokenType == JsonTokenType.Null)
 01443            {
 01444                return text == null;
 1445            }
 1446
 01447            return TextEqualsHelper(text.AsSpan(), isPropertyName: false);
 01448        }
 1449
 1450        /// <summary>
 1451        ///   Compares the text represented by <paramref name="utf8Text" /> to the string value of this element.
 1452        /// </summary>
 1453        /// <param name="utf8Text">The UTF-8 encoded text to compare against.</param>
 1454        /// <returns>
 1455        ///   <see langword="true" /> if the string value of this element has the same UTF-8 encoding as
 1456        ///   <paramref name="utf8Text" />, <see langword="false" /> otherwise.
 1457        /// </returns>
 1458        /// <exception cref="InvalidOperationException">
 1459        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1460        /// </exception>
 1461        /// <remarks>
 1462        ///   This method is functionally equal to doing an ordinal comparison of the string produced by UTF-8 decoding
 1463        ///   <paramref name="utf8Text" /> with the result of calling <see cref="GetString" />, but avoids creating the
 1464        ///   string instances.
 1465        /// </remarks>
 1466        public bool ValueEquals(ReadOnlySpan<byte> utf8Text)
 31467        {
 1468            // CheckValidInstance is done in the helper
 1469
 31470            if (TokenType == JsonTokenType.Null)
 01471            {
 1472                // This is different than Length == 0, in that it tests true for null, but false for ""
 1473#pragma warning disable CA2265
 01474                return utf8Text.Slice(0, 0) == default;
 1475#pragma warning restore CA2265
 1476            }
 1477
 31478            return TextEqualsHelper(utf8Text, isPropertyName: false, shouldUnescape: true);
 31479        }
 1480
 1481        /// <summary>
 1482        ///   Compares <paramref name="text" /> to the string value of this element.
 1483        /// </summary>
 1484        /// <param name="text">The text to compare against.</param>
 1485        /// <returns>
 1486        ///   <see langword="true" /> if the string value of this element matches <paramref name="text"/>,
 1487        ///   <see langword="false" /> otherwise.
 1488        /// </returns>
 1489        /// <exception cref="InvalidOperationException">
 1490        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1491        /// </exception>
 1492        /// <remarks>
 1493        ///   This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and
 1494        ///   the result of calling <see cref="GetString" />, but avoids creating the string instance.
 1495        /// </remarks>
 1496        public bool ValueEquals(ReadOnlySpan<char> text)
 01497        {
 1498            // CheckValidInstance is done in the helper
 1499
 01500            if (TokenType == JsonTokenType.Null)
 01501            {
 1502                // This is different than Length == 0, in that it tests true for null, but false for ""
 1503#pragma warning disable CA2265
 01504                return text.Slice(0, 0) == default;
 1505#pragma warning restore CA2265
 1506            }
 1507
 01508            return TextEqualsHelper(text, isPropertyName: false);
 01509        }
 1510
 1511        internal bool TextEqualsHelper(ReadOnlySpan<byte> utf8Text, bool isPropertyName, bool shouldUnescape)
 31512        {
 31513            CheckValidInstance();
 1514
 31515            return _parent.TextEquals(_idx, utf8Text, isPropertyName, shouldUnescape);
 31516        }
 1517
 1518        internal bool TextEqualsHelper(ReadOnlySpan<char> text, bool isPropertyName)
 01519        {
 01520            CheckValidInstance();
 1521
 01522            return _parent.TextEquals(_idx, text, isPropertyName);
 01523        }
 1524
 1525        internal bool ValueIsEscapedHelper(bool isPropertyName)
 01526        {
 01527            CheckValidInstance();
 1528
 01529            return _parent.ValueIsEscaped(_idx, isPropertyName);
 01530        }
 1531
 1532        /// <summary>
 1533        ///   Write the element into the provided writer as a JSON value.
 1534        /// </summary>
 1535        /// <param name="writer">The writer.</param>
 1536        /// <exception cref="ArgumentNullException">
 1537        ///   The <paramref name="writer"/> parameter is <see langword="null"/>.
 1538        /// </exception>
 1539        /// <exception cref="InvalidOperationException">
 1540        ///   This value's <see cref="ValueKind"/> is <see cref="JsonValueKind.Undefined"/>.
 1541        /// </exception>
 1542        /// <exception cref="ObjectDisposedException">
 1543        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1544        /// </exception>
 1545        public void WriteTo(Utf8JsonWriter writer)
 01546        {
 01547            ArgumentNullException.ThrowIfNull(writer);
 1548
 01549            CheckValidInstance();
 1550
 01551            _parent.WriteElementTo(_idx, writer);
 01552        }
 1553
 1554        internal void WritePropertyNameTo(Utf8JsonWriter writer)
 01555        {
 01556            CheckValidInstance();
 1557
 01558            _parent.WritePropertyName(_idx, writer);
 01559        }
 1560
 1561        /// <summary>
 1562        ///   Get an enumerator to enumerate the values in the JSON array represented by this JsonElement.
 1563        /// </summary>
 1564        /// <returns>
 1565        ///   An enumerator to enumerate the values in the JSON array represented by this JsonElement.
 1566        /// </returns>
 1567        /// <exception cref="InvalidOperationException">
 1568        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Array"/>.
 1569        /// </exception>
 1570        /// <exception cref="ObjectDisposedException">
 1571        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1572        /// </exception>
 1573        public ArrayEnumerator EnumerateArray()
 01574        {
 01575            CheckValidInstance();
 1576
 01577            JsonTokenType tokenType = TokenType;
 1578
 01579            if (tokenType != JsonTokenType.StartArray)
 01580            {
 01581                ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartArray, tokenType);
 1582            }
 1583
 01584            return new ArrayEnumerator(this);
 01585        }
 1586
 1587        /// <summary>
 1588        ///   Get an enumerator to enumerate the properties in the JSON object represented by this JsonElement.
 1589        /// </summary>
 1590        /// <returns>
 1591        ///   An enumerator to enumerate the properties in the JSON object represented by this JsonElement.
 1592        /// </returns>
 1593        /// <exception cref="InvalidOperationException">
 1594        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 1595        /// </exception>
 1596        /// <exception cref="ObjectDisposedException">
 1597        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1598        /// </exception>
 1599        public ObjectEnumerator EnumerateObject()
 01600        {
 01601            CheckValidInstance();
 1602
 01603            JsonTokenType tokenType = TokenType;
 1604
 01605            if (tokenType != JsonTokenType.StartObject)
 01606            {
 01607                ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType);
 1608            }
 1609
 01610            return new ObjectEnumerator(this);
 01611        }
 1612
 1613        /// <summary>
 1614        ///   Gets a string representation for the current value appropriate to the value type.
 1615        /// </summary>
 1616        /// <remarks>
 1617        ///   <para>
 1618        ///     For JsonElement built from <see cref="JsonDocument"/>:
 1619        ///   </para>
 1620        ///
 1621        ///   <para>
 1622        ///     For <see cref="JsonValueKind.Null"/>, <see cref="string.Empty"/> is returned.
 1623        ///   </para>
 1624        ///
 1625        ///   <para>
 1626        ///     For <see cref="JsonValueKind.True"/>, <see cref="bool.TrueString"/> is returned.
 1627        ///   </para>
 1628        ///
 1629        ///   <para>
 1630        ///     For <see cref="JsonValueKind.False"/>, <see cref="bool.FalseString"/> is returned.
 1631        ///   </para>
 1632        ///
 1633        ///   <para>
 1634        ///     For <see cref="JsonValueKind.String"/>, the value of <see cref="GetString"/>() is returned.
 1635        ///   </para>
 1636        ///
 1637        ///   <para>
 1638        ///     For other types, the value of <see cref="GetRawText"/>() is returned.
 1639        ///   </para>
 1640        /// </remarks>
 1641        /// <returns>
 1642        ///   A string representation for the current value appropriate to the value type.
 1643        /// </returns>
 1644        /// <exception cref="ObjectDisposedException">
 1645        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1646        /// </exception>
 1647        public override string ToString()
 01648        {
 01649            switch (TokenType)
 1650            {
 1651                case JsonTokenType.None:
 1652                case JsonTokenType.Null:
 01653                    return string.Empty;
 1654                case JsonTokenType.True:
 01655                    return bool.TrueString;
 1656                case JsonTokenType.False:
 01657                    return bool.FalseString;
 1658                case JsonTokenType.Number:
 1659                case JsonTokenType.StartArray:
 1660                case JsonTokenType.StartObject:
 01661                    {
 1662                        // null parent should have hit the None case
 01663                        Debug.Assert(_parent != null);
 01664                        return _parent.GetRawValueAsString(_idx);
 1665                    }
 1666                case JsonTokenType.String:
 01667                    return GetString()!;
 1668                case JsonTokenType.Comment:
 1669                case JsonTokenType.EndArray:
 1670                case JsonTokenType.EndObject:
 1671                default:
 01672                    Debug.Fail($"No handler for {nameof(JsonTokenType)}.{TokenType}");
 1673                    return string.Empty;
 1674            }
 01675        }
 1676
 1677        /// <summary>
 1678        ///   Get a JsonElement which can be safely stored beyond the lifetime of the
 1679        ///   original <see cref="JsonDocument"/>.
 1680        /// </summary>
 1681        /// <returns>
 1682        ///   A JsonElement which can be safely stored beyond the lifetime of the
 1683        ///   original <see cref="JsonDocument"/>.
 1684        /// </returns>
 1685        /// <remarks>
 1686        ///   <para>
 1687        ///     If this JsonElement is itself the output of a previous call to Clone, or
 1688        ///     a value contained within another JsonElement which was the output of a previous
 1689        ///     call to Clone, this method results in no additional memory allocation.
 1690        ///   </para>
 1691        /// </remarks>
 1692        public JsonElement Clone()
 01693        {
 01694            CheckValidInstance();
 1695
 01696            if (!_parent.IsDisposable)
 01697            {
 01698                return this;
 1699            }
 1700
 01701            return _parent.CloneElement(_idx);
 01702        }
 1703
 1704        private void CheckValidInstance()
 1551705        {
 1551706            if (_parent == null)
 01707            {
 01708                throw new InvalidOperationException();
 1709            }
 1551710        }
 1711
 01712        internal readonly int MetadataDbIndex => _idx;
 1713
 1714        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 01715        private string DebuggerDisplay => $"ValueKind = {ValueKind} : \"{ToString()}\"";
 1716    }
 1717}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Document\JsonElement.ObjectEnumerator.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.Collections;
 5using System.Collections.Generic;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public partial struct JsonElement
 11    {
 12        /// <summary>
 13        ///   An enumerable and enumerator for the properties of a JSON object.
 14        /// </summary>
 15        [DebuggerDisplay("{Current,nq}")]
 16        public struct ObjectEnumerator : IEnumerable<JsonProperty>, IEnumerator<JsonProperty>
 17        {
 18            private readonly JsonElement _target;
 19            private int _curIdx;
 20            private readonly int _endIdxOrVersion;
 21
 22            internal ObjectEnumerator(JsonElement target, int currentIndex = -1)
 023            {
 024                _target = target;
 025                _curIdx = currentIndex;
 26
 027                Debug.Assert(target.TokenType == JsonTokenType.StartObject);
 028                _endIdxOrVersion = target._parent.GetEndIndex(_target._idx, includeEndElement: false);
 029            }
 30
 31            /// <inheritdoc />
 32            public JsonProperty Current
 33            {
 34                get
 035                {
 036                    if (_curIdx < 0)
 037                    {
 038                        return default;
 39                    }
 40
 041                    return new JsonProperty(new JsonElement(_target._parent, _curIdx));
 042                }
 43            }
 44
 45            /// <summary>
 46            ///   Returns an enumerator that iterates the properties of an object.
 47            /// </summary>
 48            /// <returns>
 49            ///   An <see cref="ObjectEnumerator"/> value that can be used to iterate
 50            ///   through the object.
 51            /// </returns>
 52            /// <remarks>
 53            ///   The enumerator will enumerate the properties in the order they are
 54            ///   declared, and when an object has multiple definitions of a single
 55            ///   property they will all individually be returned (each in the order
 56            ///   they appear in the content).
 57            /// </remarks>
 58            public ObjectEnumerator GetEnumerator()
 059            {
 060                ObjectEnumerator ator = this;
 061                ator._curIdx = -1;
 062                return ator;
 063            }
 64
 65            /// <inheritdoc />
 066            IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 67
 68            /// <inheritdoc />
 069            IEnumerator<JsonProperty> IEnumerable<JsonProperty>.GetEnumerator() => GetEnumerator();
 70
 71            /// <inheritdoc />
 72            public void Dispose()
 073            {
 074                _curIdx = _endIdxOrVersion;
 075            }
 76
 77            /// <inheritdoc />
 78            public void Reset()
 079            {
 080                _curIdx = -1;
 081            }
 82
 83            /// <inheritdoc />
 084            object IEnumerator.Current => Current;
 85
 86            /// <inheritdoc />
 87            public bool MoveNext()
 088            {
 089                if (_curIdx >= _endIdxOrVersion)
 090                {
 091                    return false;
 92                }
 93
 094                if (_curIdx < 0)
 095                {
 096                    _curIdx = _target._idx + JsonDocument.DbRow.Size;
 097                }
 98                else
 099                {
 0100                    _curIdx = _target._parent.GetEndIndex(_curIdx, includeEndElement: true);
 0101                }
 102
 103                // _curIdx is now pointing at a property name, move one more to get the value
 0104                _curIdx += JsonDocument.DbRow.Size;
 105
 0106                return _curIdx < _endIdxOrVersion;
 0107            }
 108        }
 109    }
 110}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Document\JsonElement.Parse.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.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.IO;
 7
 8namespace System.Text.Json
 9{
 10    public readonly partial struct JsonElement
 11    {
 12        /// <summary>
 13        ///   Parses one JSON value (including objects or arrays) from the provided reader.
 14        /// </summary>
 15        /// <param name="reader">The reader to read.</param>
 16        /// <returns>
 17        ///   A JsonElement representing the value (and nested values) read from the reader.
 18        /// </returns>
 19        /// <remarks>
 20        ///   <para>
 21        ///     If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/>
 22        ///     is <see cref="JsonTokenType.PropertyName"/> or <see cref="JsonTokenType.None"/>, the
 23        ///     reader will be advanced by one call to <see cref="Utf8JsonReader.Read"/> to determine
 24        ///     the start of the value.
 25        ///   </para>
 26        ///
 27        ///   <para>
 28        ///     Upon completion of this method, <paramref name="reader"/> will be positioned at the
 29        ///     final token in the JSON value. If an exception is thrown, the reader is reset to
 30        ///     the state it was in when the method was called.
 31        ///   </para>
 32        ///
 33        ///   <para>
 34        ///     This method makes a copy of the data the reader acted on, so there is no caller
 35        ///     requirement to maintain data integrity beyond the return of this method.
 36        ///   </para>
 37        /// </remarks>
 38        /// <exception cref="ArgumentException">
 39        ///   <paramref name="reader"/> is using unsupported options.
 40        /// </exception>
 41        /// <exception cref="ArgumentException">
 42        ///   The current <paramref name="reader"/> token does not start or represent a value.
 43        /// </exception>
 44        /// <exception cref="JsonException">
 45        ///   A value could not be read from the reader.
 46        /// </exception>
 47        public static JsonElement ParseValue(ref Utf8JsonReader reader)
 45648        {
 45649            bool ret = JsonDocument.TryParseValue(ref reader, out JsonDocument? document, shouldThrow: true, useArrayPoo
 50
 4451            Debug.Assert(ret, "TryParseValue returned false with shouldThrow: true.");
 4452            Debug.Assert(document != null, "null document returned with shouldThrow: true.");
 4453            return document.RootElement;
 4454        }
 55
 56        internal static JsonElement ParseValue(ref Utf8JsonReader reader, bool allowDuplicateProperties)
 149657        {
 149658            bool ret = JsonDocument.TryParseValue(
 149659                ref reader,
 149660                out JsonDocument? document,
 149661                shouldThrow: true,
 149662                useArrayPools: false,
 149663                allowDuplicateProperties: allowDuplicateProperties);
 64
 87865            Debug.Assert(ret, "TryParseValue returned false with shouldThrow: true.");
 87866            Debug.Assert(document != null, "null document returned with shouldThrow: true.");
 87867            return document.RootElement;
 87868        }
 69
 70        internal static JsonElement ParseValue(Stream utf8Json, JsonDocumentOptions options)
 071        {
 072            JsonDocument document = JsonDocument.ParseValue(utf8Json, options);
 073            return document.RootElement;
 074        }
 75
 76        /// <summary>
 77        /// Parses UTF8-encoded text representing a single JSON value into a <see cref="JsonElement"/>.
 78        /// </summary>
 79        /// <param name="utf8Json">The JSON text to parse.</param>
 80        /// <param name="options">Options to control the reader behavior during parsing.</param>
 81        /// <returns>A <see cref="JsonElement"/> representation of the JSON value.</returns>
 82        /// <exception cref="JsonException"><paramref name="utf8Json"/> does not represent a valid single JSON value.</e
 83        /// <exception cref="ArgumentException"><paramref name="options"/> contains unsupported options.</exception>
 84        public static JsonElement Parse([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<byte> utf8Json, JsonDocu
 085        {
 086            JsonDocument document = JsonDocument.ParseValue(utf8Json, options);
 087            return document.RootElement;
 088        }
 89
 90        /// <summary>
 91        /// Parses text representing a single JSON value into a <see cref="JsonElement"/>.
 92        /// </summary>
 93        /// <param name="json">The JSON text to parse.</param>
 94        /// <param name="options">Options to control the reader behavior during parsing.</param>
 95        /// <returns>A <see cref="JsonElement"/> representation of the JSON value.</returns>
 96        /// <exception cref="JsonException"><paramref name="json"/> does not represent a valid single JSON value.</excep
 97        /// <exception cref="ArgumentException"><paramref name="options"/> contains unsupported options.</exception>
 98        public static JsonElement Parse([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, JsonDocument
 099        {
 0100            JsonDocument document = JsonDocument.ParseValue(json, options);
 0101            return document.RootElement;
 0102        }
 103
 104        /// <summary>
 105        /// Parses text representing a single JSON value into a <see cref="JsonElement"/>.
 106        /// </summary>
 107        /// <param name="json">The JSON text to parse.</param>
 108        /// <param name="options">Options to control the reader behavior during parsing.</param>
 109        /// <returns>A <see cref="JsonElement"/> representation of the JSON value.</returns>
 110        /// <exception cref="ArgumentNullException"><paramref name="json"/> is <see langword="null"/>.</exception>
 111        /// <exception cref="JsonException"><paramref name="json"/> does not represent a valid single JSON value.</excep
 112        /// <exception cref="ArgumentException"><paramref name="options"/> contains unsupported options.</exception>
 113        public static JsonElement Parse([StringSyntax(StringSyntaxAttribute.Json)] string json, JsonDocumentOptions opti
 0114        {
 0115            ArgumentNullException.ThrowIfNull(json);
 116
 0117            JsonDocument document = JsonDocument.ParseValue(json, options);
 0118            return document.RootElement;
 0119        }
 120
 121        /// <summary>
 122        ///   Attempts to parse one JSON value (including objects or arrays) from the provided reader.
 123        /// </summary>
 124        /// <param name="reader">The reader to read.</param>
 125        /// <param name="element">Receives the parsed element.</param>
 126        /// <returns>
 127        ///   <see langword="true"/> if a value was read and parsed into a JsonElement;
 128        ///   <see langword="false"/> if the reader ran out of data while parsing.
 129        ///   All other situations result in an exception being thrown.
 130        /// </returns>
 131        /// <remarks>
 132        ///   <para>
 133        ///     If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/>
 134        ///     is <see cref="JsonTokenType.PropertyName"/> or <see cref="JsonTokenType.None"/>, the
 135        ///     reader will be advanced by one call to <see cref="Utf8JsonReader.Read"/> to determine
 136        ///     the start of the value.
 137        ///   </para>
 138        ///
 139        ///   <para>
 140        ///     Upon completion of this method, <paramref name="reader"/> will be positioned at the
 141        ///     final token in the JSON value.  If an exception is thrown, or <see langword="false"/>
 142        ///     is returned, the reader is reset to the state it was in when the method was called.
 143        ///   </para>
 144        ///
 145        ///   <para>
 146        ///     This method makes a copy of the data the reader acted on, so there is no caller
 147        ///     requirement to maintain data integrity beyond the return of this method.
 148        ///   </para>
 149        /// </remarks>
 150        /// <exception cref="ArgumentException">
 151        ///   <paramref name="reader"/> is using unsupported options.
 152        /// </exception>
 153        /// <exception cref="ArgumentException">
 154        ///   The current <paramref name="reader"/> token does not start or represent a value.
 155        /// </exception>
 156        /// <exception cref="JsonException">
 157        ///   A value could not be read from the reader.
 158        /// </exception>
 159        public static bool TryParseValue(ref Utf8JsonReader reader, [NotNullWhen(true)] out JsonElement? element)
 0160        {
 0161            bool ret = JsonDocument.TryParseValue(ref reader, out JsonDocument? document, shouldThrow: false, useArrayPo
 0162            element = document?.RootElement;
 0163            return ret;
 0164        }
 165    }
 166}

Methods/Properties

.ctor(System.Text.Json.JsonElement,System.Int32)
Current()
GetEnumerator()
System.Collections.IEnumerable.GetEnumerator()
System.Collections.Generic.IEnumerable<System.Text.Json.JsonElement>.GetEnumerator()
Dispose()
Reset()
em.Collections.IEnumerator.get_Current()
MoveNext()
.ctor(System.Text.Json.JsonDocument,System.Int32)
TokenType()
ValueKind()
Item(System.Int32)
GetArrayLength()
GetPropertyCount()
GetProperty(System.String)
GetProperty(System.ReadOnlySpan`1<System.Char>)
GetProperty(System.ReadOnlySpan`1<System.Byte>)
TryGetProperty(System.String,System.Text.Json.JsonElement&)
TryGetProperty(System.ReadOnlySpan`1<System.Char>,System.Text.Json.JsonElement&)
TryGetProperty(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.JsonElement&)
GetBoolean()
ThrowJsonElementWrongTypeException(System.Text.Json.JsonTokenType)
GetString()
TryGetBytesFromBase64(System.Byte[]&)
GetBytesFromBase64()
TryGetSByte(System.SByte&)
GetSByte()
TryGetByte(System.Byte&)
GetByte()
TryGetInt16(System.Int16&)
GetInt16()
TryGetUInt16(System.UInt16&)
GetUInt16()
TryGetInt32(System.Int32&)
GetInt32()
TryGetUInt32(System.UInt32&)
GetUInt32()
TryGetInt64(System.Int64&)
GetInt64()
TryGetUInt64(System.UInt64&)
GetUInt64()
TryGetDouble(System.Double&)
GetDouble()
TryGetSingle(System.Single&)
GetSingle()
TryGetDecimal(System.Decimal&)
GetDecimal()
TryGetDateTime(System.DateTime&)
GetDateTime()
TryGetDateTimeOffset(System.DateTimeOffset&)
GetDateTimeOffset()
TryGetGuid(System.Guid&)
GetGuid()
GetPropertyName()
GetPropertyNameRaw()
GetRawText()
GetRawValue()
GetPropertyRawText()
ValueIsEscaped()
ValueSpan()
DeepEquals(System.Text.Json.JsonElement,System.Text.Json.JsonElement)
UnorderedObjectDeepEquals(System.Text.Json.JsonElement/ObjectEnumerator,System.Text.Json.JsonElement/ObjectEnumerator,System.Int32)
NameEquals(System.Text.Json.JsonProperty,System.Text.Json.JsonProperty)
ValueEquals(System.String)
ValueEquals(System.ReadOnlySpan`1<System.Byte>)
ValueEquals(System.ReadOnlySpan`1<System.Char>)
TextEqualsHelper(System.ReadOnlySpan`1<System.Byte>,System.Boolean,System.Boolean)
TextEqualsHelper(System.ReadOnlySpan`1<System.Char>,System.Boolean)
ValueIsEscapedHelper(System.Boolean)
WriteTo(System.Text.Json.Utf8JsonWriter)
WritePropertyNameTo(System.Text.Json.Utf8JsonWriter)
EnumerateArray()
EnumerateObject()
ToString()
Clone()
CheckValidInstance()
MetadataDbIndex()
DebuggerDisplay()
.ctor(System.Text.Json.JsonElement,System.Int32)
Current()
GetEnumerator()
System.Collections.IEnumerable.GetEnumerator()
System.Collections.Generic.IEnumerable<System.Text.Json.JsonProperty>.GetEnumerator()
Dispose()
Reset()
em.Collections.IEnumerator.get_Current()
MoveNext()
ParseValue(System.Text.Json.Utf8JsonReader&)
ParseValue(System.Text.Json.Utf8JsonReader&,System.Boolean)
ParseValue(System.IO.Stream,System.Text.Json.JsonDocumentOptions)
Parse(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.JsonDocumentOptions)
Parse(System.ReadOnlySpan`1<System.Char>,System.Text.Json.JsonDocumentOptions)
Parse(System.String,System.Text.Json.JsonDocumentOptions)
TryParseValue(System.Text.Json.Utf8JsonReader&,System.Nullable`1<System.Text.Json.JsonElement>&)