< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 28
Coverable lines: 28
Total lines: 141
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
NameEquals(...)100%110%
NameEquals(...)100%110%
NameEquals(...)100%110%
EscapedNameEquals(...)100%110%
WriteTo(...)100%110%
ToString()100%110%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Document\JsonProperty.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;
 5using System.Diagnostics;
 6
 7namespace System.Text.Json
 8{
 9    /// <summary>
 10    ///   Represents a single property for a JSON object.
 11    /// </summary>
 12    [DebuggerDisplay("{DebuggerDisplay,nq}")]
 13    public readonly struct JsonProperty
 14    {
 15        /// <summary>
 16        ///   The value of this property.
 17        /// </summary>
 018        public JsonElement Value { get; }
 19
 20        internal JsonProperty(JsonElement value)
 021        {
 022            Value = value;
 023        }
 24
 25        /// <summary>
 26        ///   The name of this property.
 27        ///   This allocates a new string instance for each call.
 28        /// </summary>
 029        public string Name => Value.GetPropertyName();
 30
 31        /// <summary>
 32        ///   Compares <paramref name="text" /> to the name of this property.
 33        /// </summary>
 34        /// <param name="text">The text to compare against.</param>
 35        /// <returns>
 36        ///   <see langword="true" /> if the name of this property matches <paramref name="text"/>,
 37        ///   <see langword="false" /> otherwise.
 38        /// </returns>
 39        /// <exception cref="InvalidOperationException">
 40        ///   This value's <see cref="Type"/> is not <see cref="JsonTokenType.PropertyName"/>.
 41        /// </exception>
 42        /// <remarks>
 43        ///   This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and
 44        ///   <see cref="Name" />, but can avoid creating the string instance.
 45        /// </remarks>
 46        public bool NameEquals(string? text)
 047        {
 048            return NameEquals(text.AsSpan());
 049        }
 50
 51        /// <summary>
 52        ///   Compares the text represented by <paramref name="utf8Text" /> to the name of this property.
 53        /// </summary>
 54        /// <param name="utf8Text">The UTF-8 encoded text to compare against.</param>
 55        /// <returns>
 56        ///   <see langword="true" /> if the name of this property has the same UTF-8 encoding as
 57        ///   <paramref name="utf8Text" />, <see langword="false" /> otherwise.
 58        /// </returns>
 59        /// <exception cref="InvalidOperationException">
 60        ///   This value's <see cref="Type"/> is not <see cref="JsonTokenType.PropertyName"/>.
 61        /// </exception>
 62        /// <remarks>
 63        ///   This method is functionally equal to doing an ordinal comparison of <paramref name="utf8Text" /> and
 64        ///   <see cref="Name" />, but can avoid creating the string instance.
 65        /// </remarks>
 66        public bool NameEquals(ReadOnlySpan<byte> utf8Text)
 067        {
 068            return Value.TextEqualsHelper(utf8Text, isPropertyName: true, shouldUnescape: true);
 069        }
 70
 71        /// <summary>
 72        ///   Compares <paramref name="text" /> to the name of this property.
 73        /// </summary>
 74        /// <param name="text">The text to compare against.</param>
 75        /// <returns>
 76        ///   <see langword="true" /> if the name of this property matches <paramref name="text"/>,
 77        ///   <see langword="false" /> otherwise.
 78        /// </returns>
 79        /// <exception cref="InvalidOperationException">
 80        ///   This value's <see cref="Type"/> is not <see cref="JsonTokenType.PropertyName"/>.
 81        /// </exception>
 82        /// <remarks>
 83        ///   This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and
 84        ///   <see cref="Name" />, but can avoid creating the string instance.
 85        /// </remarks>
 86        public bool NameEquals(ReadOnlySpan<char> text)
 087        {
 088            return Value.TextEqualsHelper(text, isPropertyName: true);
 089        }
 90
 91        internal bool EscapedNameEquals(ReadOnlySpan<byte> utf8Text)
 092        {
 093            return Value.TextEqualsHelper(utf8Text, isPropertyName: true, shouldUnescape: false);
 094        }
 95
 096        internal bool NameIsEscaped => Value.ValueIsEscapedHelper(isPropertyName: true);
 097        internal ReadOnlySpan<byte> NameSpan => Value.GetPropertyNameRaw();
 98
 99        /// <summary>
 100        ///   Write the property into the provided writer as a named JSON object property.
 101        /// </summary>
 102        /// <param name="writer">The writer.</param>
 103        /// <exception cref="ArgumentNullException">
 104        ///   The <paramref name="writer"/> parameter is <see langword="null"/>.
 105        /// </exception>
 106        /// <exception cref="ArgumentException">
 107        ///   This <see cref="Name"/>'s length is too large to be a JSON object property.
 108        /// </exception>
 109        /// <exception cref="InvalidOperationException">
 110        ///   This <see cref="Value"/>'s <see cref="JsonElement.ValueKind"/> would result in an invalid JSON.
 111        /// </exception>
 112        /// <exception cref="ObjectDisposedException">
 113        ///   The parent <see cref="JsonDocument"/> has been disposed.
 114        /// </exception>>
 115        public void WriteTo(Utf8JsonWriter writer)
 0116        {
 0117            ArgumentNullException.ThrowIfNull(writer);
 118
 0119            Value.WritePropertyNameTo(writer);
 0120            Value.WriteTo(writer);
 0121        }
 122
 123        /// <summary>
 124        ///   Provides a <see cref="string"/> representation of the property for
 125        ///   debugging purposes.
 126        /// </summary>
 127        /// <returns>
 128        ///   A string containing the un-interpreted value of the property, beginning
 129        ///   at the declaring open-quote and ending at the last character that is part of
 130        ///   the value.
 131        /// </returns>
 132        public override string ToString()
 0133        {
 0134            return Value.GetPropertyRawText();
 0135        }
 136
 137        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 138        private string DebuggerDisplay
 0139            => Value.ValueKind == JsonValueKind.Undefined ? "<Undefined>" : $"\"{ToString()}\"";
 140    }
 141}