< Summary

Line coverage
3%
Covered lines: 18
Uncovered lines: 510
Coverable lines: 528
Total lines: 2100
Line coverage: 3.4%
Branch coverage
0%
Covered branches: 0
Total branches: 126
Branch coverage: 0%
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%110%
File 2: GetPropertyRawText()100%110%
File 2: DeepEquals(...)0%29290%
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(...)0%220%
File 2: ValueEquals(...)0%220%
File 2: TextEqualsHelper(...)100%110%
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()0%220%
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%1133.33%
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)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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}
 103

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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;
 10using System.Text;
 11
 12namespace System.Text.Json
 13{
 14    /// <summary>
 15    ///   Represents a specific JSON value within a <see cref="JsonDocument"/>.
 16    /// </summary>
 17    [DebuggerDisplay("{DebuggerDisplay,nq}")]
 18    public readonly partial struct JsonElement
 19    {
 20        private readonly JsonDocument _parent;
 21        private readonly int _idx;
 22
 23        internal JsonElement(JsonDocument parent, int idx)
 824        {
 25            // parent is usually not null, but the Current property
 26            // on the enumerators (when initialized as `default`) can
 27            // get here with a null.
 828            Debug.Assert(idx >= 0);
 29
 830            _parent = parent;
 831            _idx = idx;
 832        }
 33
 34        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 35        private JsonTokenType TokenType
 36        {
 37            get
 038            {
 039                return _parent?.GetJsonTokenType(_idx) ?? JsonTokenType.None;
 040            }
 41        }
 42        /// <summary>
 43        ///   The <see cref="JsonValueKind"/> that the value is.
 44        /// </summary>
 45        /// <exception cref="ObjectDisposedException">
 46        ///   The parent <see cref="JsonDocument"/> has been disposed.
 47        /// </exception>
 048        public JsonValueKind ValueKind => TokenType.ToValueKind();
 49
 50        /// <summary>
 51        ///   Get the value at a specified index when the current value is a
 52        ///   <see cref="JsonValueKind.Array"/>.
 53        /// </summary>
 54        /// <exception cref="InvalidOperationException">
 55        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Array"/>.
 56        /// </exception>
 57        /// <exception cref="IndexOutOfRangeException">
 58        ///   <paramref name="index"/> is not in the range [0, <see cref="GetArrayLength"/>()).
 59        /// </exception>
 60        /// <exception cref="ObjectDisposedException">
 61        ///   The parent <see cref="JsonDocument"/> has been disposed.
 62        /// </exception>
 63        public JsonElement this[int index]
 64        {
 65            get
 066            {
 067                CheckValidInstance();
 68
 069                return _parent.GetArrayIndexElement(_idx, index);
 070            }
 71        }
 72
 73        /// <summary>
 74        ///   Get the number of values contained within the current array value.
 75        /// </summary>
 76        /// <returns>The number of values contained within the current array value.</returns>
 77        /// <exception cref="InvalidOperationException">
 78        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Array"/>.
 79        /// </exception>
 80        /// <exception cref="ObjectDisposedException">
 81        ///   The parent <see cref="JsonDocument"/> has been disposed.
 82        /// </exception>
 83        public int GetArrayLength()
 084        {
 085            CheckValidInstance();
 86
 087            return _parent.GetArrayLength(_idx);
 088        }
 89
 90        /// <summary>
 91        ///   Get the number of properties contained within the current object value.
 92        /// </summary>
 93        /// <returns>The number of properties contained within the current object value.</returns>
 94        /// <exception cref="InvalidOperationException">
 95        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 96        /// </exception>
 97        /// <exception cref="ObjectDisposedException">
 98        ///   The parent <see cref="JsonDocument"/> has been disposed.
 99        /// </exception>
 100        public int GetPropertyCount()
 0101        {
 0102            CheckValidInstance();
 103
 0104            return _parent.GetPropertyCount(_idx);
 0105        }
 106
 107        /// <summary>
 108        ///   Gets a <see cref="JsonElement"/> representing the value of a required property identified
 109        ///   by <paramref name="propertyName"/>.
 110        /// </summary>
 111        /// <remarks>
 112        ///   Property name matching is performed as an ordinal, case-sensitive, comparison.
 113        ///
 114        ///   If a property is defined multiple times for the same object, the last such definition is
 115        ///   what is matched.
 116        /// </remarks>
 117        /// <param name="propertyName">Name of the property whose value to return.</param>
 118        /// <returns>
 119        ///   A <see cref="JsonElement"/> representing the value of the requested property.
 120        /// </returns>
 121        /// <seealso cref="EnumerateObject"/>
 122        /// <exception cref="InvalidOperationException">
 123        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 124        /// </exception>
 125        /// <exception cref="KeyNotFoundException">
 126        ///   No property was found with the requested name.
 127        /// </exception>
 128        /// <exception cref="ArgumentNullException">
 129        ///   <paramref name="propertyName"/> is <see langword="null"/>.
 130        /// </exception>
 131        /// <exception cref="ObjectDisposedException">
 132        ///   The parent <see cref="JsonDocument"/> has been disposed.
 133        /// </exception>
 134        public JsonElement GetProperty(string propertyName)
 0135        {
 0136            ArgumentNullException.ThrowIfNull(propertyName);
 137
 0138            if (TryGetProperty(propertyName, out JsonElement property))
 0139            {
 0140                return property;
 141            }
 142
 0143            throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, propertyName));
 0144        }
 145
 146        /// <summary>
 147        ///   Gets a <see cref="JsonElement"/> representing the value of a required property identified
 148        ///   by <paramref name="propertyName"/>.
 149        /// </summary>
 150        /// <remarks>
 151        ///   <para>
 152        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 153        ///   </para>
 154        ///
 155        ///   <para>
 156        ///     If a property is defined multiple times for the same object, the last such definition is
 157        ///     what is matched.
 158        ///   </para>
 159        /// </remarks>
 160        /// <param name="propertyName">Name of the property whose value to return.</param>
 161        /// <returns>
 162        ///   A <see cref="JsonElement"/> representing the value of the requested property.
 163        /// </returns>
 164        /// <seealso cref="EnumerateObject"/>
 165        /// <exception cref="InvalidOperationException">
 166        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 167        /// </exception>
 168        /// <exception cref="KeyNotFoundException">
 169        ///   No property was found with the requested name.
 170        /// </exception>
 171        /// <exception cref="ObjectDisposedException">
 172        ///   The parent <see cref="JsonDocument"/> has been disposed.
 173        /// </exception>
 174        public JsonElement GetProperty(ReadOnlySpan<char> propertyName)
 0175        {
 0176            if (TryGetProperty(propertyName, out JsonElement property))
 0177            {
 0178                return property;
 179            }
 180
 0181            throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, propertyName.ToString()));
 0182        }
 183
 184        /// <summary>
 185        ///   Gets a <see cref="JsonElement"/> representing the value of a required property identified
 186        ///   by <paramref name="utf8PropertyName"/>.
 187        /// </summary>
 188        /// <remarks>
 189        ///   <para>
 190        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 191        ///   </para>
 192        ///
 193        ///   <para>
 194        ///     If a property is defined multiple times for the same object, the last such definition is
 195        ///     what is matched.
 196        ///   </para>
 197        /// </remarks>
 198        /// <param name="utf8PropertyName">
 199        ///   The UTF-8 (with no Byte-Order-Mark (BOM)) representation of the name of the property to return.
 200        /// </param>
 201        /// <returns>
 202        ///   A <see cref="JsonElement"/> representing the value of the requested property.
 203        /// </returns>
 204        /// <exception cref="InvalidOperationException">
 205        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 206        /// </exception>
 207        /// <exception cref="KeyNotFoundException">
 208        ///   No property was found with the requested name.
 209        /// </exception>
 210        /// <exception cref="ObjectDisposedException">
 211        ///   The parent <see cref="JsonDocument"/> has been disposed.
 212        /// </exception>
 213        /// <seealso cref="EnumerateObject"/>
 214        public JsonElement GetProperty(ReadOnlySpan<byte> utf8PropertyName)
 0215        {
 0216            if (TryGetProperty(utf8PropertyName, out JsonElement property))
 0217            {
 0218                return property;
 219            }
 220
 0221            throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, Encoding.UTF8.GetString(utf8PropertyName
 0222        }
 223
 224        /// <summary>
 225        ///   Looks for a property named <paramref name="propertyName"/> in the current object, returning
 226        ///   whether or not such a property existed. When the property exists <paramref name="value"/>
 227        ///   is assigned to the value of that property.
 228        /// </summary>
 229        /// <remarks>
 230        ///   <para>
 231        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 232        ///   </para>
 233        ///
 234        ///   <para>
 235        ///     If a property is defined multiple times for the same object, the last such definition is
 236        ///     what is matched.
 237        ///   </para>
 238        /// </remarks>
 239        /// <param name="propertyName">Name of the property to find.</param>
 240        /// <param name="value">Receives the value of the located property.</param>
 241        /// <returns>
 242        ///   <see langword="true"/> if the property was found, <see langword="false"/> otherwise.
 243        /// </returns>
 244        /// <exception cref="InvalidOperationException">
 245        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 246        /// </exception>
 247        /// <exception cref="ArgumentNullException">
 248        ///   <paramref name="propertyName"/> is <see langword="null"/>.
 249        /// </exception>
 250        /// <exception cref="ObjectDisposedException">
 251        ///   The parent <see cref="JsonDocument"/> has been disposed.
 252        /// </exception>
 253        /// <seealso cref="EnumerateObject"/>
 254        public bool TryGetProperty(string propertyName, out JsonElement value)
 0255        {
 0256            ArgumentNullException.ThrowIfNull(propertyName);
 257
 0258            return TryGetProperty(propertyName.AsSpan(), out value);
 0259        }
 260
 261        /// <summary>
 262        ///   Looks for a property named <paramref name="propertyName"/> in the current object, returning
 263        ///   whether or not such a property existed. When the property exists <paramref name="value"/>
 264        ///   is assigned to the value of that property.
 265        /// </summary>
 266        /// <remarks>
 267        ///   <para>
 268        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 269        ///   </para>
 270        ///
 271        ///   <para>
 272        ///     If a property is defined multiple times for the same object, the last such definition is
 273        ///     what is matched.
 274        ///   </para>
 275        /// </remarks>
 276        /// <param name="propertyName">Name of the property to find.</param>
 277        /// <param name="value">Receives the value of the located property.</param>
 278        /// <returns>
 279        ///   <see langword="true"/> if the property was found, <see langword="false"/> otherwise.
 280        /// </returns>
 281        /// <seealso cref="EnumerateObject"/>
 282        /// <exception cref="InvalidOperationException">
 283        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 284        /// </exception>
 285        /// <exception cref="ObjectDisposedException">
 286        ///   The parent <see cref="JsonDocument"/> has been disposed.
 287        /// </exception>
 288        public bool TryGetProperty(ReadOnlySpan<char> propertyName, out JsonElement value)
 0289        {
 0290            CheckValidInstance();
 291
 0292            return _parent.TryGetNamedPropertyValue(_idx, propertyName, out value);
 0293        }
 294
 295        /// <summary>
 296        ///   Looks for a property named <paramref name="utf8PropertyName"/> in the current object, returning
 297        ///   whether or not such a property existed. When the property exists <paramref name="value"/>
 298        ///   is assigned to the value of that property.
 299        /// </summary>
 300        /// <remarks>
 301        ///   <para>
 302        ///     Property name matching is performed as an ordinal, case-sensitive, comparison.
 303        ///   </para>
 304        ///
 305        ///   <para>
 306        ///     If a property is defined multiple times for the same object, the last such definition is
 307        ///     what is matched.
 308        ///   </para>
 309        /// </remarks>
 310        /// <param name="utf8PropertyName">
 311        ///   The UTF-8 (with no Byte-Order-Mark (BOM)) representation of the name of the property to return.
 312        /// </param>
 313        /// <param name="value">Receives the value of the located property.</param>
 314        /// <returns>
 315        ///   <see langword="true"/> if the property was found, <see langword="false"/> otherwise.
 316        /// </returns>
 317        /// <seealso cref="EnumerateObject"/>
 318        /// <exception cref="InvalidOperationException">
 319        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 320        /// </exception>
 321        /// <exception cref="ObjectDisposedException">
 322        ///   The parent <see cref="JsonDocument"/> has been disposed.
 323        /// </exception>
 324        public bool TryGetProperty(ReadOnlySpan<byte> utf8PropertyName, out JsonElement value)
 0325        {
 0326            CheckValidInstance();
 327
 0328            return _parent.TryGetNamedPropertyValue(_idx, utf8PropertyName, out value);
 0329        }
 330
 331        /// <summary>
 332        ///   Gets the value of the element as a <see cref="bool"/>.
 333        /// </summary>
 334        /// <remarks>
 335        ///   This method does not parse the contents of a JSON string value.
 336        /// </remarks>
 337        /// <returns>The value of the element as a <see cref="bool"/>.</returns>
 338        /// <exception cref="InvalidOperationException">
 339        ///   This value's <see cref="ValueKind"/> is neither <see cref="JsonValueKind.True"/> or
 340        ///   <see cref="JsonValueKind.False"/>.
 341        /// </exception>
 342        /// <exception cref="ObjectDisposedException">
 343        ///   The parent <see cref="JsonDocument"/> has been disposed.
 344        /// </exception>
 345        public bool GetBoolean()
 0346        {
 347            // CheckValidInstance is redundant.  Asking for the type will
 348            // return None, which then throws the same exception in the return statement.
 349
 0350            JsonTokenType type = TokenType;
 351
 0352            return
 0353                type == JsonTokenType.True ? true :
 0354                type == JsonTokenType.False ? false :
 0355                ThrowJsonElementWrongTypeException(type);
 356
 357            static bool ThrowJsonElementWrongTypeException(JsonTokenType actualType)
 0358            {
 0359                throw ThrowHelper.GetJsonElementWrongTypeException(nameof(Boolean), actualType.ToValueKind());
 360            }
 0361        }
 362
 363        /// <summary>
 364        ///   Gets the value of the element as a <see cref="string"/>.
 365        /// </summary>
 366        /// <remarks>
 367        ///   This method does not create a string representation of values other than JSON strings.
 368        /// </remarks>
 369        /// <returns>The value of the element as a <see cref="string"/>.</returns>
 370        /// <exception cref="InvalidOperationException">
 371        ///   This value's <see cref="ValueKind"/> is neither <see cref="JsonValueKind.String"/> nor <see cref="JsonValu
 372        /// </exception>
 373        /// <exception cref="ObjectDisposedException">
 374        ///   The parent <see cref="JsonDocument"/> has been disposed.
 375        /// </exception>
 376        /// <seealso cref="ToString"/>
 377        public string? GetString()
 0378        {
 0379            CheckValidInstance();
 380
 0381            return _parent.GetString(_idx, JsonTokenType.String);
 0382        }
 383
 384        /// <summary>
 385        ///   Attempts to represent the current JSON string as bytes assuming it is Base64 encoded.
 386        /// </summary>
 387        /// <param name="value">Receives the value.</param>
 388        /// <remarks>
 389        ///  This method does not create a byte[] representation of values other than base 64 encoded JSON strings.
 390        /// </remarks>
 391        /// <returns>
 392        ///   <see langword="true"/> if the entire token value is encoded as valid Base64 text and can be successfully d
 393        ///   <see langword="false"/> otherwise.
 394        /// </returns>
 395        /// <exception cref="InvalidOperationException">
 396        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 397        /// </exception>
 398        /// <exception cref="ObjectDisposedException">
 399        ///   The parent <see cref="JsonDocument"/> has been disposed.
 400        /// </exception>
 401        public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value)
 0402        {
 0403            CheckValidInstance();
 404
 0405            return _parent.TryGetValue(_idx, out value);
 0406        }
 407
 408        /// <summary>
 409        ///   Gets the value of the element as bytes.
 410        /// </summary>
 411        /// <remarks>
 412        ///   This method does not create a byte[] representation of values other than Base64 encoded JSON strings.
 413        /// </remarks>
 414        /// <returns>The value decode to bytes.</returns>
 415        /// <exception cref="InvalidOperationException">
 416        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 417        /// </exception>
 418        /// <exception cref="FormatException">
 419        ///   The value is not encoded as Base64 text and hence cannot be decoded to bytes.
 420        /// </exception>
 421        /// <exception cref="ObjectDisposedException">
 422        ///   The parent <see cref="JsonDocument"/> has been disposed.
 423        /// </exception>
 424        /// <seealso cref="ToString"/>
 425        public byte[] GetBytesFromBase64()
 0426        {
 0427            if (!TryGetBytesFromBase64(out byte[]? value))
 0428            {
 0429                ThrowHelper.ThrowFormatException();
 430            }
 431
 0432            return value;
 0433        }
 434
 435        /// <summary>
 436        ///   Attempts to represent the current JSON number as an <see cref="sbyte"/>.
 437        /// </summary>
 438        /// <param name="value">Receives the value.</param>
 439        /// <remarks>
 440        ///   This method does not parse the contents of a JSON string value.
 441        /// </remarks>
 442        /// <returns>
 443        ///   <see langword="true"/> if the number can be represented as an <see cref="sbyte"/>,
 444        ///   <see langword="false"/> otherwise.
 445        /// </returns>
 446        /// <exception cref="InvalidOperationException">
 447        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 448        /// </exception>
 449        /// <exception cref="ObjectDisposedException">
 450        ///   The parent <see cref="JsonDocument"/> has been disposed.
 451        /// </exception>
 452        [CLSCompliant(false)]
 453        public bool TryGetSByte(out sbyte value)
 0454        {
 0455            CheckValidInstance();
 456
 0457            return _parent.TryGetValue(_idx, out value);
 0458        }
 459
 460        /// <summary>
 461        ///   Gets the current JSON number as an <see cref="sbyte"/>.
 462        /// </summary>
 463        /// <returns>The current JSON number as an <see cref="sbyte"/>.</returns>
 464        /// <exception cref="InvalidOperationException">
 465        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 466        /// </exception>
 467        /// <exception cref="FormatException">
 468        ///   The value cannot be represented as an <see cref="sbyte"/>.
 469        /// </exception>
 470        /// <exception cref="ObjectDisposedException">
 471        ///   The parent <see cref="JsonDocument"/> has been disposed.
 472        /// </exception>
 473        [CLSCompliant(false)]
 474        public sbyte GetSByte()
 0475        {
 0476            if (!TryGetSByte(out sbyte value))
 0477            {
 0478                ThrowHelper.ThrowFormatException();
 479            }
 480
 0481            return value;
 0482        }
 483
 484        /// <summary>
 485        ///   Attempts to represent the current JSON number as a <see cref="byte"/>.
 486        /// </summary>
 487        /// <param name="value">Receives the value.</param>
 488        /// <remarks>
 489        ///   This method does not parse the contents of a JSON string value.
 490        /// </remarks>
 491        /// <returns>
 492        ///   <see langword="true"/> if the number can be represented as a <see cref="byte"/>,
 493        ///   <see langword="false"/> otherwise.
 494        /// </returns>
 495        /// <exception cref="InvalidOperationException">
 496        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 497        /// </exception>
 498        /// <exception cref="ObjectDisposedException">
 499        ///   The parent <see cref="JsonDocument"/> has been disposed.
 500        /// </exception>
 501        public bool TryGetByte(out byte value)
 0502        {
 0503            CheckValidInstance();
 504
 0505            return _parent.TryGetValue(_idx, out value);
 0506        }
 507
 508        /// <summary>
 509        ///   Gets the current JSON number as a <see cref="byte"/>.
 510        /// </summary>
 511        /// <returns>The current JSON number as a <see cref="byte"/>.</returns>
 512        /// <remarks>
 513        ///   This method does not parse the contents of a JSON string value.
 514        /// </remarks>
 515        /// <exception cref="InvalidOperationException">
 516        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 517        /// </exception>
 518        /// <exception cref="FormatException">
 519        ///   The value cannot be represented as a <see cref="byte"/>.
 520        /// </exception>
 521        /// <exception cref="ObjectDisposedException">
 522        ///   The parent <see cref="JsonDocument"/> has been disposed.
 523        /// </exception>
 524        public byte GetByte()
 0525        {
 0526            if (!TryGetByte(out byte value))
 0527            {
 0528                ThrowHelper.ThrowFormatException();
 529            }
 530
 0531            return value;
 0532        }
 533
 534        /// <summary>
 535        ///   Attempts to represent the current JSON number as an <see cref="short"/>.
 536        /// </summary>
 537        /// <param name="value">Receives the value.</param>
 538        /// <remarks>
 539        ///   This method does not parse the contents of a JSON string value.
 540        /// </remarks>
 541        /// <returns>
 542        ///   <see langword="true"/> if the number can be represented as an <see cref="short"/>,
 543        ///   <see langword="false"/> otherwise.
 544        /// </returns>
 545        /// <exception cref="InvalidOperationException">
 546        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 547        /// </exception>
 548        /// <exception cref="ObjectDisposedException">
 549        ///   The parent <see cref="JsonDocument"/> has been disposed.
 550        /// </exception>
 551        public bool TryGetInt16(out short value)
 0552        {
 0553            CheckValidInstance();
 554
 0555            return _parent.TryGetValue(_idx, out value);
 0556        }
 557
 558        /// <summary>
 559        ///   Gets the current JSON number as an <see cref="short"/>.
 560        /// </summary>
 561        /// <returns>The current JSON number as an <see cref="short"/>.</returns>
 562        /// <exception cref="InvalidOperationException">
 563        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 564        /// </exception>
 565        /// <exception cref="FormatException">
 566        ///   The value cannot be represented as an <see cref="short"/>.
 567        /// </exception>
 568        /// <exception cref="ObjectDisposedException">
 569        ///   The parent <see cref="JsonDocument"/> has been disposed.
 570        /// </exception>
 571        public short GetInt16()
 0572        {
 0573            if (!TryGetInt16(out short value))
 0574            {
 0575                ThrowHelper.ThrowFormatException();
 576            }
 577
 0578            return value;
 0579        }
 580
 581        /// <summary>
 582        ///   Attempts to represent the current JSON number as a <see cref="ushort"/>.
 583        /// </summary>
 584        /// <param name="value">Receives the value.</param>
 585        /// <remarks>
 586        ///   This method does not parse the contents of a JSON string value.
 587        /// </remarks>
 588        /// <returns>
 589        ///   <see langword="true"/> if the number can be represented as a <see cref="ushort"/>,
 590        ///   <see langword="false"/> otherwise.
 591        /// </returns>
 592        /// <exception cref="InvalidOperationException">
 593        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 594        /// </exception>
 595        /// <exception cref="ObjectDisposedException">
 596        ///   The parent <see cref="JsonDocument"/> has been disposed.
 597        /// </exception>
 598        [CLSCompliant(false)]
 599        public bool TryGetUInt16(out ushort value)
 0600        {
 0601            CheckValidInstance();
 602
 0603            return _parent.TryGetValue(_idx, out value);
 0604        }
 605
 606        /// <summary>
 607        ///   Gets the current JSON number as a <see cref="ushort"/>.
 608        /// </summary>
 609        /// <returns>The current JSON number as a <see cref="ushort"/>.</returns>
 610        /// <remarks>
 611        ///   This method does not parse the contents of a JSON string value.
 612        /// </remarks>
 613        /// <exception cref="InvalidOperationException">
 614        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 615        /// </exception>
 616        /// <exception cref="FormatException">
 617        ///   The value cannot be represented as a <see cref="ushort"/>.
 618        /// </exception>
 619        /// <exception cref="ObjectDisposedException">
 620        ///   The parent <see cref="JsonDocument"/> has been disposed.
 621        /// </exception>
 622        [CLSCompliant(false)]
 623        public ushort GetUInt16()
 0624        {
 0625            if (!TryGetUInt16(out ushort value))
 0626            {
 0627                ThrowHelper.ThrowFormatException();
 628            }
 629
 0630            return value;
 0631        }
 632
 633        /// <summary>
 634        ///   Attempts to represent the current JSON number as an <see cref="int"/>.
 635        /// </summary>
 636        /// <param name="value">Receives the value.</param>
 637        /// <remarks>
 638        ///   This method does not parse the contents of a JSON string value.
 639        /// </remarks>
 640        /// <returns>
 641        ///   <see langword="true"/> if the number can be represented as an <see cref="int"/>,
 642        ///   <see langword="false"/> otherwise.
 643        /// </returns>
 644        /// <exception cref="InvalidOperationException">
 645        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 646        /// </exception>
 647        /// <exception cref="ObjectDisposedException">
 648        ///   The parent <see cref="JsonDocument"/> has been disposed.
 649        /// </exception>
 650        public bool TryGetInt32(out int value)
 0651        {
 0652            CheckValidInstance();
 653
 0654            return _parent.TryGetValue(_idx, out value);
 0655        }
 656
 657        /// <summary>
 658        ///   Gets the current JSON number as an <see cref="int"/>.
 659        /// </summary>
 660        /// <returns>The current JSON number as an <see cref="int"/>.</returns>
 661        /// <exception cref="InvalidOperationException">
 662        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 663        /// </exception>
 664        /// <exception cref="FormatException">
 665        ///   The value cannot be represented as an <see cref="int"/>.
 666        /// </exception>
 667        /// <exception cref="ObjectDisposedException">
 668        ///   The parent <see cref="JsonDocument"/> has been disposed.
 669        /// </exception>
 670        public int GetInt32()
 0671        {
 0672            if (!TryGetInt32(out int value))
 0673            {
 0674                ThrowHelper.ThrowFormatException();
 675            }
 676
 0677            return value;
 0678        }
 679
 680        /// <summary>
 681        ///   Attempts to represent the current JSON number as a <see cref="uint"/>.
 682        /// </summary>
 683        /// <param name="value">Receives the value.</param>
 684        /// <remarks>
 685        ///   This method does not parse the contents of a JSON string value.
 686        /// </remarks>
 687        /// <returns>
 688        ///   <see langword="true"/> if the number can be represented as a <see cref="uint"/>,
 689        ///   <see langword="false"/> otherwise.
 690        /// </returns>
 691        /// <exception cref="InvalidOperationException">
 692        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 693        /// </exception>
 694        /// <exception cref="ObjectDisposedException">
 695        ///   The parent <see cref="JsonDocument"/> has been disposed.
 696        /// </exception>
 697        [CLSCompliant(false)]
 698        public bool TryGetUInt32(out uint value)
 0699        {
 0700            CheckValidInstance();
 701
 0702            return _parent.TryGetValue(_idx, out value);
 0703        }
 704
 705        /// <summary>
 706        ///   Gets the current JSON number as a <see cref="uint"/>.
 707        /// </summary>
 708        /// <returns>The current JSON number as a <see cref="uint"/>.</returns>
 709        /// <remarks>
 710        ///   This method does not parse the contents of a JSON string value.
 711        /// </remarks>
 712        /// <exception cref="InvalidOperationException">
 713        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 714        /// </exception>
 715        /// <exception cref="FormatException">
 716        ///   The value cannot be represented as a <see cref="uint"/>.
 717        /// </exception>
 718        /// <exception cref="ObjectDisposedException">
 719        ///   The parent <see cref="JsonDocument"/> has been disposed.
 720        /// </exception>
 721        [CLSCompliant(false)]
 722        public uint GetUInt32()
 0723        {
 0724            if (!TryGetUInt32(out uint value))
 0725            {
 0726                ThrowHelper.ThrowFormatException();
 727            }
 728
 0729            return value;
 0730        }
 731
 732        /// <summary>
 733        ///   Attempts to represent the current JSON number as a <see cref="long"/>.
 734        /// </summary>
 735        /// <param name="value">Receives the value.</param>
 736        /// <remarks>
 737        ///   This method does not parse the contents of a JSON string value.
 738        /// </remarks>
 739        /// <returns>
 740        ///   <see langword="true"/> if the number can be represented as a <see cref="long"/>,
 741        ///   <see langword="false"/> otherwise.
 742        /// </returns>
 743        /// <exception cref="InvalidOperationException">
 744        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 745        /// </exception>
 746        /// <exception cref="ObjectDisposedException">
 747        ///   The parent <see cref="JsonDocument"/> has been disposed.
 748        /// </exception>
 749        public bool TryGetInt64(out long value)
 0750        {
 0751            CheckValidInstance();
 752
 0753            return _parent.TryGetValue(_idx, out value);
 0754        }
 755
 756        /// <summary>
 757        ///   Gets the current JSON number as a <see cref="long"/>.
 758        /// </summary>
 759        /// <returns>The current JSON number as a <see cref="long"/>.</returns>
 760        /// <remarks>
 761        ///   This method does not parse the contents of a JSON string value.
 762        /// </remarks>
 763        /// <exception cref="InvalidOperationException">
 764        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 765        /// </exception>
 766        /// <exception cref="FormatException">
 767        ///   The value cannot be represented as a <see cref="long"/>.
 768        /// </exception>
 769        /// <exception cref="ObjectDisposedException">
 770        ///   The parent <see cref="JsonDocument"/> has been disposed.
 771        /// </exception>
 772        public long GetInt64()
 0773        {
 0774            if (!TryGetInt64(out long value))
 0775            {
 0776                ThrowHelper.ThrowFormatException();
 777            }
 778
 0779            return value;
 0780        }
 781
 782        /// <summary>
 783        ///   Attempts to represent the current JSON number as a <see cref="ulong"/>.
 784        /// </summary>
 785        /// <param name="value">Receives the value.</param>
 786        /// <remarks>
 787        ///   This method does not parse the contents of a JSON string value.
 788        /// </remarks>
 789        /// <returns>
 790        ///   <see langword="true"/> if the number can be represented as a <see cref="ulong"/>,
 791        ///   <see langword="false"/> otherwise.
 792        /// </returns>
 793        /// <exception cref="InvalidOperationException">
 794        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 795        /// </exception>
 796        /// <exception cref="ObjectDisposedException">
 797        ///   The parent <see cref="JsonDocument"/> has been disposed.
 798        /// </exception>
 799        [CLSCompliant(false)]
 800        public bool TryGetUInt64(out ulong value)
 0801        {
 0802            CheckValidInstance();
 803
 0804            return _parent.TryGetValue(_idx, out value);
 0805        }
 806
 807        /// <summary>
 808        ///   Gets the current JSON number as a <see cref="ulong"/>.
 809        /// </summary>
 810        /// <returns>The current JSON number as a <see cref="ulong"/>.</returns>
 811        /// <remarks>
 812        ///   This method does not parse the contents of a JSON string value.
 813        /// </remarks>
 814        /// <exception cref="InvalidOperationException">
 815        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 816        /// </exception>
 817        /// <exception cref="FormatException">
 818        ///   The value cannot be represented as a <see cref="ulong"/>.
 819        /// </exception>
 820        /// <exception cref="ObjectDisposedException">
 821        ///   The parent <see cref="JsonDocument"/> has been disposed.
 822        /// </exception>
 823        [CLSCompliant(false)]
 824        public ulong GetUInt64()
 0825        {
 0826            if (!TryGetUInt64(out ulong value))
 0827            {
 0828                ThrowHelper.ThrowFormatException();
 829            }
 830
 0831            return value;
 0832        }
 833
 834        /// <summary>
 835        ///   Attempts to represent the current JSON number as a <see cref="double"/>.
 836        /// </summary>
 837        /// <param name="value">Receives the value.</param>
 838        /// <remarks>
 839        ///   <para>
 840        ///     This method does not parse the contents of a JSON string value.
 841        ///   </para>
 842        ///
 843        ///   <para>
 844        ///     On .NET Core this method does not return <see langword="false"/> for values larger than
 845        ///     <see cref="double.MaxValue"/> (or smaller than <see cref="double.MinValue"/>),
 846        ///     instead <see langword="true"/> is returned and <see cref="double.PositiveInfinity"/> (or
 847        ///     <see cref="double.NegativeInfinity"/>) is emitted.
 848        ///   </para>
 849        /// </remarks>
 850        /// <returns>
 851        ///   <see langword="true"/> if the number can be represented as a <see cref="double"/>,
 852        ///   <see langword="false"/> otherwise.
 853        /// </returns>
 854        /// <exception cref="InvalidOperationException">
 855        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 856        /// </exception>
 857        /// <exception cref="ObjectDisposedException">
 858        ///   The parent <see cref="JsonDocument"/> has been disposed.
 859        /// </exception>
 860        public bool TryGetDouble(out double value)
 0861        {
 0862            CheckValidInstance();
 863
 0864            return _parent.TryGetValue(_idx, out value);
 0865        }
 866
 867        /// <summary>
 868        ///   Gets the current JSON number as a <see cref="double"/>.
 869        /// </summary>
 870        /// <returns>The current JSON number as a <see cref="double"/>.</returns>
 871        /// <remarks>
 872        ///   <para>
 873        ///     This method does not parse the contents of a JSON string value.
 874        ///   </para>
 875        ///
 876        ///   <para>
 877        ///     On .NET Core this method returns <see cref="double.PositiveInfinity"/> (or
 878        ///     <see cref="double.NegativeInfinity"/>) for values larger than
 879        ///     <see cref="double.MaxValue"/> (or smaller than <see cref="double.MinValue"/>).
 880        ///   </para>
 881        /// </remarks>
 882        /// <exception cref="InvalidOperationException">
 883        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 884        /// </exception>
 885        /// <exception cref="FormatException">
 886        ///   The value cannot be represented as a <see cref="double"/>.
 887        /// </exception>
 888        /// <exception cref="ObjectDisposedException">
 889        ///   The parent <see cref="JsonDocument"/> has been disposed.
 890        /// </exception>
 891        public double GetDouble()
 0892        {
 0893            if (!TryGetDouble(out double value))
 0894            {
 0895                ThrowHelper.ThrowFormatException();
 896            }
 897
 0898            return value;
 0899        }
 900
 901        /// <summary>
 902        ///   Attempts to represent the current JSON number as a <see cref="float"/>.
 903        /// </summary>
 904        /// <param name="value">Receives the value.</param>
 905        /// <remarks>
 906        ///   <para>
 907        ///     This method does not parse the contents of a JSON string value.
 908        ///   </para>
 909        ///
 910        ///   <para>
 911        ///     On .NET Core this method does not return <see langword="false"/> for values larger than
 912        ///     <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>),
 913        ///     instead <see langword="true"/> is returned and <see cref="float.PositiveInfinity"/> (or
 914        ///     <see cref="float.NegativeInfinity"/>) is emitted.
 915        ///   </para>
 916        /// </remarks>
 917        /// <returns>
 918        ///   <see langword="true"/> if the number can be represented as a <see cref="float"/>,
 919        ///   <see langword="false"/> otherwise.
 920        /// </returns>
 921        /// <exception cref="InvalidOperationException">
 922        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 923        /// </exception>
 924        /// <exception cref="ObjectDisposedException">
 925        ///   The parent <see cref="JsonDocument"/> has been disposed.
 926        /// </exception>
 927        public bool TryGetSingle(out float value)
 0928        {
 0929            CheckValidInstance();
 930
 0931            return _parent.TryGetValue(_idx, out value);
 0932        }
 933
 934        /// <summary>
 935        ///   Gets the current JSON number as a <see cref="float"/>.
 936        /// </summary>
 937        /// <returns>The current JSON number as a <see cref="float"/>.</returns>
 938        /// <remarks>
 939        ///   <para>
 940        ///     This method does not parse the contents of a JSON string value.
 941        ///   </para>
 942        ///
 943        ///   <para>
 944        ///     On .NET Core this method returns <see cref="float.PositiveInfinity"/> (or
 945        ///     <see cref="float.NegativeInfinity"/>) for values larger than
 946        ///     <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>).
 947        ///   </para>
 948        /// </remarks>
 949        /// <exception cref="InvalidOperationException">
 950        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 951        /// </exception>
 952        /// <exception cref="FormatException">
 953        ///   The value cannot be represented as a <see cref="float"/>.
 954        /// </exception>
 955        /// <exception cref="ObjectDisposedException">
 956        ///   The parent <see cref="JsonDocument"/> has been disposed.
 957        /// </exception>
 958        public float GetSingle()
 0959        {
 0960            if (!TryGetSingle(out float value))
 0961            {
 0962                ThrowHelper.ThrowFormatException();
 963            }
 964
 0965            return value;
 0966        }
 967
 968        /// <summary>
 969        ///   Attempts to represent the current JSON number as a <see cref="decimal"/>.
 970        /// </summary>
 971        /// <param name="value">Receives the value.</param>
 972        /// <remarks>
 973        ///   This method does not parse the contents of a JSON string value.
 974        /// </remarks>
 975        /// <returns>
 976        ///   <see langword="true"/> if the number can be represented as a <see cref="decimal"/>,
 977        ///   <see langword="false"/> otherwise.
 978        /// </returns>
 979        /// <exception cref="InvalidOperationException">
 980        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 981        /// </exception>
 982        /// <exception cref="ObjectDisposedException">
 983        ///   The parent <see cref="JsonDocument"/> has been disposed.
 984        /// </exception>
 985        /// <seealso cref="GetRawText"/>
 986        public bool TryGetDecimal(out decimal value)
 0987        {
 0988            CheckValidInstance();
 989
 0990            return _parent.TryGetValue(_idx, out value);
 0991        }
 992
 993        /// <summary>
 994        ///   Gets the current JSON number as a <see cref="decimal"/>.
 995        /// </summary>
 996        /// <returns>The current JSON number as a <see cref="decimal"/>.</returns>
 997        /// <remarks>
 998        ///   This method does not parse the contents of a JSON string value.
 999        /// </remarks>
 1000        /// <exception cref="InvalidOperationException">
 1001        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>.
 1002        /// </exception>
 1003        /// <exception cref="FormatException">
 1004        ///   The value cannot be represented as a <see cref="decimal"/>.
 1005        /// </exception>
 1006        /// <exception cref="ObjectDisposedException">
 1007        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1008        /// </exception>
 1009        /// <seealso cref="GetRawText"/>
 1010        public decimal GetDecimal()
 01011        {
 01012            if (!TryGetDecimal(out decimal value))
 01013            {
 01014                ThrowHelper.ThrowFormatException();
 1015            }
 1016
 01017            return value;
 01018        }
 1019
 1020        /// <summary>
 1021        ///   Attempts to represent the current JSON string as a <see cref="DateTime"/>.
 1022        /// </summary>
 1023        /// <param name="value">Receives the value.</param>
 1024        /// <remarks>
 1025        ///   This method does not create a DateTime representation of values other than JSON strings.
 1026        /// </remarks>
 1027        /// <returns>
 1028        ///   <see langword="true"/> if the string can be represented as a <see cref="DateTime"/>,
 1029        ///   <see langword="false"/> otherwise.
 1030        /// </returns>
 1031        /// <exception cref="InvalidOperationException">
 1032        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1033        /// </exception>
 1034        /// <exception cref="ObjectDisposedException">
 1035        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1036        /// </exception>
 1037        public bool TryGetDateTime(out DateTime value)
 01038        {
 01039            CheckValidInstance();
 1040
 01041            return _parent.TryGetValue(_idx, out value);
 01042        }
 1043
 1044        /// <summary>
 1045        ///   Gets the value of the element as a <see cref="DateTime"/>.
 1046        /// </summary>
 1047        /// <remarks>
 1048        ///   This method does not create a DateTime representation of values other than JSON strings.
 1049        /// </remarks>
 1050        /// <returns>The value of the element as a <see cref="DateTime"/>.</returns>
 1051        /// <exception cref="InvalidOperationException">
 1052        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1053        /// </exception>
 1054        /// <exception cref="FormatException">
 1055        ///   The value cannot be represented as a <see cref="DateTime"/>.
 1056        /// </exception>
 1057        /// <exception cref="ObjectDisposedException">
 1058        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1059        /// </exception>
 1060        /// <seealso cref="ToString"/>
 1061        public DateTime GetDateTime()
 01062        {
 01063            if (!TryGetDateTime(out DateTime value))
 01064            {
 01065                ThrowHelper.ThrowFormatException();
 1066            }
 1067
 01068            return value;
 01069        }
 1070
 1071        /// <summary>
 1072        ///   Attempts to represent the current JSON string as a <see cref="DateTimeOffset"/>.
 1073        /// </summary>
 1074        /// <param name="value">Receives the value.</param>
 1075        /// <remarks>
 1076        ///   This method does not create a DateTimeOffset representation of values other than JSON strings.
 1077        /// </remarks>
 1078        /// <returns>
 1079        ///   <see langword="true"/> if the string can be represented as a <see cref="DateTimeOffset"/>,
 1080        ///   <see langword="false"/> otherwise.
 1081        /// </returns>
 1082        /// <exception cref="InvalidOperationException">
 1083        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1084        /// </exception>
 1085        /// <exception cref="ObjectDisposedException">
 1086        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1087        /// </exception>
 1088        public bool TryGetDateTimeOffset(out DateTimeOffset value)
 01089        {
 01090            CheckValidInstance();
 1091
 01092            return _parent.TryGetValue(_idx, out value);
 01093        }
 1094
 1095        /// <summary>
 1096        ///   Gets the value of the element as a <see cref="DateTimeOffset"/>.
 1097        /// </summary>
 1098        /// <remarks>
 1099        ///   This method does not create a DateTimeOffset representation of values other than JSON strings.
 1100        /// </remarks>
 1101        /// <returns>The value of the element as a <see cref="DateTimeOffset"/>.</returns>
 1102        /// <exception cref="InvalidOperationException">
 1103        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1104        /// </exception>
 1105        /// <exception cref="FormatException">
 1106        ///   The value cannot be represented as a <see cref="DateTimeOffset"/>.
 1107        /// </exception>
 1108        /// <exception cref="ObjectDisposedException">
 1109        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1110        /// </exception>
 1111        /// <seealso cref="ToString"/>
 1112        public DateTimeOffset GetDateTimeOffset()
 01113        {
 01114            if (!TryGetDateTimeOffset(out DateTimeOffset value))
 01115            {
 01116                ThrowHelper.ThrowFormatException();
 1117            }
 1118
 01119            return value;
 01120        }
 1121
 1122        /// <summary>
 1123        ///   Attempts to represent the current JSON string as a <see cref="Guid"/>.
 1124        /// </summary>
 1125        /// <param name="value">Receives the value.</param>
 1126        /// <remarks>
 1127        ///   This method does not create a Guid representation of values other than JSON strings.
 1128        /// </remarks>
 1129        /// <returns>
 1130        ///   <see langword="true"/> if the string can be represented as a <see cref="Guid"/>,
 1131        ///   <see langword="false"/> otherwise.
 1132        /// </returns>
 1133        /// <exception cref="InvalidOperationException">
 1134        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1135        /// </exception>
 1136        /// <exception cref="ObjectDisposedException">
 1137        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1138        /// </exception>
 1139        public bool TryGetGuid(out Guid value)
 01140        {
 01141            CheckValidInstance();
 1142
 01143            return _parent.TryGetValue(_idx, out value);
 01144        }
 1145
 1146        /// <summary>
 1147        ///   Gets the value of the element as a <see cref="Guid"/>.
 1148        /// </summary>
 1149        /// <remarks>
 1150        ///   This method does not create a Guid representation of values other than JSON strings.
 1151        /// </remarks>
 1152        /// <returns>The value of the element as a <see cref="Guid"/>.</returns>
 1153        /// <exception cref="InvalidOperationException">
 1154        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1155        /// </exception>
 1156        /// <exception cref="FormatException">
 1157        ///   The value cannot be represented as a <see cref="Guid"/>.
 1158        /// </exception>
 1159        /// <exception cref="ObjectDisposedException">
 1160        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1161        /// </exception>
 1162        /// <seealso cref="ToString"/>
 1163        public Guid GetGuid()
 01164        {
 01165            if (!TryGetGuid(out Guid value))
 01166            {
 01167                ThrowHelper.ThrowFormatException();
 1168            }
 1169
 01170            return value;
 01171        }
 1172
 1173        internal string GetPropertyName()
 01174        {
 01175            CheckValidInstance();
 1176
 01177            return _parent.GetNameOfPropertyValue(_idx);
 01178        }
 1179
 1180        internal ReadOnlySpan<byte> GetPropertyNameRaw()
 01181        {
 01182            CheckValidInstance();
 1183
 01184            return _parent.GetPropertyNameRaw(_idx);
 01185        }
 1186
 1187        /// <summary>
 1188        ///   Gets the original input data backing this value, returning it as a <see cref="string"/>.
 1189        /// </summary>
 1190        /// <returns>
 1191        ///   The original input data backing this value, returning it as a <see cref="string"/>.
 1192        /// </returns>
 1193        /// <exception cref="ObjectDisposedException">
 1194        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1195        /// </exception>
 1196        public string GetRawText()
 01197        {
 01198            CheckValidInstance();
 1199
 01200            return _parent.GetRawValueAsString(_idx);
 01201        }
 1202
 1203        internal ReadOnlyMemory<byte> GetRawValue()
 01204        {
 01205            CheckValidInstance();
 1206
 01207            return _parent.GetRawValue(_idx, includeQuotes: true);
 01208        }
 1209
 1210        internal string GetPropertyRawText()
 01211        {
 01212            CheckValidInstance();
 1213
 01214            return _parent.GetPropertyRawValueAsString(_idx);
 01215        }
 1216
 1217        internal bool ValueIsEscaped
 1218        {
 1219            get
 01220            {
 01221                CheckValidInstance();
 1222
 01223                return _parent.ValueIsEscaped(_idx, isPropertyName: false);
 01224            }
 1225        }
 1226
 1227        internal ReadOnlySpan<byte> ValueSpan
 1228        {
 1229            get
 01230            {
 01231                CheckValidInstance();
 1232
 01233                return _parent.GetRawValue(_idx, includeQuotes: false).Span;
 01234            }
 1235        }
 1236
 1237        /// <summary>
 1238        /// Compares the values of two <see cref="JsonElement"/> values for equality, including the values of all descen
 1239        /// </summary>
 1240        /// <param name="element1">The first <see cref="JsonElement"/> to compare.</param>
 1241        /// <param name="element2">The second <see cref="JsonElement"/> to compare.</param>
 1242        /// <returns><see langword="true"/> if the two values are equal; otherwise, <see langword="false"/>.</returns>
 1243        /// <remarks>
 1244        /// Deep equality of two JSON values is defined as follows:
 1245        /// <list type="bullet">
 1246        /// <item>JSON values of different kinds are not equal.</item>
 1247        /// <item>JSON constants <see langword="null"/>, <see langword="false"/>, and <see langword="true"/> only equal 
 1248        /// <item>JSON numbers are equal if and only if they have they have equivalent decimal representations, with no 
 1249        /// <item>JSON strings are equal if and only if they are equal using ordinal string comparison.</item>
 1250        /// <item>JSON arrays are equal if and only if they are of equal length and each of their elements are pairwise 
 1251        /// <item>
 1252        ///     JSON objects are equal if and only if they have the same number of properties and each property in the f
 1253        ///     has a corresponding property in the second object with the same name and equal value. The order of prope
 1254        ///     significant, with the exception of repeated properties that must be specified in the same order (with in
 1255        /// </item>
 1256        /// </list>
 1257        /// </remarks>
 1258        public static bool DeepEquals(JsonElement element1, JsonElement element2)
 01259        {
 01260            if (!RuntimeHelpers.TryEnsureSufficientExecutionStack())
 01261            {
 01262                ThrowHelper.ThrowInsufficientExecutionStackException_JsonElementDeepEqualsInsufficientExecutionStack();
 1263            }
 1264
 01265            element1.CheckValidInstance();
 01266            element2.CheckValidInstance();
 1267
 01268            JsonValueKind kind = element1.ValueKind;
 01269            if (kind != element2.ValueKind)
 01270            {
 01271                return false;
 1272            }
 1273
 01274            switch (kind)
 1275            {
 1276                case JsonValueKind.Null or JsonValueKind.False or JsonValueKind.True:
 01277                    return true;
 1278
 1279                case JsonValueKind.Number:
 01280                    return JsonHelpers.AreEqualJsonNumbers(element1.GetRawValue().Span, element2.GetRawValue().Span);
 1281
 1282                case JsonValueKind.String:
 01283                    if (element2.ValueIsEscaped)
 01284                    {
 01285                        if (element1.ValueIsEscaped)
 01286                        {
 1287                            // Need to unescape and compare both inputs.
 01288                            return JsonReaderHelper.UnescapeAndCompareBothInputs(element1.ValueSpan, element2.ValueSpan)
 1289                        }
 1290
 1291                        // Swap values so that unescaping is handled by the LHS.
 01292                        (element1, element2) = (element2, element1);
 01293                    }
 1294
 01295                    return element1.ValueEquals(element2.ValueSpan);
 1296
 1297                case JsonValueKind.Array:
 01298                    if (element1.GetArrayLength() != element2.GetArrayLength())
 01299                    {
 01300                        return false;
 1301                    }
 1302
 01303                    ArrayEnumerator arrayEnumerator2 = element2.EnumerateArray();
 01304                    foreach (JsonElement e1 in element1.EnumerateArray())
 01305                    {
 01306                        bool success = arrayEnumerator2.MoveNext();
 01307                        Debug.Assert(success, "enumerators must have matching length");
 1308
 01309                        if (!DeepEquals(e1, arrayEnumerator2.Current))
 01310                        {
 01311                            return false;
 1312                        }
 01313                    }
 1314
 01315                    Debug.Assert(!arrayEnumerator2.MoveNext());
 01316                    return true;
 1317
 1318                default:
 01319                    Debug.Assert(kind is JsonValueKind.Object);
 1320
 01321                    int count = element1.GetPropertyCount();
 01322                    if (count != element2.GetPropertyCount())
 01323                    {
 01324                        return false;
 1325                    }
 1326
 01327                    ObjectEnumerator objectEnumerator1 = element1.EnumerateObject();
 01328                    ObjectEnumerator objectEnumerator2 = element2.EnumerateObject();
 1329
 1330                    // Two JSON objects are considered equal if they define the same set of properties.
 1331                    // Start optimistically with pairwise comparison, but fall back to unordered
 1332                    // comparison as soon as a mismatch is encountered.
 1333
 01334                    while (objectEnumerator1.MoveNext())
 01335                    {
 01336                        bool success = objectEnumerator2.MoveNext();
 01337                        Debug.Assert(success, "enumerators should have matching lengths");
 1338
 01339                        JsonProperty prop1 = objectEnumerator1.Current;
 01340                        JsonProperty prop2 = objectEnumerator2.Current;
 1341
 01342                        if (!NameEquals(prop1, prop2))
 01343                        {
 1344                            // We have our first mismatch, fall back to unordered comparison.
 01345                            return UnorderedObjectDeepEquals(objectEnumerator1, objectEnumerator2, remainingProps: count
 1346                        }
 1347
 01348                        if (!DeepEquals(prop1.Value, prop2.Value))
 01349                        {
 01350                            return false;
 1351                        }
 1352
 01353                        count--;
 01354                    }
 1355
 01356                    Debug.Assert(!objectEnumerator2.MoveNext());
 01357                    return true;
 1358
 1359                    static bool UnorderedObjectDeepEquals(ObjectEnumerator objectEnumerator1, ObjectEnumerator objectEnu
 01360                    {
 1361                        // JsonElement objects allow duplicate property names, which is optional per the JSON RFC.
 1362                        // Even though this implementation of equality does not take property ordering into account,
 1363                        // repeated property names must be specified in the same order (although they may be interleaved
 1364                        // This is to preserve a degree of coherence with JSON serialization, where either the first
 1365                        // or last occurrence of a repeated property name is used. It also simplifies the implementation
 1366                        // and keeps it at O(n + m) complexity.
 1367
 01368                        Dictionary<string, ValueQueue<JsonElement>> properties2 = new(capacity: remainingProps, StringCo
 1369                        do
 01370                        {
 01371                            JsonProperty prop2 = objectEnumerator2.Current;
 1372#if NET
 01373                            ref ValueQueue<JsonElement> values = ref CollectionsMarshal.GetValueRefOrAddDefault(properti
 1374#else
 1375                            properties2.TryGetValue(prop2.Name, out ValueQueue<JsonElement> values);
 1376#endif
 01377                            values.Enqueue(prop2.Value);
 1378#if !NET
 1379                            properties2[prop2.Name] = values;
 1380#endif
 01381                        }
 01382                        while (objectEnumerator2.MoveNext());
 1383
 1384                        do
 01385                        {
 01386                            JsonProperty prop = objectEnumerator1.Current;
 1387#if NET
 01388                            ref ValueQueue<JsonElement> values = ref CollectionsMarshal.GetValueRefOrAddDefault(properti
 1389#else
 1390                            bool exists = properties2.TryGetValue(prop.Name, out ValueQueue<JsonElement> values);
 1391#endif
 01392                            if (!exists || !values.TryDequeue(out JsonElement value) || !DeepEquals(prop.Value, value))
 01393                            {
 01394                                return false;
 1395                            }
 1396#if !NET
 1397                            properties2[prop.Name] = values;
 1398#endif
 01399                        }
 01400                        while (objectEnumerator1.MoveNext());
 1401
 01402                        return true;
 01403                    }
 1404
 1405                    static bool NameEquals(JsonProperty left, JsonProperty right)
 01406                    {
 01407                        if (right.NameIsEscaped)
 01408                        {
 01409                            if (left.NameIsEscaped)
 01410                            {
 1411                                // Need to unescape and compare both inputs.
 01412                                return JsonReaderHelper.UnescapeAndCompareBothInputs(left.NameSpan, right.NameSpan);
 1413                            }
 1414
 1415                            // Swap values so that unescaping is handled by the LHS
 01416                            (left, right) = (right, left);
 01417                        }
 1418
 01419                        return left.NameEquals(right.NameSpan);
 01420                    }
 1421            }
 01422        }
 1423
 1424        /// <summary>
 1425        ///   Compares <paramref name="text" /> to the string value of this element.
 1426        /// </summary>
 1427        /// <param name="text">The text to compare against.</param>
 1428        /// <returns>
 1429        ///   <see langword="true" /> if the string value of this element matches <paramref name="text"/>,
 1430        ///   <see langword="false" /> otherwise.
 1431        /// </returns>
 1432        /// <exception cref="InvalidOperationException">
 1433        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1434        /// </exception>
 1435        /// <remarks>
 1436        ///   This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and
 1437        ///   the result of calling <see cref="GetString" />, but avoids creating the string instance.
 1438        /// </remarks>
 1439        public bool ValueEquals(string? text)
 01440        {
 1441            // CheckValidInstance is done in the helper
 1442
 01443            if (TokenType == JsonTokenType.Null)
 01444            {
 01445                return text is null;
 1446            }
 1447
 01448            return TextEqualsHelper(text.AsSpan(), isPropertyName: false);
 01449        }
 1450
 1451        /// <summary>
 1452        ///   Compares the text represented by <paramref name="utf8Text" /> to the string value of this element.
 1453        /// </summary>
 1454        /// <param name="utf8Text">The UTF-8 encoded text to compare against.</param>
 1455        /// <returns>
 1456        ///   <see langword="true" /> if the string value of this element has the same UTF-8 encoding as
 1457        ///   <paramref name="utf8Text" />, <see langword="false" /> otherwise.
 1458        /// </returns>
 1459        /// <exception cref="InvalidOperationException">
 1460        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1461        /// </exception>
 1462        /// <remarks>
 1463        ///   This method is functionally equal to doing an ordinal comparison of the string produced by UTF-8 decoding
 1464        ///   <paramref name="utf8Text" /> with the result of calling <see cref="GetString" />, but avoids creating the
 1465        ///   string instances.
 1466        /// </remarks>
 1467        public bool ValueEquals(ReadOnlySpan<byte> utf8Text)
 01468        {
 1469            // CheckValidInstance is done in the helper
 1470
 01471            if (TokenType == JsonTokenType.Null)
 01472            {
 1473                // This is different than Length == 0, in that it tests true for null, but false for ""
 1474#pragma warning disable CA2265
 01475                return utf8Text.Slice(0, 0) == default;
 1476#pragma warning restore CA2265
 1477            }
 1478
 01479            return TextEqualsHelper(utf8Text, isPropertyName: false, shouldUnescape: true);
 01480        }
 1481
 1482        /// <summary>
 1483        ///   Compares <paramref name="text" /> to the string value of this element.
 1484        /// </summary>
 1485        /// <param name="text">The text to compare against.</param>
 1486        /// <returns>
 1487        ///   <see langword="true" /> if the string value of this element matches <paramref name="text"/>,
 1488        ///   <see langword="false" /> otherwise.
 1489        /// </returns>
 1490        /// <exception cref="InvalidOperationException">
 1491        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
 1492        /// </exception>
 1493        /// <remarks>
 1494        ///   This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and
 1495        ///   the result of calling <see cref="GetString" />, but avoids creating the string instance.
 1496        /// </remarks>
 1497        public bool ValueEquals(ReadOnlySpan<char> text)
 01498        {
 1499            // CheckValidInstance is done in the helper
 1500
 01501            if (TokenType == JsonTokenType.Null)
 01502            {
 1503                // This is different than Length == 0, in that it tests true for null, but false for ""
 1504#pragma warning disable CA2265
 01505                return text.Slice(0, 0) == default;
 1506#pragma warning restore CA2265
 1507            }
 1508
 01509            return TextEqualsHelper(text, isPropertyName: false);
 01510        }
 1511
 1512        internal bool TextEqualsHelper(ReadOnlySpan<byte> utf8Text, bool isPropertyName, bool shouldUnescape)
 01513        {
 01514            CheckValidInstance();
 1515
 01516            return _parent.TextEquals(_idx, utf8Text, isPropertyName, shouldUnescape);
 01517        }
 1518
 1519        internal bool TextEqualsHelper(ReadOnlySpan<char> text, bool isPropertyName)
 01520        {
 01521            CheckValidInstance();
 1522
 01523            return _parent.TextEquals(_idx, text, isPropertyName);
 01524        }
 1525
 1526        internal bool ValueIsEscapedHelper(bool isPropertyName)
 01527        {
 01528            CheckValidInstance();
 1529
 01530            return _parent.ValueIsEscaped(_idx, isPropertyName);
 01531        }
 1532
 1533        /// <summary>
 1534        ///   Write the element into the provided writer as a JSON value.
 1535        /// </summary>
 1536        /// <param name="writer">The writer.</param>
 1537        /// <exception cref="ArgumentNullException">
 1538        ///   The <paramref name="writer"/> parameter is <see langword="null"/>.
 1539        /// </exception>
 1540        /// <exception cref="InvalidOperationException">
 1541        ///   This value's <see cref="ValueKind"/> is <see cref="JsonValueKind.Undefined"/>.
 1542        /// </exception>
 1543        /// <exception cref="ObjectDisposedException">
 1544        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1545        /// </exception>
 1546        public void WriteTo(Utf8JsonWriter writer)
 01547        {
 01548            ArgumentNullException.ThrowIfNull(writer);
 1549
 01550            CheckValidInstance();
 1551
 01552            _parent.WriteElementTo(_idx, writer);
 01553        }
 1554
 1555        internal void WritePropertyNameTo(Utf8JsonWriter writer)
 01556        {
 01557            CheckValidInstance();
 1558
 01559            _parent.WritePropertyName(_idx, writer);
 01560        }
 1561
 1562        /// <summary>
 1563        ///   Get an enumerator to enumerate the values in the JSON array represented by this JsonElement.
 1564        /// </summary>
 1565        /// <returns>
 1566        ///   An enumerator to enumerate the values in the JSON array represented by this JsonElement.
 1567        /// </returns>
 1568        /// <exception cref="InvalidOperationException">
 1569        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Array"/>.
 1570        /// </exception>
 1571        /// <exception cref="ObjectDisposedException">
 1572        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1573        /// </exception>
 1574        public ArrayEnumerator EnumerateArray()
 01575        {
 01576            CheckValidInstance();
 1577
 01578            JsonTokenType tokenType = TokenType;
 1579
 01580            if (tokenType != JsonTokenType.StartArray)
 01581            {
 01582                ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartArray, tokenType);
 1583            }
 1584
 01585            return new ArrayEnumerator(this);
 01586        }
 1587
 1588        /// <summary>
 1589        ///   Get an enumerator to enumerate the properties in the JSON object represented by this JsonElement.
 1590        /// </summary>
 1591        /// <returns>
 1592        ///   An enumerator to enumerate the properties in the JSON object represented by this JsonElement.
 1593        /// </returns>
 1594        /// <exception cref="InvalidOperationException">
 1595        ///   This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>.
 1596        /// </exception>
 1597        /// <exception cref="ObjectDisposedException">
 1598        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1599        /// </exception>
 1600        public ObjectEnumerator EnumerateObject()
 01601        {
 01602            CheckValidInstance();
 1603
 01604            JsonTokenType tokenType = TokenType;
 1605
 01606            if (tokenType != JsonTokenType.StartObject)
 01607            {
 01608                ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType);
 1609            }
 1610
 01611            return new ObjectEnumerator(this);
 01612        }
 1613
 1614        /// <summary>
 1615        ///   Gets a string representation for the current value appropriate to the value type.
 1616        /// </summary>
 1617        /// <remarks>
 1618        ///   <para>
 1619        ///     For JsonElement built from <see cref="JsonDocument"/>:
 1620        ///   </para>
 1621        ///
 1622        ///   <para>
 1623        ///     For <see cref="JsonValueKind.Null"/>, <see cref="string.Empty"/> is returned.
 1624        ///   </para>
 1625        ///
 1626        ///   <para>
 1627        ///     For <see cref="JsonValueKind.True"/>, <see cref="bool.TrueString"/> is returned.
 1628        ///   </para>
 1629        ///
 1630        ///   <para>
 1631        ///     For <see cref="JsonValueKind.False"/>, <see cref="bool.FalseString"/> is returned.
 1632        ///   </para>
 1633        ///
 1634        ///   <para>
 1635        ///     For <see cref="JsonValueKind.String"/>, the value of <see cref="GetString"/>() is returned.
 1636        ///   </para>
 1637        ///
 1638        ///   <para>
 1639        ///     For other types, the value of <see cref="GetRawText"/>() is returned.
 1640        ///   </para>
 1641        /// </remarks>
 1642        /// <returns>
 1643        ///   A string representation for the current value appropriate to the value type.
 1644        /// </returns>
 1645        /// <exception cref="ObjectDisposedException">
 1646        ///   The parent <see cref="JsonDocument"/> has been disposed.
 1647        /// </exception>
 1648        public override string ToString()
 01649        {
 01650            switch (TokenType)
 1651            {
 1652                case JsonTokenType.None:
 1653                case JsonTokenType.Null:
 01654                    return string.Empty;
 1655                case JsonTokenType.True:
 01656                    return bool.TrueString;
 1657                case JsonTokenType.False:
 01658                    return bool.FalseString;
 1659                case JsonTokenType.Number:
 1660                case JsonTokenType.StartArray:
 1661                case JsonTokenType.StartObject:
 01662                    {
 1663                        // null parent should have hit the None case
 01664                        Debug.Assert(_parent is not null);
 01665                        return _parent.GetRawValueAsString(_idx);
 1666                    }
 1667                case JsonTokenType.String:
 01668                    return GetString()!;
 1669                case JsonTokenType.Comment:
 1670                case JsonTokenType.EndArray:
 1671                case JsonTokenType.EndObject:
 1672                default:
 01673                    Debug.Fail($"No handler for {nameof(JsonTokenType)}.{TokenType}");
 1674                    return string.Empty;
 1675            }
 01676        }
 1677
 1678        /// <summary>
 1679        ///   Get a JsonElement which can be safely stored beyond the lifetime of the
 1680        ///   original <see cref="JsonDocument"/>.
 1681        /// </summary>
 1682        /// <returns>
 1683        ///   A JsonElement which can be safely stored beyond the lifetime of the
 1684        ///   original <see cref="JsonDocument"/>.
 1685        /// </returns>
 1686        /// <remarks>
 1687        ///   <para>
 1688        ///     If this JsonElement is itself the output of a previous call to Clone, or
 1689        ///     a value contained within another JsonElement which was the output of a previous
 1690        ///     call to Clone, this method results in no additional memory allocation.
 1691        ///   </para>
 1692        /// </remarks>
 1693        public JsonElement Clone()
 01694        {
 01695            CheckValidInstance();
 1696
 01697            if (!_parent.IsDisposable)
 01698            {
 01699                return this;
 1700            }
 1701
 01702            return _parent.CloneElement(_idx);
 01703        }
 1704
 1705        private void CheckValidInstance()
 01706        {
 01707            if (_parent is null)
 01708            {
 01709                throw new InvalidOperationException();
 1710            }
 01711        }
 1712
 01713        internal readonly int MetadataDbIndex => _idx;
 1714
 1715        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 01716        private string DebuggerDisplay => $"ValueKind = {ValueKind} : \"{ToString()}\"";
 1717    }
 1718}
 1719

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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}
 111

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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)
 1648        {
 1649            bool ret = JsonDocument.TryParseValue(ref reader, out JsonDocument? document, shouldThrow: true, useArrayPoo
 50
 051            Debug.Assert(ret, "TryParseValue returned false with shouldThrow: true.");
 052            Debug.Assert(document is not null, "null document returned with shouldThrow: true.");
 053            return document.RootElement;
 054        }
 55
 56        internal static JsonElement ParseValue(ref Utf8JsonReader reader, bool allowDuplicateProperties)
 3257        {
 3258            bool ret = JsonDocument.TryParseValue(
 3259                ref reader,
 3260                out JsonDocument? document,
 3261                shouldThrow: true,
 3262                useArrayPools: false,
 3263                allowDuplicateProperties: allowDuplicateProperties);
 64
 865            Debug.Assert(ret, "TryParseValue returned false with shouldThrow: true.");
 866            Debug.Assert(document is not null, "null document returned with shouldThrow: true.");
 867            return document.RootElement;
 868        }
 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}
 167

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>&)